From ca0b56983af66ac2b858521a33366226e5e64b6b Mon Sep 17 00:00:00 2001 From: ingeniumed Date: Wed, 26 Jun 2024 16:07:34 +1000 Subject: [PATCH] lint the story budget module --- modules/story-budget/story-budget.php | 330 ++++++++++++++------------ 1 file changed, 179 insertions(+), 151 deletions(-) diff --git a/modules/story-budget/story-budget.php b/modules/story-budget/story-budget.php index ee524097..43285cba 100644 --- a/modules/story-budget/story-budget.php +++ b/modules/story-budget/story-budget.php @@ -7,24 +7,27 @@ */ class EF_Story_Budget extends EF_Module { - var $taxonomy_used = 'category'; + public $taxonomy_used = 'category'; - var $module; + public $module; - var $num_columns = 0; + public $num_columns = 0; - var $max_num_columns; + public $max_num_columns; - var $no_matching_posts = true; + public $no_matching_posts = true; - var $terms = array(); + public $terms = array(); - var $user_filters; + public $user_filters; + // phpcs:ignore Generic.NamingConventions.UpperCaseConstantName.ClassConstantNotUpperCase const screen_id = 'dashboard_page_story-budget'; + // phpcs:ignore Generic.NamingConventions.UpperCaseConstantName.ClassConstantNotUpperCase const usermeta_key_prefix = 'ef_story_budget_'; + // phpcs:ignore Generic.NamingConventions.UpperCaseConstantName.ClassConstantNotUpperCase const default_num_columns = 1; private $term_columns; @@ -32,12 +35,13 @@ class EF_Story_Budget extends EF_Module { /** * Register the module with Edit Flow but don't do anything else */ - function __construct() { + public function __construct() { $this->module_url = $this->get_module_url( __FILE__ ); // Register the module with Edit Flow $args = array( 'title' => __( 'Story Budget', 'edit-flow' ), + // translators: %s is a link to the story budget page 'short_description' => sprintf( __( 'View the status of all your content at a glance.', 'edit-flow' ), admin_url( 'index.php?page=story-budget' ) ), 'extended_description' => __( 'Use the story budget to see how content on your site is progressing. Filter by specific categories or date ranges to see details about each post in progress.', 'edit-flow' ), 'module_url' => $this->module_url, @@ -50,17 +54,17 @@ function __construct() { 'autoload' => false, ); $this->module = EditFlow()->register_module( 'story_budget', $args ); - } /** * Initialize the rest of the stuff in the class if the module is active */ - function init() { + public function init() { $view_story_budget_cap = apply_filters( 'ef_view_story_budget_cap', 'ef_view_story_budget' ); - if ( !current_user_can( $view_story_budget_cap ) ) + if ( ! current_user_can( $view_story_budget_cap ) ) { return; + } $this->num_columns = $this->get_num_columns(); $this->max_num_columns = apply_filters( 'ef_story_budget_max_num_columns', 3 ); @@ -78,7 +82,6 @@ function init() { // Load necessary scripts and stylesheets add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) ); add_action( 'admin_enqueue_scripts', array( $this, 'action_enqueue_admin_styles' ) ); - } /** @@ -86,15 +89,15 @@ function init() { * * @since 0.7 */ - function install() { + public function install() { $story_budget_roles = array( 'administrator' => array( 'ef_view_story_budget' ), - 'editor' => array( 'ef_view_story_budget' ), - 'author' => array( 'ef_view_story_budget' ), - 'contributor' => array( 'ef_view_story_budget' ) + 'editor' => array( 'ef_view_story_budget' ), + 'author' => array( 'ef_view_story_budget' ), + 'contributor' => array( 'ef_view_story_budget' ), ); - foreach( $story_budget_roles as $role => $caps ) { + foreach ( $story_budget_roles as $role => $caps ) { $this->add_caps_to_role( $role, $caps ); } } @@ -104,23 +107,23 @@ function install() { * * @since 0.7 */ - function upgrade( $previous_version ) { + public function upgrade( $previous_version ) { global $edit_flow; // Upgrade path to v0.7 - if ( version_compare( $previous_version, '0.7' , '<' ) ) { + if ( version_compare( $previous_version, '0.7', '<' ) ) { // Migrate whether the story budget was enabled or not and clean up old option - if ( $enabled = get_option( 'edit_flow_story_budget_enabled' ) ) + if ( $enabled = get_option( 'edit_flow_story_budget_enabled' ) ) { $enabled = 'on'; - else + } else { $enabled = 'off'; + } $edit_flow->update_module_option( $this->module->name, 'enabled', $enabled ); delete_option( 'edit_flow_story_budget_enabled' ); // Technically we've run this code before so we don't want to auto-install new data $edit_flow->update_module_option( $this->module->name, 'loaded_once', true ); } - } /** @@ -128,8 +131,8 @@ function upgrade( $previous_version ) { * * @uses add_submenu_page() */ - function action_admin_menu() { - add_submenu_page( 'index.php', __('Story Budget', 'edit-flow'), __('Story Budget', 'edit-flow'), apply_filters( 'ef_view_story_budget_cap', 'ef_view_story_budget' ), $this->module->slug, array( $this, 'story_budget') ); + public function action_admin_menu() { + add_submenu_page( 'index.php', __( 'Story Budget', 'edit-flow' ), __( 'Story Budget', 'edit-flow' ), apply_filters( 'ef_view_story_budget_cap', 'ef_view_story_budget' ), $this->module->slug, array( $this, 'story_budget' ) ); } /** @@ -137,11 +140,12 @@ function action_admin_menu() { * * @uses enqueue_admin_script() */ - function enqueue_admin_scripts() { + public function enqueue_admin_scripts() { global $current_screen; - if ( $current_screen->id != self::screen_id ) + if ( self::screen_id != $current_screen->id ) { return; + } $num_columns = $this->get_num_columns(); echo ''; @@ -153,11 +157,12 @@ function enqueue_admin_scripts() { /** * Enqueue a screen and print stylesheet for the story budget. */ - function action_enqueue_admin_styles() { + public function action_enqueue_admin_styles() { global $current_screen; - if ( $current_screen->id != self::screen_id ) + if ( self::screen_id != $current_screen->id ) { return; + } wp_enqueue_style( 'edit_flow-story_budget-styles', $this->module_url . 'lib/story-budget.css', false, EDIT_FLOW_VERSION, 'screen' ); wp_enqueue_style( 'edit_flow-story_budget-print-styles', $this->module_url . 'lib/story-budget-print.css', false, EDIT_FLOW_VERSION, 'print' ); @@ -169,7 +174,7 @@ function action_enqueue_admin_styles() { * * @since 0.7 */ - function register_term_columns() { + public function register_term_columns() { $term_columns = array( 'title' => __( 'Title', 'edit-flow' ), @@ -188,17 +193,18 @@ function register_term_columns() { * * @since 0.7 */ - function handle_form_date_range_change() { + public function handle_form_date_range_change() { if ( ! isset( $_POST['ef-story-budget-range-submit'], $_POST['ef-story-budget-number-days'], $_POST['ef-story-budget-start-date_hidden'] ) ) { return; } - if ( !wp_verify_nonce( $_POST['nonce'], 'change-date' ) ) - wp_die( $this->module->messages['nonce-failed'] ); + if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( $_POST['nonce'], 'change-date' ) ) { + wp_die( esc_html( $this->module->messages['nonce-failed'] ) ); + } $current_user = wp_get_current_user(); - $new_filters = array ( + $new_filters = array( 'start_date' => $_POST['ef-story-budget-start-date_hidden'], 'number_days' => (int) $_POST['ef-story-budget-number-days'], ); @@ -245,7 +251,7 @@ public function update_user_filters_from_form_date_range_change( $current_user, /** * Get the number of columns to show on the story budget */ - function get_num_columns() { + public function get_num_columns() { if ( empty( $this->num_columns ) ) { $current_user = wp_get_current_user(); @@ -264,18 +270,18 @@ function get_num_columns() { * * @since 0.8.3 */ - function add_screen_options_panel() { - require_once( EDIT_FLOW_ROOT . '/common/php/' . 'screen-options.php' ); + public function add_screen_options_panel() { + require_once EDIT_FLOW_ROOT . '/common/php/screen-options.php'; add_screen_options_panel( self::usermeta_key_prefix . 'screen_columns', __( 'Screen Layout', 'edit-flow' ), array( $this, 'print_column_prefs' ), self::screen_id, array( $this, 'save_column_prefs' ), true ); } /** * Print column number preferences for screen options */ - function print_column_prefs() { + public function print_column_prefs() { $return_val = __( 'Number of Columns: ', 'edit-flow' ); for ( $i = 1; $i <= $this->max_num_columns; ++$i ) { - $return_val .= "\n"; + $return_val .= "\n"; } return $return_val; } @@ -283,7 +289,7 @@ function print_column_prefs() { /** * Save the current user's preference for number of columns. */ - function save_column_prefs( $posted_fields ) { + public function save_column_prefs( $posted_fields ) { $key = self::usermeta_key_prefix . 'screen_columns'; $this->num_columns = (int) $posted_fields[ $key ]; @@ -297,23 +303,23 @@ function save_column_prefs( $posted_fields ) { * ouput any messages, create the table navigation, then print the columns based on * get_num_columns(), which will in turn print the stories themselves. */ - function story_budget() { + public function story_budget() { // Update the current user's filters with the variables set in $_GET $this->user_filters = $this->update_user_filters(); - if ( !empty( $this->user_filters[$this->taxonomy_used] ) ) { + if ( ! empty( $this->user_filters[ $this->taxonomy_used ] ) ) { $terms = array(); - $terms[] = get_term( $this->user_filters[$this->taxonomy_used], $this->taxonomy_used ); + $terms[] = get_term( $this->user_filters[ $this->taxonomy_used ], $this->taxonomy_used ); } else { // Get all of the terms from the taxonomy, regardless whether there are published posts - $args = array( + $terms = get_terms( array( + 'taxonomy' => $this->taxonomy_used, 'orderby' => 'name', 'order' => 'asc', 'hide_empty' => 0, 'parent' => 0, - ); - $terms = get_terms( $this->taxonomy_used, $args ); + )); } $this->terms = apply_filters( 'ef_story_budget_filter_terms', $terms ); // allow for reordering or any other filtering of terms @@ -328,22 +334,22 @@ function story_budget() {
num_columns ) . '">'; - foreach( (array) $this->terms as $term ) { - $this->print_term( $term ); - } + foreach ( (array) $this->terms as $term ) { + $this->print_term( $term ); + } echo '
'; ?> @@ -355,9 +361,9 @@ function story_budget() { * * @since 0.7 */ - function story_budget_time_range() { + public function story_budget_time_range() { ?> -
+ user_filters['start_date'] ) ) ); ?> @@ -382,7 +388,7 @@ function story_budget_time_range() { * @param object $term The term we're getting posts for * @return array $term_posts An array of post objects for the term */ - function get_posts_for_term( $term, $args = null ) { + public function get_posts_for_term( $term, $args = null ) { $defaults = array( 'post_status' => null, @@ -395,7 +401,7 @@ function get_posts_for_term( $term, $args = null ) { $arg_terms = array( $term->term_id, ); - $arg_terms = array_merge( $arg_terms, get_term_children( $term->term_id, $this->taxonomy_used ) ) ; + $arg_terms = array_merge( $arg_terms, get_term_children( $term->term_id, $this->taxonomy_used ) ); $args['tax_query'] = array( array( 'taxonomy' => $this->taxonomy_used, @@ -418,15 +424,17 @@ function get_posts_for_term( $term, $args = null ) { } // Filter by post_author if it's set - if ( $args['author'] === '0' ) unset( $args['author'] ); + if ( '0' === $args['author'] ) { + unset( $args['author'] ); + } $beginning_date = strtotime( $this->user_filters['start_date'] ); $days_to_show = $this->user_filters['number_days']; $ending_date = $beginning_date + ( $days_to_show * DAY_IN_SECONDS ); $args['date_query'] = array( - 'after' => date( "Y-m-d", $beginning_date ), - 'before' => date( "Y-m-d", $ending_date ), + 'after' => gmdate( 'Y-m-d', $beginning_date ), + 'before' => gmdate( 'Y-m-d', $ending_date ), 'inclusive' => true, ); @@ -451,23 +459,29 @@ function get_posts_for_term( $term, $args = null ) { * * @param object $term The term to print. */ - function print_term( $term ) { + public function print_term( $term ) { global $wpdb; $posts = $this->get_posts_for_term( $term, $this->user_filters ); - if ( !empty( $posts ) ) + if ( ! empty( $posts ) ) { // Don't display the message for $no_matching_posts $this->no_matching_posts = false; + } - ?> -
+ ?> +

name ); ?>

- + - term_columns as $key => $name ): ?> + term_columns as $key => $name ) : ?> @@ -475,17 +489,18 @@ function print_term( $term ) { print_post( $post, $term ); + foreach ( $posts as $post ) { + $this->print_post( $post, $term ); + } ?>
- +

- - term_columns as $key => $name ) { + term_columns as $key => $name ) { echo ''; if ( method_exists( $this, 'term_column_' . $key ) ) { $method = 'term_column_' . $key; - echo $this->$method( $post, $parent_term ); + echo wp_kses_post( $this->$method( $post, $parent_term ) ); } else { - echo $this->term_column_default( $post, $key, $parent_term ); + echo wp_kses_post( $this->term_column_default( $post, $key, $parent_term ) ); } echo ''; - } ?> + } + ?> post_status ); return $status_name->label; @@ -545,12 +563,12 @@ function term_column_default( $post, $column_name, $parent_term ) { return $output; break; case 'post_modified': + // translators: %s is a human-readable time difference return sprintf( esc_html__( '%s ago', 'edit-flow' ), human_time_diff( get_the_time( 'U', $post->ID ), current_time( 'timestamp' ) ) ); break; default: break; } - } /** @@ -558,29 +576,35 @@ function term_column_default( $post, $column_name, $parent_term ) { * * @since 0.7 */ - function term_column_title( $post, $parent_term ) { + public function term_column_title( $post, $parent_term ) { $post_title = _draft_or_post_title( $post->ID ); $post_type_object = get_post_type_object( $post->post_type ); $can_edit_post = current_user_can( $post_type_object->cap->edit_post, $post->ID ); - if ( $can_edit_post ) + if ( $can_edit_post ) { $output = '' . esc_html( $post_title ) . ''; - else + } else { $output = '' . esc_html( $post_title ) . ''; + } // Edit or Trash or View $output .= '
'; $item_actions = array(); - if ( $can_edit_post ) + if ( $can_edit_post ) { $item_actions['edit'] = '' . __( 'Edit', 'edit-flow' ) . ''; - if ( EMPTY_TRASH_DAYS > 0 && current_user_can( $post_type_object->cap->delete_post, $post->ID ) ) + } + if ( EMPTY_TRASH_DAYS > 0 && current_user_can( $post_type_object->cap->delete_post, $post->ID ) ) { $item_actions['trash'] = '' . __( 'Trash', 'edit-flow' ) . ''; + } // Display a View or a Preview link depending on whether the post has been published or not - if ( in_array( $post->post_status, array( 'publish' ) ) ) + if ( in_array( $post->post_status, array( 'publish' ) ) ) { + // translators: %s is the post title $item_actions['view'] = '' . __( 'View', 'edit-flow' ) . ''; - else if ( $can_edit_post ) + } else if ( $can_edit_post ) { + // translators: %s is the post title $item_actions['previewpost'] = '' . __( 'Preview', 'edit-flow' ) . ''; + } $item_actions = apply_filters( 'ef_story_budget_item_actions', $item_actions, $post->ID ); if ( count( $item_actions ) ) { @@ -599,26 +623,28 @@ function term_column_title( $post, $parent_term ) { /** * Print any messages that should appear based on the action performed */ - function print_messages() { - ?> + public function print_messages() { + ?> -

'; // Following mostly stolen from edit.php if ( isset( $_GET['trashed'] ) && (int) $_GET['trashed'] ) { - printf( _n( 'Item moved to the trash.', '%d items moved to the trash.', $_GET['trashed'] ), number_format_i18n( $_GET['trashed'] ) ); - $ids = isset($_GET['ids']) ? $_GET['ids'] : 0; - echo ' ' . __( 'Undo', 'edit-flow' ) . '
'; - unset($_GET['trashed']); + // translators: %d is the number of posts trashed + printf( esc_html( _n( 'Item moved to the trash.', '%d items moved to the trash.', $_GET['trashed'] ), number_format_i18n( $_GET['trashed'] ) ) ); + $ids = isset( $_GET['ids'] ) ? $_GET['ids'] : 0; + echo ' ' . esc_html__( 'Undo', 'edit-flow' ) . '
'; + unset( $_GET['trashed'] ); } - if ( isset($_GET['untrashed'] ) && (int) $_GET['untrashed'] ) { - printf( _n( 'Item restored from the Trash.', '%d items restored from the Trash.', $_GET['untrashed'] ), number_format_i18n( $_GET['untrashed'] ) ); - unset($_GET['undeleted']); + if ( isset( $_GET['untrashed'] ) && (int) $_GET['untrashed'] ) { + // translators: %d is the number of posts restored from the trash + printf( esc_html( _n( 'Item restored from the Trash.', '%d items restored from the Trash.', $_GET['untrashed'] ), number_format_i18n( $_GET['untrashed'] ) ) ); + unset( $_GET['undeleted'] ); } echo '

'; @@ -628,16 +654,16 @@ function print_messages() { /** * Print the table navigation and filter controls, using the current user's filters if any are set. */ - function table_navigation() { - ?> + public function table_navigation() { + ?>
story_budget_filters() as $select_id => $select_name ) { - echo $this->story_budget_filter_options( $select_id, $select_name, $this->user_filters ); - } + foreach ( $this->story_budget_filters() as $select_id => $select_name ) { + echo wp_kses_post( $this->story_budget_filter_options( $select_id, $select_name, $this->user_filters ) ); + } ?> @@ -647,8 +673,8 @@ function table_navigation() { story_budget_filters() as $select_id => $select_name ) { - echo ''; + foreach ( $this->story_budget_filters() as $select_id => $select_name ) { + echo ''; } ?> @@ -661,23 +687,23 @@ function table_navigation() {
- $this->filter_get_param( 'post_status' ), - 'cat' => $this->filter_get_param( 'cat' ), - 'author' => $this->filter_get_param( 'author' ), - 'start_date' => $this->filter_get_param( 'start_date' ), - 'number_days' => $this->filter_get_param( 'number_days' ) + 'post_status' => $this->filter_get_param( 'post_status' ), + 'cat' => $this->filter_get_param( 'cat' ), + 'author' => $this->filter_get_param( 'author' ), + 'start_date' => $this->filter_get_param( 'start_date' ), + 'number_days' => $this->filter_get_param( 'number_days' ), ); $current_user_filters = array(); @@ -685,18 +711,20 @@ function update_user_filters() { // If any of the $_GET vars are missing, then use the current user filter foreach ( $user_filters as $key => $value ) { - if ( is_null( $value ) && !empty( $current_user_filters[$key] ) ) { - $user_filters[$key] = $current_user_filters[$key]; + if ( is_null( $value ) && ! empty( $current_user_filters[ $key ] ) ) { + $user_filters[ $key ] = $current_user_filters[ $key ]; } } - if ( !$user_filters['start_date'] ) - $user_filters['start_date'] = date( 'Y-m-d' ); + if ( ! $user_filters['start_date'] ) { + $user_filters['start_date'] = gmdate( 'Y-m-d' ); + } - if ( !$user_filters['number_days'] ) + if ( ! $user_filters['number_days'] ) { $user_filters['number_days'] = 10; + } - $user_filters = apply_filters('ef_story_budget_filter_values', $user_filters, $current_user_filters); + $user_filters = apply_filters( 'ef_story_budget_filter_values', $user_filters, $current_user_filters ); $this->update_user_meta( $current_user->ID, self::usermeta_key_prefix . 'filters', $user_filters ); return $user_filters; @@ -708,15 +736,16 @@ function update_user_filters() { * * @return array The filters for the current user, or the default filters if the current user has none. */ - function get_user_filters() { + public function get_user_filters() { $current_user = wp_get_current_user(); $user_filters = array(); $user_filters = $this->get_user_meta( $current_user->ID, self::usermeta_key_prefix . 'filters', true ); // If usermeta didn't have filters already, insert defaults into DB - if ( empty( $user_filters ) ) + if ( empty( $user_filters ) ) { $user_filters = $this->update_user_filters(); + } return $user_filters; } @@ -724,72 +753,72 @@ function get_user_filters() { * * @param string $param The parameter to look for in $_GET * @return null if the parameter is not set in $_GET, empty string if the parameter is empty in $_GET, - * or a sanitized version of the parameter from $_GET if set and not empty + * or a sanitized version of the parameter from $_GET if set and not empty */ - function filter_get_param( $param ) { + public function filter_get_param( $param ) { // Sure, this could be done in one line. But we're cooler than that: let's make it more readable! - if ( !isset( $_GET[$param] ) ) { + if ( ! isset( $_GET[ $param ] ) ) { return null; - } else if ( empty( $_GET[$param] ) ) { + } else if ( empty( $_GET[ $param ] ) ) { return ''; } - return sanitize_key( $_GET[$param] ); + return sanitize_key( $_GET[ $param ] ); } - function story_budget_filters() { + public function story_budget_filters() { $select_filter_names = array(); $select_filter_names['post_status'] = 'post_status'; $select_filter_names['cat'] = 'cat'; $select_filter_names['author'] = 'author'; - return apply_filters('ef_story_budget_filter_names', $select_filter_names); + return apply_filters( 'ef_story_budget_filter_names', $select_filter_names ); } - function story_budget_filter_options( $select_id, $select_name, $filters ) { - switch( $select_id ) { + public function story_budget_filter_options( $select_id, $select_name, $filters ) { + switch ( $select_id ) { case 'post_status': - $post_stati = $this->get_budget_post_stati(); - ?> + $post_stati = $this->get_budget_post_stati(); + ?> - __( 'View all categories', 'edit-flow' ), 'hide_empty' => 0, 'hierarchical' => 1, 'show_count' => 0, 'orderby' => 'name', - 'selected' => $this->user_filters['cat'] - ); + 'selected' => $this->user_filters['cat'], + ); wp_dropdown_categories( $category_dropdown_args ); } - break; + break; case 'author': $users_dropdown_args = array( - 'show_option_all' => __( 'View all users', 'edit-flow' ), - 'name' => 'author', - 'selected' => $this->user_filters['author'], - 'who' => 'authors', - ); + 'show_option_all' => __( 'View all users', 'edit-flow' ), + 'name' => 'author', + 'selected' => $this->user_filters['author'], + 'who' => 'authors', + ); $users_dropdown_args = apply_filters( 'ef_story_budget_users_dropdown_args', $users_dropdown_args ); wp_dropdown_users( $users_dropdown_args ); - break; + break; default: - do_action( 'ef_story_budget_filter_display', $select_id, $select_name, $filters); - break; + do_action( 'ef_story_budget_filter_display', $select_id, $select_name, $filters ); + break; } } @@ -808,13 +837,12 @@ public function get_budget_post_stati() { $final_statuses = []; - foreach( $post_stati as $status ) { - if ( !empty( $custom_status_slug_keys[ $status->name ] ) ) { + foreach ( $post_stati as $status ) { + if ( ! empty( $custom_status_slug_keys[ $status->name ] ) ) { $final_statuses[] = $status; } } return apply_filters( 'ef_budget_post_stati', $final_statuses ); } - }