Skip to content

Commit

Permalink
Add sniff to check for add_..._page() functions + unit tests.
Browse files Browse the repository at this point in the history
Closes #11.
  • Loading branch information
jrfnl committed Jul 12, 2016
1 parent b8a5e09 commit 44c37b2
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 1 deletion.
2 changes: 1 addition & 1 deletion WordPress-Theme/ruleset.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<!-- For more information: https://make.wordpress.org/themes/handbook/review/ -->
<description>Standards any Theme to be published on wordpress.org should comply with.</description>


<rule ref="WordPress-Theme"/>


</ruleset>
55 changes: 55 additions & 0 deletions WordPress/Sniffs/Theme/NoAddAdminPagesSniff.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/**
* WordPress_Sniffs_Theme_NoAddAdminPagesSniff.
*
* Forbids the use of add_..._page() functions within Themes with the exception of `add_theme_page()`.
*
* @category Theme
* @package PHP_CodeSniffer
* @author Juliette Reinders Folmer <[email protected]>
*/
class WordPress_Sniffs_Theme_NoAddAdminPagesSniff extends WordPress_Sniffs_Functions_FunctionRestrictionsSniff {

/**
* Groups of functions to restrict
*
* Example: groups => array(
* 'lambda' => array(
* 'type' => 'error' | 'warning',
* 'message' => 'Use anonymous functions instead please!',
* 'functions' => array( 'eval', 'create_function' ),
* )
* )
*
* @return array
*/
public function getGroups() {
return array(

'add_menu_pages' => array(
'type' => 'error',
'message' => 'Themes should use <strong>add_theme_page()</strong> for adding admin pages. Found %s.',
'functions' => array(
// Menu Pages.
'add_menu_page',
'add_object_page',
'add_utility_page',

// SubMenu Pages.
'add_submenu_page',

// WordPress Administration Menus.
'add_dashboard_page',
'add_posts_page',
'add_media_page',
'add_pages_page',
'add_comments_page',
'add_plugins_page',
'add_users_page',
'add_management_page',
'add_options_page',
),
),
);

}
} // end class
28 changes: 28 additions & 0 deletions WordPress/Tests/Theme/NoAddAdminPagesUnitTest.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

// Menu Pages.
add_menu_page();
add_object_page();
add_utility_page();

// SubMenu Pages.
add_submenu_page();

// WordPress Administration Menus.
add_dashboard_page();
add_posts_page();
add_media_page();
add_pages_page();
add_comments_page();
add_plugins_page();
add_users_page();
add_management_page();
add_options_page();

// Add Theme Page is allowed.
add_theme_page(); // Ok.

// Method names within a Theme class should be fine.
Theme_Object::add_menu_page(); // Ok.
$this->add_plugins_page(); // Ok.
$theme_object->add_options_page(); // Ok.
55 changes: 55 additions & 0 deletions WordPress/Tests/Theme/NoAddAdminPagesUnitTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Unit test class for the NoAddAdminPages sniff.
*
* A sniff unit test checks a .inc file for expected violations of a single
* coding standard. Expected errors and warnings are stored in this class.
*
* @category Theme
* @package PHP_CodeSniffer
* @author Juliette Reinders Folmer <[email protected]>
*/
class WordPress_Tests_Theme_NoAddAdminPagesUnitTest extends AbstractSniffUnitTest {

/**
* Returns the lines where errors should occur.
*
* The key of the array should represent the line number and the value
* should represent the number of errors that should occur on that line.
*
* @return array(int => int)
*/
public function getErrorList() {
return array(
4 => 1,
5 => 1,
6 => 1,
9 => 1,
12 => 1,
13 => 1,
14 => 1,
15 => 1,
16 => 1,
17 => 1,
18 => 1,
19 => 1,
20 => 1,
);

} // end getErrorList()


/**
* Returns the lines where warnings should occur.
*
* The key of the array should represent the line number and the value
* should represent the number of warnings that should occur on that line.
*
* @return array(int => int)
*/
public function getWarningList() {
return array();

} // end getWarningList()

} // end class

0 comments on commit 44c37b2

Please sign in to comment.