From e968a6ccaa46f746fdcacef6eae1224dd0dacdf8 Mon Sep 17 00:00:00 2001 From: Henrique Mouta Date: Tue, 6 Jul 2021 16:32:44 +0100 Subject: [PATCH 1/3] Initial work on PHPCS cleanup --- .phpcs.xml.dist | 6 +- includes/class-syndication-admin-notices.php | 134 +++-- includes/class-syndication-client-factory.php | 63 ++- includes/class-syndication-event-counter.php | 21 +- includes/class-syndication-logger-viewer.php | 245 +++++---- includes/class-syndication-logger.php | 307 ++++++----- .../class-syndication-site-auto-retry.php | 92 ++-- ...class-syndication-site-failure-monitor.php | 16 +- includes/class-syndication-wp-rest-client.php | 329 ++++++----- includes/class-syndication-wp-rss-client.php | 265 ++++----- includes/class-syndication-wp-xml-client.php | 364 ++++++++----- .../class-syndication-wp-xmlrpc-client.php | 514 ++++++++++-------- ...lass-walker-category-dropdown-multiple.php | 31 +- includes/class-wp-cli.php | 206 ++++--- includes/interface-syndication-client.php | 61 ++- push-syndication.php | 17 +- 16 files changed, 1550 insertions(+), 1121 deletions(-) diff --git a/.phpcs.xml.dist b/.phpcs.xml.dist index 301811c7..0b3aad08 100644 --- a/.phpcs.xml.dist +++ b/.phpcs.xml.dist @@ -48,7 +48,9 @@ - + + + @@ -56,7 +58,7 @@ - + diff --git a/includes/class-syndication-admin-notices.php b/includes/class-syndication-admin-notices.php index 06103407..0925a011 100644 --- a/includes/class-syndication-admin-notices.php +++ b/includes/class-syndication-admin-notices.php @@ -5,11 +5,14 @@ */ class Syndication_Logger_Admin_Notice { - private static $notice_option = 'syn_notices'; + private static $notice_option = 'syn_notices'; private static $notice_bundles_option = 'syn_notice_bundles'; private static $dismiss_parameter = 'syn_dismiss'; + /** + * Syndication_Logger_Admin_Notice constructor. + */ public function __construct() { add_action( 'admin_init', array( __CLASS__, 'handle_dismiss_syndication_notice' ) ); add_action( 'admin_notices', array( __CLASS__, 'display_valid_notices' ) ); @@ -17,24 +20,25 @@ public function __construct() { /** * Create a admin notice - * @param text $message_text The message you would like to show - * @param string $message_type A message type, shown alongside with your message and used to categorize and bundle messages of the same type - * @param string $class css class applied to the notice eg. 'updated', 'error', 'update-nag' - * @param boolean $summarize_multiple setting this to true allows summarizing messages of the same message_type into one message. The message text is then passed through the syn_message_text_multiple filter and all messages of this type can be dismissed at once + * + * @param string $message_text The message you would like to show. + * @param string $message_type A message type, shown alongside with your message and used to categorize and bundle messages of the same type. + * @param string $class css class applied to the notice eg. 'updated', 'error', 'update-nag'. + * @param boolean $summarize_multiple setting this to true allows summarizing messages of the same message_type into one message. The message text is then passed through the syn_message_text_multiple filter and all messages of this type can be dismissed at once. */ public static function add_notice( $message_text, $message_type = 'Syndication', $class = 'updated', $summarize_multiple = false ) { $notices = get_option( self::$notice_option ); - $changed = false; + $changed = false; $message_key = md5( $message_type . $message_text ); - if ( ! is_array( $notices ) || ! isset( $notices[$message_type] ) || ! isset( $notices[$message_type][$message_key] ) ) { - $notices[$message_type][$message_key] = array( - 'message_text' => sanitize_text_field( $message_text ), - 'summarize_multiple' => (boolean) $summarize_multiple, - 'message_type' => sanitize_text_field( $message_type ), - 'class' => sanitize_text_field( $class ), + if ( ! is_array( $notices ) || ! isset( $notices[ $message_type ] ) || ! isset( $notices[ $message_type ][ $message_key ] ) ) { + $notices[ $message_type ][ $message_key ] = array( + 'message_text' => sanitize_text_field( $message_text ), + 'summarize_multiple' => (bool) $summarize_multiple, + 'message_type' => sanitize_text_field( $message_type ), + 'class' => sanitize_text_field( $class ), ); - $changed = true; + $changed = true; } if ( true === $changed ) { @@ -50,40 +54,40 @@ public static function add_notice( $message_text, $message_type = 'Syndication', public static function display_valid_notices() { $capability = apply_filters( 'syn_syndicate_cap', 'manage_options' ); - $messages = get_option( self::$notice_option ); - $notice_bundles = get_option( self::$notice_bundles_option ); - $messages_to_display = array(); + $messages = get_option( self::$notice_option ); + $notice_bundles = get_option( self::$notice_bundles_option ); + $messages_to_display = array(); $notice_bundles_changed = false; - if ( !is_array( $messages ) || empty( $messages ) ) { + if ( ! is_array( $messages ) || empty( $messages ) ) { return; } - foreach( $messages as $message_type => $message_values ) { - foreach( $message_values as $message_key => $message_data ) { + foreach ( $messages as $message_type => $message_values ) { + foreach ( $message_values as $message_key => $message_data ) { if ( isset( $message_data['summarize_multiple'] ) && true === $message_data['summarize_multiple'] ) { $message_text = apply_filters( 'syn_message_text_multiple', $message_data['message_text'], $message_data ); } else { $message_text = apply_filters( 'syn_message_text', $message_data['message_text'], $message_data ); } - $new_message_key = md5( $message_type . $message_text ); + $new_message_key = md5( $message_type . $message_text ); $new_message_data = array( - 'message_text' => sanitize_text_field( $message_text ), - 'summarize_multiple' => (boolean) $message_data['summarize_multiple'], - 'message_type' => sanitize_text_field( $message_data['message_type'] ), - 'class' => sanitize_text_field( $message_data['class'] ) + 'message_text' => sanitize_text_field( $message_text ), + 'summarize_multiple' => (bool) $message_data['summarize_multiple'], + 'message_type' => sanitize_text_field( $message_data['message_type'] ), + 'class' => sanitize_text_field( $message_data['class'] ), ); if ( $new_message_key != $message_key ) { - if ( ! isset( $notice_bundles[$new_message_key] ) || ! in_array( $message_key, $notice_bundles[$new_message_key] ) ) { - $notice_bundles[$new_message_key][] = $message_key; - $notice_bundles_changed = true; + if ( ! isset( $notice_bundles[ $new_message_key ] ) || ! in_array( $message_key, $notice_bundles[ $new_message_key ] ) ) { + $notice_bundles[ $new_message_key ][] = $message_key; + $notice_bundles_changed = true; } } if ( current_user_can( $capability ) ) { - $messages_to_display[$message_type][$new_message_key] = $new_message_data; + $messages_to_display[ $message_type ][ $new_message_key ] = $new_message_data; } } } @@ -92,11 +96,23 @@ public static function display_valid_notices() { update_option( self::$notice_bundles_option, $notice_bundles ); } - foreach( $messages_to_display as $message_type => $message_values ) { - foreach( $message_values as $message_key => $message_data ) { + foreach ( $messages_to_display as $message_type => $message_values ) { + foreach ( $message_values as $message_key => $message_data ) { $dismiss_nonce = wp_create_nonce( esc_attr( $message_key ) ); printf( '

', esc_attr( $message_data['class'] ) ); - printf( __('%1$s : %2$s Hide Notice'), esc_html( $message_type ), wp_kses_post( $message_data['message_text'] ), add_query_arg( array( self::$dismiss_parameter => esc_attr( $message_key ), 'syn_dismiss_nonce' => esc_attr( $dismiss_nonce ) ) ) ); + printf( + __( '%1$s : %2$s Hide Notice' ), // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped + esc_html( $message_type ), + wp_kses_post( $message_data['message_text'] ), + esc_url( + add_query_arg( + array( + self::$dismiss_parameter => esc_attr( $message_key ), + 'syn_dismiss_nonce' => esc_attr( $dismiss_nonce ), + ) + ) + ) + ); printf( '

' ); } } @@ -108,49 +124,63 @@ public static function display_valid_notices() { public static function handle_dismiss_syndication_notice() { $capability = apply_filters( 'syn_syndicate_cap', 'manage_options' ); - // add nonce - if ( isset( $_GET[self::$dismiss_parameter] ) && current_user_can( $capability ) ) { - - $dismiss_key = esc_attr( $_GET[self::$dismiss_parameter] ); - $dismiss_nonce = esc_attr( $_GET['syn_dismiss_nonce'] ); + // add nonce. + if ( isset( $_GET[ self::$dismiss_parameter ] ) && current_user_can( $capability ) ) { + $dismiss_key = esc_attr( $_GET[ self::$dismiss_parameter ] ); // phpcs:ignore + $dismiss_nonce = esc_attr( isset( $_GET['syn_dismiss_nonce'] ) ? $_GET['syn_dismiss_nonce'] : '' ); // phpcs:ignore if ( ! wp_verify_nonce( $dismiss_nonce, $dismiss_key ) ) { - wp_die( __( "Invalid security check" ) ); + wp_die( esc_html__( 'Invalid security check' ) ); // phpcs:ignore WordPress.WP.I18n.MissingArgDomain } - $messages = get_option( self::$notice_option ); + $messages = get_option( self::$notice_option ); $notice_bundles = get_option( self::$notice_bundles_option ); $dismiss_items = array(); - if ( isset( $notice_bundles[$dismiss_key] ) ) { - $dismiss_items = $notice_bundles[$dismiss_key]; + if ( isset( $notice_bundles[ $dismiss_key ] ) ) { + $dismiss_items = $notice_bundles[ $dismiss_key ]; } else { $dismiss_items = array( $dismiss_key ); } - foreach( $messages as $message_type => $message_values ) { + foreach ( $messages as $message_type => $message_values ) { $message_keys = array_keys( $message_values ); - $dismiss_it = array_intersect( $message_keys, $dismiss_items ); - foreach( $dismiss_it as $dismiss_it_key ) { - unset( $messages[$message_type][$dismiss_it_key] ); + $dismiss_it = array_intersect( $message_keys, $dismiss_items ); + foreach ( $dismiss_it as $dismiss_it_key ) { + unset( $messages[ $message_type ][ $dismiss_it_key ] ); } } - if ( isset( $notice_bundles[$dismiss_key] ) ) { - unset( $notice_bundles[$dismiss_key] ); + if ( isset( $notice_bundles[ $dismiss_key ] ) ) { + unset( $notice_bundles[ $dismiss_key ] ); } update_option( self::$notice_option, $messages ); update_option( self::$notice_bundles_option, $notice_bundles ); - } } } -add_filter( 'syn_message_text_multiple', 'syn_handle_multiple_error_notices', 10, 2 ); -function syn_handle_multiple_error_notices( $message, $message_data ) { - return __( 'There have been multiple errors. Please validate your syndication logs' ); +/** + * @param $message + * @param $message_data + * + * @return string|void + */ +function syn_handle_multiple_error_notices( $message, $message_data ) { //phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals + return esc_html__( 'There have been multiple errors. Please validate your syndication logs', 'push-syndication' ); } +add_filter( 'syn_message_text_multiple', 'syn_handle_multiple_error_notices', 10, 2 ); -add_action( 'push_syndication_site_disabled', 'syn_add_site_disabled_notice', 10, 2 ); +/** + * @param $site_id + * @param $count + */ function syn_add_site_disabled_notice( $site_id, $count ) { - Syndication_Logger_Admin_Notice::add_notice( $message_text = sprintf( __( 'Site %d disabled after %d pull failure(s).', 'push-syndication' ), (int) $site_id, (int) $count ), $message_type = 'Syndication site disabled', $class = 'error', $summarize_multiple = false ); + Syndication_Logger_Admin_Notice::add_notice( + sprintf( __( 'Site %1$d disabled after %2$d pull failure(s).', 'push-syndication' ), (int) $site_id, (int) $count ), + 'Syndication site disabled', + 'error', + false + ); } +add_action( 'push_syndication_site_disabled', 'syn_add_site_disabled_notice', 10, 2 ); + diff --git a/includes/class-syndication-client-factory.php b/includes/class-syndication-client-factory.php index f2e755ba..51d9aa40 100644 --- a/includes/class-syndication-client-factory.php +++ b/includes/class-syndication-client-factory.php @@ -1,45 +1,68 @@ _get_safe_option_name( $event_slug, $event_object_id ); - $count = get_option( $option_name, 0 ); - $count = $count + 1; + $option_name = $this->get_safe_option_name( $event_slug, $event_object_id ); + $count = get_option( $option_name, 0 ); + $count = ++$count; update_option( $option_name, $count ); /** @@ -61,10 +62,10 @@ public function count_event( $event_slug, $event_object_id = null ) { */ public function reset_event( $event_slug, $event_object_id ) { // Coerce the slug and ID to strings. PHP will fire appropriate warnings if the given slug and ID are not coercible. - $event_slug = (string) $event_slug; + $event_slug = (string) $event_slug; $event_object_id = (string) $event_object_id; - delete_option( $this->_get_safe_option_name( $event_slug, $event_object_id ) ); + delete_option( $this->get_safe_option_name( $event_slug, $event_object_id ) ); } /** @@ -76,7 +77,7 @@ public function reset_event( $event_slug, $event_object_id ) { * @param $event_object_id * @return string */ - protected function _get_safe_option_name( $event_slug, $event_object_id ) { + protected function get_safe_option_name( $event_slug, $event_object_id ) { return 'push_syndication_event_counter_' . md5( $event_slug . $event_object_id ); } -} \ No newline at end of file +} diff --git a/includes/class-syndication-logger-viewer.php b/includes/class-syndication-logger-viewer.php index 240c7d48..ae150ef3 100644 --- a/includes/class-syndication-logger-viewer.php +++ b/includes/class-syndication-logger-viewer.php @@ -1,9 +1,14 @@ __( 'log', 'push-syndication' ), - 'plural' => __( 'logs', 'push-syndication' ), - 'ajax' => false - ) ); + parent::__construct( + array( + 'singular' => __( 'log', 'push-syndication' ), + 'plural' => __( 'logs', 'push-syndication' ), + 'ajax' => false, + ) + ); add_action( 'admin_head', array( $this, 'admin_header' ) ); } public function admin_header() { + // phpcs:ignore WordPress.Security.NonceVerification.Recommended $current_page = ( isset( $_GET['page'] ) ) ? (int) $_GET['page'] : false; - if( 'syndication_dashboard' != $current_page ) + if ( 'syndication_dashboard' != $current_page ) { return; + } ?>