Skip to content

Commit

Permalink
Add comments option tab, #2
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberhobo committed Apr 28, 2017
1 parent 9157705 commit db046ce
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 0 deletions.
28 changes: 28 additions & 0 deletions source/Admin/Options_Tabs/Comments.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
namespace Postmatic\Commentium\Admin\Options_Tabs;

use Prompt_Admin_Comment_Options_Tab;

class Comments extends Prompt_Admin_Comment_Options_Tab {

protected function table_entries() {
$entries = parent::table_entries();

if ( ! $this->options->is_api_transport() ) {
return $entries;
}

$entries[2]['title'] = __( 'Comment digests', 'postmatic-premium' );
$entries[2]['desc'] = __(
'How many comments during a 14 hour period will trigger comment digests to be sent instead of individual comments? There is a minimum of 3.',
'postmatic-premium'
) . html( 'p',
__(
'Postmatic automatically combines comment notifications on posts that go viral so your users do not get too many emails. Setting the trigger to 3 comments is good for most sites.',
'postmatic-premium'
)
);

return $entries;
}
}
19 changes: 19 additions & 0 deletions source/Filters/Options.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php
namespace Postmatic\Commentium\Filters;

/**
* Filter option values.
*/
class Options {

/**
* Add an option to enable replies only.
*
* @param array $options
* @return array
*/
public static function default_options( array $options = array() ) {
$options['enable_replies_only'] = false;
return $options;
}
}
48 changes: 48 additions & 0 deletions source/Filters/Options_Page.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php
namespace Postmatic\Commentium\Filters;

use Postmatic\Commentium\Admin\Options_Tabs;

use Prompt_Core;

/**
* Filter basic options page items.
* @since 1.0.1
*/
class Options_Page {

/**
* Add premium tabs to the basic ones in hard-coded order.
*
* @since 1.0.1
* @param \Prompt_Admin_Options_Tab[] $tabs
* @return \Prompt_Admin_Options_Tab[]
*/
public static function tabs( $tabs ) {
$new_tabs = array();

foreach( $tabs as $tab ) {
$new_tabs[] = $tab;
static::maybe_append_tab( $new_tabs );
}

return $new_tabs;
}

/**
* Look at the last tab in a list and decide whether to replace it or add our tabs after it. May change the passed array.
* @since 1.0.1
* @param \Prompt_Admin_Options_Tab[] $tabs
*/
protected static function maybe_append_tab( &$tabs ) {

$last_tab = $tabs[count($tabs)-1];

if ( 'Prompt_Admin_Comment_Options_Tab' === get_class( $last_tab ) and Prompt_Core::$options->is_api_transport() ) {
array_pop( $tabs );
$tabs[] = new Options_Tabs\Comments( Prompt_Core::$options );
}

}

}
33 changes: 33 additions & 0 deletions tests/phpunit/Admin/Options_Tabs/Comments.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php
namespace Postmatic\Commentium\Unit_Tests\Admin\Options_Tabs;

use Postmatic\Commentium\Admin\Options_Tabs;

use Prompt_Core;
use Prompt_Enum_Email_Transports;

use WP_UnitTestCase;

class Comments extends WP_UnitTestCase {

public function test_render_local() {
$tab = new Options_Tabs\Comments( Prompt_Core::$options );

$content = $tab->render();

$this->assertContains( 'Comment flood control', $content, 'Expected the old flood control title.' );
$this->assertContains( 'comment_flood_control_trigger_count', $content, 'Expected the same flood control field name.' );
}

public function test_render_api() {
Prompt_Core::$options->set( 'email_transport', Prompt_Enum_Email_Transports::API );

$tab = new Options_Tabs\Comments( Prompt_Core::$options );

$content = $tab->render();

$this->assertContains( 'Comment digests', $content, 'Expected a new flood control title.' );
$this->assertContains( 'comment_flood_control_trigger_count', $content, 'Expected the same flood control field name.' );
}

}
23 changes: 23 additions & 0 deletions tests/phpunit/Filters/Options.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

namespace Postmatic\Commentium\Unit_Tests\Filters;

use Postmatic\Commentium\Filters;

use \WP_UnitTestCase;

class Options extends WP_UnitTestCase {

public function test_default_options() {
$original_options = array( 'foo' => 'bar' );

$filtered_options = Filters\Options::default_options( $original_options );

$this->assertEquals(
array_merge( $original_options, array( 'enable_replies_only' => '' ) ),
$filtered_options,
'Expected custom replies only option to be added.'
);
}

}
43 changes: 43 additions & 0 deletions tests/phpunit/Filters/Options_Page.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
namespace Postmatic\Commentium\Unit_Tests\Filters;

use Postmatic\Commentium\Filters;

use \WP_UnitTestCase;

class Options_Page extends WP_UnitTestCase {

public function test_tabs() {
$tabs = array(
new \Prompt_Admin_Comment_Options_Tab( \Prompt_Core::$options ),
);

$tabs = Filters\Options_Page::tabs( $tabs );

$this->assertCount( 1, $tabs, 'Expected no tabs to be added.' );
$this->assertInstanceOf(
'Prompt_Admin_Comment_Options_Tab',
$tabs[0],
'Expected the original comments tab.'
);
}

public function test_premium_replace_tabs() {
$tabs = array(
new \Prompt_Admin_Comment_Options_Tab( \Prompt_Core::$options ),
);

\Prompt_Core::$options->set( 'enabled_message_types', array( 'post', 'digest' ) );
\Prompt_Core::$options->set( 'email_transport', 'api' );

$tabs = Filters\Options_Page::tabs( $tabs );

$this->assertCount( 1, $tabs, 'Expected no tabs to be added.' );

$this->assertInstanceOf(
'Postmatic\Commentium\Admin\Options_Tabs\Comments',
$tabs[0],
'Expected the original comments tab to be replaced.'
);
}
}

0 comments on commit db046ce

Please sign in to comment.