Skip to content

Commit

Permalink
Add more recommended WP phpcs rules
Browse files Browse the repository at this point in the history
  • Loading branch information
abrain committed Nov 2, 2024
1 parent b328bc3 commit bcfd04e
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 41 deletions.
34 changes: 34 additions & 0 deletions .phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -124,4 +124,38 @@

<rule ref="WordPress.Security.SafeRedirect"/>
<rule ref="WordPress.Security.PluginMenuSlug"/>

<rule ref="WordPress.WP.DeprecatedFunctions"/>
<rule ref="WordPress.WP.DeprecatedClasses"/>
<rule ref="WordPress.WP.DeprecatedParameters"/>
<rule ref="WordPress.WP.DeprecatedParameterValues"/>
<rule ref="WordPress.WP.AlternativeFunctions"/>
<rule ref="WordPress.WP.DiscouragedConstants"/>
<rule ref="WordPress.WP.DiscouragedFunctions"/>
<rule ref="WordPress.WP.EnqueuedResourceParameters"/>
<rule ref="WordPress.WP.CronInterval"/>
<rule ref="WordPress.WP.PostsPerPage"/>
<rule ref="WordPress.WP.EnqueuedResources"/>
<rule ref="WordPress.WP.GlobalVariablesOverride">
<exclude-pattern>tests/</exclude-pattern>
</rule>
<rule ref="WordPress.WP.Capabilities">
<properties>
<property name="custom_capabilities" type="array">
<element value="edit_einsatzbericht"/>
<element value="read_einsatzbericht"/>
<element value="delete_einsatzbericht"/>
<element value="edit_einsatzberichte"/>
<element value="edit_others_einsatzberichte"/>
<element value="publish_einsatzberichte"/>
<element value="read_private_einsatzberichte"/>
<element value="delete_einsatzberichte"/>
<element value="delete_private_einsatzberichte"/>
<element value="delete_published_einsatzberichte"/>
<element value="delete_others_einsatzberichte"/>
<element value="edit_private_einsatzberichte"/>
<element value="edit_published_einsatzbericht"/>
</property>
</properties>
</rule>
</ruleset>
11 changes: 7 additions & 4 deletions src/includes/Admin/Initializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,8 @@ public function enqueueEditScripts($hook)
'einsatzverwaltung-edit-script',
Core::$scriptUrl . 'einsatzverwaltung-edit.js',
array('jquery', 'jquery-ui-autocomplete', 'wp-i18n'),
Core::VERSION
Core::VERSION,
true
);
wp_localize_script(
'einsatzverwaltung-edit-script',
Expand All @@ -126,7 +127,8 @@ public function enqueueEditScripts($hook)
'einsatzverwaltung-settings-script',
Core::$scriptUrl . 'einsatzverwaltung-settings.js',
array('jquery-ui-draggable', 'jquery-ui-droppable', 'jquery-ui-sortable'),
Core::VERSION
Core::VERSION,
true
);
} elseif ('edit.php' == $hook) {
$screen = get_current_screen();
Expand All @@ -135,7 +137,7 @@ public function enqueueEditScripts($hook)
'einsatzverwaltung-report-list-table',
Core::$scriptUrl . 'report-list-table.js',
false,
null,
Core::VERSION,
true
);
}
Expand Down Expand Up @@ -169,7 +171,8 @@ public function enqueueEditScripts($hook)
'einsatzverwaltung-admin-script',
Core::$scriptUrl . 'einsatzverwaltung-admin.js',
array('wp-color-picker'),
Core::VERSION
Core::VERSION,
true
);
wp_enqueue_style('wp-color-picker');
}
Expand Down
3 changes: 2 additions & 1 deletion src/includes/Export/Page.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ public function enqueueAdminScripts($hook)
'einsatzverwaltung-export',
Core::$scriptUrl . 'export.js',
array('jquery'),
Core::VERSION
Core::VERSION,
true
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/includes/Frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function enqueueStyleAndScripts()
Core::VERSION
);
wp_add_inline_style('einsatzverwaltung-frontend', ReportListRenderer::getDynamicCss());
wp_enqueue_script('einsatzverwaltung-reportlist', Core::$scriptUrl . 'reportlist.js');
wp_enqueue_script('einsatzverwaltung-reportlist', Core::$scriptUrl . 'reportlist.js', [], Core::VERSION, true);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/includes/Settings/MainPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
use function get_permalink;
use function get_post_type_archive_link;
use function home_url;
use function parse_url;
use function str_replace;
use function strpos;
use function wp_parse_url;
use const PHP_URL_PATH;

/**
Expand Down Expand Up @@ -165,7 +165,7 @@ private function getConflictingPage(): ?WP_Post
if (strpos($reportArchiveUrl, $homeUrl) === 0) {
$reportArchivePath = str_replace($homeUrl, '', $reportArchiveUrl);
} else {
$reportArchivePath = parse_url($reportArchiveUrl, PHP_URL_PATH);
$reportArchivePath = wp_parse_url($reportArchiveUrl, PHP_URL_PATH);
}

return get_page_by_path($reportArchivePath);
Expand Down
2 changes: 1 addition & 1 deletion src/includes/Types/IncidentType.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function registerHooks()
// Enqueue the scripts to handle media upload and selection
if ($screen->taxonomy === self::getSlug() && in_array($screen->base, array('edit-tags', 'term'))) {
wp_enqueue_media();
wp_enqueue_script('einsatzverwaltung-media-selector', Core::$scriptUrl . 'media-selector.js');
wp_enqueue_script('einsatzverwaltung-media-selector', Core::$scriptUrl . 'media-selector.js', [], Core::VERSION, true);
}
});

Expand Down
23 changes: 0 additions & 23 deletions src/includes/Types/Vehicle.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ public function registerHooks()
{
$taxonomySlug = self::getSlug();
add_action("{$taxonomySlug}_pre_add_form", array($this, 'deprectatedHierarchyNotice'));
add_action('admin_menu', array($this, 'addBadgeToMenu'));

/**
* Prevent the Gutenberg Editor from creating a UI for this taxonomy, so we can use our own
Expand Down Expand Up @@ -205,28 +204,6 @@ public function deprectatedHierarchyNotice()
}
}

public function addBadgeToMenu()
{
global $submenu;
$termsWithParentCount = $this->getTermsWithParentCount();
if ($termsWithParentCount > 0) {
$submenuKey = 'edit.php?post_type=' . Report::getSlug();
if (array_key_exists($submenuKey, $submenu)) {
$vehicleEntry = array_filter($submenu[$submenuKey], function ($entry) {
return $entry[2] === 'edit-tags.php?taxonomy=fahrzeug&amp;post_type=einsatz';
});

foreach ($vehicleEntry as $id => $entry) {
$entry[0] .= sprintf(
' <span class="awaiting-mod"><span class="pending-count">%d</span></span>',
esc_html($termsWithParentCount)
);
$submenu[$submenuKey][$id] = $entry;
}
}
}
}

/**
* @return int Returns the number of terms in this taxonomy that have a parent term.
*/
Expand Down
6 changes: 3 additions & 3 deletions src/includes/Widgets/RecentIncidents.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
use function get_queried_object_id;
use function get_taxonomy;
use function printf;
use function strip_tags;
use function trim;
use function wp_strip_all_tags;

/**
* WordPress-Widget für die letzten X Einsätze
Expand Down Expand Up @@ -116,7 +116,7 @@ private function echoReports(array $instance, string $title)

// Add a nav element for accessibility
if (current_theme_supports('html5', 'navigation-widgets')) {
$title = trim(strip_tags($title));
$title = trim(wp_strip_all_tags($title));
$ariaLabel = !empty($title) ? $title : $this->defaultTitle;
printf('<nav role="navigation" aria-label="%s">', esc_attr($ariaLabel));
}
Expand Down Expand Up @@ -202,7 +202,7 @@ private function echoSingleReport(IncidentReport $report, $instance)
public function update($newInstance, $oldInstance): array
{
$instance = array();
$instance['title'] = strip_tags($newInstance['title']);
$instance['title'] = wp_strip_all_tags($newInstance['title']);

$anzahl = $newInstance['anzahl'];
if (empty($anzahl) || !is_numeric($anzahl) || $anzahl < 1) {
Expand Down
6 changes: 3 additions & 3 deletions src/includes/Widgets/RecentIncidentsFormatted.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
use function esc_html_e;
use function get_taxonomy;
use function printf;
use function strip_tags;
use function trim;
use function wp_strip_all_tags;

/**
* Widget für die neuesten Einsätze, das Aussehen wird vom Benutzer per HTML-Templates bestimmt
Expand Down Expand Up @@ -203,7 +203,7 @@ public function widget($args, $instance)
// Add a nav element for accessibility, if the widget contains links
$wrapInNav = current_theme_supports('html5', 'navigation-widgets') && strpos($settings['pattern'], '%url%') !== false;
if ($wrapInNav) {
$filteredTitle = trim(strip_tags($filteredTitle));
$filteredTitle = trim(wp_strip_all_tags($filteredTitle));
$ariaLabel = !empty($filteredTitle) ? $filteredTitle : $this->defaultTitle;
printf('<nav role="navigation" aria-label="%s">', esc_attr($ariaLabel));
}
Expand Down Expand Up @@ -238,7 +238,7 @@ public function widget($args, $instance)
public function update($newInstance, $oldInstance): array
{
$instance = array();
$instance['title'] = strip_tags($newInstance['title']);
$instance['title'] = wp_strip_all_tags($newInstance['title']);
$instance['numIncidents'] = absint($newInstance['numIncidents']);
if ($instance['numIncidents'] === 0) {
$instance['numIncidents'] = $this->defaults['numIncidents'];
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/Admin/InitializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function testEnqueuePostEditScripts()
});
when('wp_create_nonce')->justReturn('a-great-nonce');

expect('wp_enqueue_script')->once()->with('einsatzverwaltung-admin-script', Mockery::type('string'), Mockery::type('array'), Mockery::type('string'));
expect('wp_enqueue_script')->once()->with('einsatzverwaltung-admin-script', Mockery::type('string'), Mockery::type('array'), Mockery::type('string'), true);
expect('wp_enqueue_style')->atLeast()->once()->withAnyArgs();
expect('wp_localize_script')->atLeast()->once()->withAnyArgs();
expect('wp_set_script_translations')->atLeast()->once()->withAnyArgs();
Expand All @@ -66,7 +66,7 @@ public function testEnqueuePostEditScripts()
*/
public function testEnqueueReportListTableScripts()
{
expect('wp_enqueue_script')->once()->with('einsatzverwaltung-report-list-table', Mockery::type('string'), false, null, true);
expect('wp_enqueue_script')->once()->with('einsatzverwaltung-report-list-table', Mockery::type('string'), false, Mockery::type('string'), true);
expect('wp_enqueue_style')->atLeast()->once()->withAnyArgs();

$screen = Mockery::mock('\WP_Screen');
Expand Down Expand Up @@ -94,7 +94,7 @@ public function testDoNotEnqueueReportListTableScriptsForOtherPostTypes()

public function testEnqueueSettingsScripts()
{
expect('wp_enqueue_script')->once()->with('einsatzverwaltung-settings-script', Mockery::type('string'), Mockery::type('array'), Mockery::type('string'));
expect('wp_enqueue_script')->once()->with('einsatzverwaltung-settings-script', Mockery::type('string'), Mockery::type('array'), Mockery::type('string'), true);
expect('wp_enqueue_style')->atLeast()->once()->withAnyArgs();

$this->initializer->enqueueEditScripts('settings_page_einsatzvw-settings');
Expand Down

0 comments on commit bcfd04e

Please sign in to comment.