Skip to content

Commit

Permalink
Merge pull request #23 from ernilambar/refine/files
Browse files Browse the repository at this point in the history
Refine/files
  • Loading branch information
ernilambar authored Jul 6, 2024
2 parents b7ffbbc + 152a7fe commit 45cf26b
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 53 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/code-quality.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: PHP Code Linting

on:
pull_request:
push:
branches:
- main
- master

jobs:
php-lint:
name: PHP Lint
runs-on: ubuntu-latest
timeout-minutes: 20
steps:
- uses: actions/checkout@v4

- uses: shivammathur/setup-php@v2
with:
php-version: '8.2'

- name: Validate Composer configuration
run: composer validate

- name: Install PHP dependencies
uses: ramsey/composer-install@83af392bf5f031813d25e6fe4cd626cdba9a2df6
with:
composer-options: '--prefer-dist --no-progress --no-interaction'

- name: Run tests
run: composer run-script phpcs
26 changes: 26 additions & 0 deletions .phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,32 @@
</properties>
</rule>

<!-- Disallows grouped use declarations. -->
<rule ref="SlevomatCodingStandard.Namespaces.DisallowGroupUse" />
<!-- Disallows leading backslash in use statement. -->
<rule ref="SlevomatCodingStandard.Namespaces.UseDoesNotStartWithBackslash" />
<!-- Checks whether uses at the top of a file are alphabetically sorted. -->
<rule ref="SlevomatCodingStandard.Namespaces.AlphabeticallySortedUses" />
<!-- Prohibits uses from the same namespace. -->
<rule ref="SlevomatCodingStandard.Namespaces.UseFromSameNamespace" />
<!-- Looks for unused imports from other namespaces. -->
<rule ref="SlevomatCodingStandard.Namespaces.UnusedUses">
<properties>
<property name="searchAnnotations" value="true" />
</properties>
</rule>
<!-- All references to functions, classes and constants should import using a use statement. -->
<rule ref="SlevomatCodingStandard.Namespaces.ReferenceUsedNamesOnly">
<properties>
<property name="allowFullyQualifiedGlobalFunctions" value="true" />
<property name="allowFullyQualifiedGlobalClasses" value="true" />
<property name="allowFullyQualifiedGlobalConstants" value="true" />
<property name="allowFallbackGlobalFunctions" value="true" />
<property name="allowFallbackGlobalConstants" value="true" />
<property name="allowFullyQualifiedNameForCollidingClasses" value="true" />
</properties>
</rule>

<!-- Loads the PHP Compatibility ruleset. -->
<rule ref="PHPCompatibilityWP" />

Expand Down
1 change: 1 addition & 0 deletions devtools/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"require-dev": {
"dealerdirect/phpcodesniffer-composer-installer": "^1.0",
"phpcompatibility/phpcompatibility-wp": "^2.1",
"slevomat/coding-standard": "^8.15",
"wp-cli/i18n-command": "^2.6",
"wp-coding-standards/wpcs": "^3.1"
},
Expand Down
85 changes: 50 additions & 35 deletions includes/classes/class-ns-featured-posts-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* @package NS_Featured_Posts
*/

use Nilambar\AdminNotice\Notice;
use Nilambar\Optioner\Optioner;

/**
Expand Down Expand Up @@ -73,8 +74,6 @@ private function __construct() {

$this->options = $plugin->get_options();



// Add an action link pointing to the options page.
$base_file = $this->plugin_slug . '/' . $this->plugin_slug . '.php';
add_filter( 'plugin_action_links_' . $base_file, array( $this, 'add_plugin_action_links' ) );
Expand Down Expand Up @@ -107,9 +106,13 @@ private function __construct() {
add_action( 'wp_ajax_nsfp_nsbl_get_posts', array( $this, 'get_posts_ajax_callback' ) );
}

/**
* Setup admin notice.
*
* @since 2.0.10
*/
public function setup_custom_notice() {
// Setup notice.
\Nilambar\AdminNotice\Notice::init(
Notice::init(
array(
'slug' => $this->plugin_slug,
'name' => esc_html__( 'NS Featured Posts', 'ns-featured-posts' ),
Expand Down Expand Up @@ -366,31 +369,31 @@ public function ajax_handler_featured_toggle() {
);

// Nonce check.
$nonce = isset( $_POST['nonce'] ) ? $_POST['nonce'] : null; // phpcs:ignore WordPress.Security.NonceVerification
$nonce = isset( $_POST['nonce'] ) ? sanitize_text_field( wp_unslash( $_POST['nonce'] ) ) : null; // phpcs:ignore WordPress.Security.NonceVerification

if ( ! wp_verify_nonce( $nonce, 'ajax-nonce' ) ) {
$output['message'] = esc_html__( 'Nonce verification failed.', 'ns-featured-posts' );

wp_send_json( $output );
}

$uno = isset( $_POST['uno'] ) ? rest_sanitize_boolean( $_POST['uno'] ) : false;
$uno = isset( $_POST['uno'] ) ? rest_sanitize_boolean( sanitize_text_field( wp_unslash( $_POST['uno'] ) ) ) : false;

$max_posts = isset( $_POST['max_posts'] ) ? absint( $_POST['max_posts'] ) : 0;
$max_status = isset( $_POST['max_status'] ) ? rest_sanitize_boolean( $_POST['max_status'] ) : false;
$max_posts = isset( $_POST['max_posts'] ) ? absint( sanitize_text_field( wp_unslash( $_POST['max_posts'] ) ) ) : 0;
$max_status = isset( $_POST['max_status'] ) ? rest_sanitize_boolean( sanitize_text_field( wp_unslash( $_POST['max_status'] ) ) ) : false;

$ns_featured = isset( $_POST['ns_featured'] ) ? $_POST['ns_featured'] : null;
$ns_featured = isset( $_POST['ns_featured'] ) ? sanitize_text_field( wp_unslash( $_POST['ns_featured'] ) ) : null;

$post_id = 0;

if ( isset( $_POST['post_id'] ) ) {
$post_id = (int) $_POST['post_id'];
$post_id = (int) sanitize_text_field( wp_unslash( $_POST['post_id'] ) );
}

$post_type = null;

if ( isset( $_POST['post_type'] ) ) {
$post_type = (string) $_POST['post_type'];
$post_type = (string) sanitize_text_field( wp_unslash( $_POST['post_type'] ) );
}

if ( ! empty( $post_id ) && ! empty( $post_type ) && null !== $ns_featured ) {
Expand Down Expand Up @@ -476,8 +479,8 @@ private function get_other_posts( $post_id, $post_type ) {
$qargs = array(
'posts_per_page' => -1,
'post__not_in' => array( $post_id ),

Check warning on line 481 in includes/classes/class-ns-featured-posts-admin.php

View workflow job for this annotation

GitHub Actions / PCP

WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_post__not_in

Using exclusionary parameters, like post__not_in, in calls to get_posts() should be done with caution, see https://wpvip.com/documentation/performance-improvements-by-removing-usage-of-post__not_in/ for more information.
'meta_key' => '_is_ns_featured_post',
'meta_value' => 'yes',
'meta_key' => '_is_ns_featured_post', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
'meta_value' => 'yes', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
'post_type' => $post_type,
'post_status' => array( 'publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', 'trash' ),
);
Expand All @@ -491,6 +494,13 @@ private function get_other_posts( $post_id, $post_type ) {
return $output;
}

/**
* Load settings assets.
*
* @since 2.0.0
*
* @param string $hook Hook name.
*/
public function load_settings_assets( $hook ) {
if ( 'settings_page_ns-featured-posts' !== $hook ) {
return;
Expand Down Expand Up @@ -524,7 +534,6 @@ public function load_assets() {
);

wp_localize_script( 'nspf-admin', 'NSFP_OBJ', $localize_args );

}

/**
Expand Down Expand Up @@ -592,7 +601,7 @@ public function save_featured_meta_box( $post_id ) {
}

// If our nonce isn't there, or we can't verify it, bail.
if ( ! isset( $_POST['nsfp_featured_metabox_nonce'] ) || ! wp_verify_nonce( $_POST['nsfp_featured_metabox_nonce'], plugin_basename( __FILE__ ) ) ) {
if ( ! isset( $_POST['nsfp_featured_metabox_nonce'] ) || ! wp_verify_nonce( $_POST['nsfp_featured_metabox_nonce'], plugin_basename( __FILE__ ) ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput
return $post_id;
}

Expand Down Expand Up @@ -662,7 +671,7 @@ public function custom_table_filtering() {
$selected_now = '';

if ( isset( $_GET['filter-ns-featured-posts'] ) ) {

Check warning on line 673 in includes/classes/class-ns-featured-posts-admin.php

View workflow job for this annotation

GitHub Actions / PCP

WordPress.Security.NonceVerification.Recommended

Processing form data without nonce verification.
$selected_now = esc_attr( $_GET['filter-ns-featured-posts'] );
$selected_now = sanitize_text_field( wp_unslash( $_GET['filter-ns-featured-posts'] ) );

Check warning on line 674 in includes/classes/class-ns-featured-posts-admin.php

View workflow job for this annotation

GitHub Actions / PCP

WordPress.Security.NonceVerification.Recommended

Processing form data without nonce verification.
}

echo '<select name="filter-ns-featured-posts" id="filter-ns-featured-posts">';
Expand All @@ -685,13 +694,11 @@ public function custom_query_filtering( $query ) {
$qv = &$query->query_vars;

if ( is_admin() && 'edit.php' === $pagenow ) {

if ( ! isset( $qv['meta_query'] ) ) {
$qv['meta_query'] = array();
$qv['meta_query'] = array(); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
}

if ( ! empty( $_GET['filter-ns-featured-posts'] ) ) {

Check warning on line 701 in includes/classes/class-ns-featured-posts-admin.php

View workflow job for this annotation

GitHub Actions / PCP

WordPress.Security.NonceVerification.Recommended

Processing form data without nonce verification.

if ( 'yes' === $_GET['filter-ns-featured-posts'] ) {

Check warning on line 702 in includes/classes/class-ns-featured-posts-admin.php

View workflow job for this annotation

GitHub Actions / PCP

WordPress.Security.NonceVerification.Recommended

Processing form data without nonce verification.
$qv['meta_query'][] = array(
'key' => '_is_ns_featured_post',
Expand Down Expand Up @@ -726,10 +733,8 @@ public function custom_query_filtering( $query ) {
* Adding filtering link.
*
* @since 1.0.0
*
* @param WP_Query $wp_query Instance of WP_Query object.
*/
public function custom_filtering_query_for_listing( $wp_query ) {
public function custom_filtering_query_for_listing() {
if ( is_admin() ) {
$allowed = $this->get_allowed_post_types();

Expand All @@ -749,7 +754,7 @@ public function custom_filtering_query_for_listing( $wp_query ) {
* @param array $views Views.
*/
public function add_views_link( $views ) {
$post_type = ( ( isset( $_GET['post_type'] ) && '' !== $_GET['post_type'] ) ? $_GET['post_type'] : 'post' );
$post_type = ( ( isset( $_GET['post_type'] ) && '' !== $_GET['post_type'] ) ? sanitize_text_field( wp_unslash( $_GET['post_type'] ) ) : 'post' );

$count = $this->get_total_featured_count( $post_type );
$class = ( isset( $_GET['featured'] ) && 'yes' === $_GET['featured'] ) ? 'current' : '';
Expand Down Expand Up @@ -783,8 +788,8 @@ public function get_total_featured_count( $post_type ) {
$args = array(
'post_type' => $post_type,
'posts_per_page' => -1,
'meta_key' => '_is_ns_featured_post',
'meta_value' => 'yes',
'meta_key' => '_is_ns_featured_post', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key
'meta_value' => 'yes', // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value
'post_status' => array( 'publish', 'pending', 'draft', 'auto-draft', 'future', 'private', 'inherit', 'trash' ),
);

Expand All @@ -806,9 +811,11 @@ public function register_custom_widgets() {
* Render sidebar.
*
* @since 2.0.0
*
* @param Optioner $optioner_object Instance of Optioner.
*/
public function render_sidebar( $object ) {
$object->render_sidebar_box(
public function render_sidebar( $optioner_object ) {
$optioner_object->render_sidebar_box(
array(
'title' => 'Help &amp; Support',
'icon' => 'dashicons-editor-help',
Expand All @@ -817,15 +824,15 @@ public function render_sidebar( $object ) {
<h4>Wanna help make this plugin better?</h4>
<p><a href="https://wordpress.org/support/plugin/ns-featured-posts/reviews/#new-post" target="_blank">Review and rate this plugin on WordPress.org</a></p>',
),
$object
$optioner_object
);

$object->render_sidebar_box(
$optioner_object->render_sidebar_box(
array(
'title' => 'Recent Blog Posts',
'content' => '<div class="ns-blog-list"></div>',
),
$object
$optioner_object
);
}

Expand Down Expand Up @@ -870,17 +877,16 @@ public function show_admin_message() {
* @since 2.0.0
*
* @param array $attributes Attributes.
* @param bool $echo Whether to echo or not.
* @param bool $display Whether to echo or not.
*/
public function render_attr( $attributes, $echo = true ) {
public function render_attr( $attributes, $display = true ) {
if ( empty( $attributes ) ) {
return;
}

$html = '';

foreach ( $attributes as $name => $value ) {

$esc_value = '';

if ( 'class' === $name && is_array( $value ) ) {
Expand All @@ -889,21 +895,25 @@ public function render_attr( $attributes, $echo = true ) {

if ( false !== $value && 'href' === $name ) {
$esc_value = esc_url( $value );

} elseif ( false !== $value ) {
$esc_value = esc_attr( $value );
}

$html .= false !== $value ? sprintf( ' %s="%s"', esc_html( $name ), $esc_value ) : esc_html( " {$name}" );
}

if ( ! empty( $html ) && true === $echo ) {
if ( ! empty( $html ) && true === $display ) {
echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
} else {
return $html;
}
}

/**
* AJAX callback for feed items.
*
* @since 2.0.0
*/
public function get_posts_ajax_callback() {
$output = array();

Expand All @@ -920,6 +930,11 @@ public function get_posts_ajax_callback() {
}
}

/**
* Returns blog feed items.
*
* @since 2.0.0
*/
public function get_blog_feed_items() {
$output = array();

Expand Down
7 changes: 1 addition & 6 deletions includes/classes/class-ns-featured-posts.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,12 @@ public static function get_instance() {
public static function activate( $network_wide ) {

if ( function_exists( 'is_multisite' ) && is_multisite() ) {

if ( $network_wide ) {

// Get all blog ids.
$blog_ids = self::get_blog_ids();

foreach ( $blog_ids as $blog_id ) {

switch_to_blog( $blog_id );
self::single_activate();
}
Expand All @@ -153,14 +151,12 @@ public static function activate( $network_wide ) {
public static function deactivate( $network_wide ) {

if ( function_exists( 'is_multisite' ) && is_multisite() ) {

if ( $network_wide ) {

// Get all blog ids.
$blog_ids = self::get_blog_ids();

foreach ( $blog_ids as $blog_id ) {

switch_to_blog( $blog_id );
self::single_deactivate();
}
Expand Down Expand Up @@ -203,7 +199,7 @@ private static function get_blog_ids() {

$ids = array();

$output = $wpdb->get_results( "SELECT blog_id FROM $wpdb->blogs WHERE archived = '0' AND spam = '0' AND deleted = '0'", ARRAY_A );
$output = $wpdb->get_results( "SELECT blog_id FROM $wpdb->blogs WHERE archived = '0' AND spam = '0' AND deleted = '0'", ARRAY_A ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery

if ( $output ) {
$ids = wp_list_pluck( $output, 'blog_id' );
Expand Down Expand Up @@ -285,7 +281,6 @@ public function migrate_options() {

if ( $opt ) {
if ( isset( $opt['nsfp_posttypes'] ) && ! empty( $opt['nsfp_posttypes'] ) ) {

$values = array_keys( $opt['nsfp_posttypes'] );

$values = array_filter( $values );
Expand Down
Loading

0 comments on commit 45cf26b

Please sign in to comment.