From bf7b0f5dfff62f6bc0ce0523cda3b61f25a5ed06 Mon Sep 17 00:00:00 2001 From: Riza Maulana Ardiyanto <83811723+rizaardiyanto1412@users.noreply.github.com> Date: Sat, 21 Sep 2024 02:57:45 +0700 Subject: [PATCH] fix/#752_php_82_deprecated_message (#754) * fix deprecated message * fix group name on Yoast checklists * change interpolation to string concat --------- Co-authored-by: Richa Ferry Setyawan --- core/Requirement/Prohibited_categories.php | 54 +++++++++++-------- core/Requirement/Prohibited_tags.php | 46 +++++++++------- core/Requirement/Required_categories.php | 52 ++++++++++-------- core/Requirement/Required_tags.php | 44 +++++++++------ .../lib/Requirement/Readability_Analysis.php | 7 +++ .../yoastseo/lib/Requirement/Seo_Analysis.php | 7 +++ 6 files changed, 132 insertions(+), 78 deletions(-) diff --git a/core/Requirement/Prohibited_categories.php b/core/Requirement/Prohibited_categories.php index c8622f53..e0eb129b 100644 --- a/core/Requirement/Prohibited_categories.php +++ b/core/Requirement/Prohibited_categories.php @@ -1,4 +1,5 @@ @@ -22,7 +23,7 @@ class Prohibited_categories extends Base_multiple */ public $name = 'prohibited_categories'; - /** + /** * The name of the group, used for the tabs * * @var string @@ -46,6 +47,13 @@ class Prohibited_categories extends Base_multiple */ private $cache_expiration = 10 * MINUTE_IN_SECONDS; + /** + * Flag to check if hooks have been initialized + * + * @var bool + */ + private $hooks_initialized = false; + public function __construct($module, $post_type) { parent::__construct($module, $post_type); @@ -57,9 +65,10 @@ public function __construct($module, $post_type) * * @return void */ - public function init_hooks() { + public function init_hooks() + { // Check if the hooks were already initialized - if (isset($this->hooks_initialized) && $this->hooks_initialized) return; + if ($this->hooks_initialized) return; // Add the AJAX action to get the list of categories add_action('wp_ajax_pp_checklists_prohibited_category', [$this, 'get_list_category_ajax']); @@ -130,7 +139,7 @@ private function get_list_categories($args = array('page' => 1, 'per_page' => 10 // Retrieve selected categories only on the first page $categories_selected = array(); - if($args['page'] === 1 && !empty($selected_categories)) { + if ($args['page'] === 1 && !empty($selected_categories)) { $args_selected = array( 'orderby' => 'name', 'order' => 'ASC', @@ -156,7 +165,7 @@ private function get_list_categories($args = array('page' => 1, 'per_page' => 10 ); $categories_limited = $this->get_categories_hierarchical($args_limited); - // Merge the two arrays + // Merge the two arrays $categories = array_merge($categories_limited, $categories_selected); // Remove duplicates based on term_id @@ -181,16 +190,17 @@ private function get_list_categories($args = array('page' => 1, 'per_page' => 10 * @param array $args * @return int */ - private function get_total_count($args = array('search' => '', 'hide_empty' => 0)) { + private function get_total_count($args = array('search' => '', 'hide_empty' => 0)) + { $args_key = base64_encode($args['search']); - $cache_key = "total_prohib_category_count_${args_key}"; + $cache_key = 'total_prohib_category_count_' . $args_key; $total_categories = get_transient($cache_key); if ($total_categories === false) { $total_categories = wp_count_terms('category', $args); set_transient($cache_key, $total_categories, $this->cache_expiration); } - + return $total_categories; } @@ -203,14 +213,14 @@ public function get_list_category_ajax() { // Check if the request is valid check_ajax_referer('pp-checklists-rules', 'nonce'); - + // Get the search query and page number from the request $search = isset($_POST['q']) ? sanitize_text_field($_POST['q']) : ''; $page = isset($_POST['page']) ? intval($_POST['page']) : 1; $per_page = 10; - + // Get the categories - $categories = $this->get_list_categories(['page' => $page ,'per_page' => $per_page, 'q' => $search]); + $categories = $this->get_list_categories(['page' => $page, 'per_page' => $per_page, 'q' => $search]); $results = array(); foreach ($categories as $category) { @@ -218,7 +228,7 @@ public function get_list_category_ajax() 'id' => $category->term_id . $this->DELIMITER . $category->name, 'text' => $category->name, ); - if(isset($category->children)) { + if (isset($category->children)) { foreach ($category->children as $child) { $results[] = array( 'id' => $child->term_id . $this->DELIMITER . $child->name, @@ -231,7 +241,7 @@ public function get_list_category_ajax() // Check if there are more categories $total_categories = $this->get_total_count(array('search' => $search, 'hide_empty' => 0)); $has_next = ($page * $per_page) < $total_categories; - + wp_send_json_success(['items' => $results, 'has_next' => $has_next]); wp_die(); } @@ -244,15 +254,15 @@ public function get_list_category_ajax() */ private function get_categories_hierarchical($args = array()) { - if( !isset( $args[ 'parent' ] ) ) $args[ 'parent' ] = 0; + if (!isset($args['parent'])) $args['parent'] = 0; $cache_key = md5('prohib_category' . json_encode($args)); $categories = get_transient($cache_key); // if cache is empty, get value from database - if($categories === false) { - $categories = get_categories( $args ); - foreach( $categories as $key => $category ) { + if ($categories === false) { + $categories = get_categories($args); + foreach ($categories as $key => $category) { $args['parent'] = $category->term_id; $categories[$key]->children = $this->get_categories_hierarchical($args); } @@ -275,7 +285,7 @@ private function transform_categories($categories = array()) foreach ($categories as $cat => $category) { $labels[$category->term_id . $this->DELIMITER . $category->name] = $category->name; - if(isset($category->children)) { + if (isset($category->children)) { foreach ($category->children as $child) { $labels[$child->term_id . $this->DELIMITER . $child->name] = "— {$child->name}"; } @@ -284,7 +294,7 @@ private function transform_categories($categories = array()) return $labels; } - + /** * Gets settings drop down labels. * @@ -307,9 +317,9 @@ public function get_setting_drop_down_labels() * @param int $index * @return String[] $categories */ - private function category_parser($categories = array(), $index = 0|1) + private function category_parser($categories = array(), $index = 0 | 1) { - return array_map(function($value) use ($index) { + return array_map(function ($value) use ($index) { return explode($this->DELIMITER, $value)[$index]; }, $categories); } @@ -361,7 +371,7 @@ public function filter_requirements_list($requirements, $post) $blocked_category_names = implode(', ', $blocked_categories); - if(empty($blocked_category_names)) { + if (empty($blocked_category_names)) { return $requirements; } diff --git a/core/Requirement/Prohibited_tags.php b/core/Requirement/Prohibited_tags.php index b72b0e38..abfbaa22 100644 --- a/core/Requirement/Prohibited_tags.php +++ b/core/Requirement/Prohibited_tags.php @@ -1,4 +1,5 @@ @@ -44,7 +45,14 @@ class Prohibited_tags extends Base_multiple * * @var int */ - private $cache_expiration = 10 * MINUTE_IN_SECONDS; + private $cache_expiration = 10 * MINUTE_IN_SECONDS; + + /** + * Flag to track if hooks have been initialized + * + * @var bool + */ + private $hooks_initialized = false; public function __construct($module, $post_type) { @@ -57,9 +65,10 @@ public function __construct($module, $post_type) * * @return void */ - public function init_hooks() { + public function init_hooks() + { // Check if the hooks were already initialized - if (isset($this->hooks_initialized) && $this->hooks_initialized) return; + if ($this->hooks_initialized) return; // Add the AJAX action to get the list of tags add_action('wp_ajax_pp_checklists_prohibited_tag', [$this, 'get_list_tag_ajax']); @@ -131,7 +140,7 @@ private function get_list_tags($args = array('page' => 1, 'per_page' => 10, 'q' // Retrieve selected tags only on the first page $tags_selected = array(); - if($args['page'] === 1 && !empty($selected_tags)) { + if ($args['page'] === 1 && !empty($selected_tags)) { $args_selected = array( 'taxonomy' => 'post_tag', 'hide_empty' => 0, @@ -140,7 +149,7 @@ private function get_list_tags($args = array('page' => 1, 'per_page' => 10, 'q' ); $cache_key_selected = md5('prohib_tag_selected' . json_encode($args_selected)); $tags_selected = get_transient($cache_key_selected); - if($tags_selected === false) { + if ($tags_selected === false) { $tags_selected = get_tags($args_selected); set_transient($cache_key_selected, $tags_selected, $this->cache_expiration); } @@ -157,7 +166,7 @@ private function get_list_tags($args = array('page' => 1, 'per_page' => 10, 'q' ); $cache_key = md5('prohib_tag' . json_encode($args_limited)); $tags_limited = get_transient($cache_key); - if($tags_limited === false) { + if ($tags_limited === false) { $tags_limited = get_tags($args_limited); set_transient($cache_key, $tags_limited, $this->cache_expiration); } @@ -187,16 +196,17 @@ private function get_list_tags($args = array('page' => 1, 'per_page' => 10, 'q' * @param array $args * @return int */ - private function get_total_count($args = array('search' => '', 'hide_empty' => 0)) { + private function get_total_count($args = array('search' => '', 'hide_empty' => 0)) + { $args_key = base64_encode($args['search']); - $cache_key = "total_prohib_tag_count_${args_key}"; + $cache_key = 'total_prohib_tag_count_' . $args_key; $total_tags = get_transient($cache_key); if ($total_tags === false) { $total_tags = wp_count_terms('post_tag', $args); set_transient($cache_key, $total_tags, $this->cache_expiration); } - + return $total_tags; } @@ -209,14 +219,14 @@ public function get_list_tag_ajax() { // Check if the request is valid check_ajax_referer('pp-checklists-rules', 'nonce'); - + // Get the search query and page number from the request $search = isset($_POST['q']) ? sanitize_text_field($_POST['q']) : ''; $page = isset($_POST['page']) ? intval($_POST['page']) : 1; $per_page = 10; - + // Get the tags - $tags = $this->get_list_tags(['page' => $page ,'per_page' => $per_page, 'q' => $search]); + $tags = $this->get_list_tags(['page' => $page, 'per_page' => $per_page, 'q' => $search]); $results = array(); foreach ($tags as $tag) { @@ -229,7 +239,7 @@ public function get_list_tag_ajax() // Check if there are more tags $total_tags = $this->get_total_count(array('search' => $search, 'hide_empty' => 0)); $has_next = ($page * $per_page) < $total_tags; - + wp_send_json_success(['items' => $results, 'has_next' => $has_next]); wp_die(); } @@ -246,7 +256,7 @@ private function transform_tags($tags = array()) foreach ($tags as $tag) { $labels[$tag->term_id . $this->DELIMITER . $tag->name] = $tag->name; - if(isset($tag->children)) { + if (isset($tag->children)) { foreach ($tag->children as $child) { $labels[$child->term_id . $this->DELIMITER . $child->name] = "— {$child->name}"; } @@ -255,7 +265,7 @@ private function transform_tags($tags = array()) return $labels; } - + /** * Gets settings drop down labels. * @@ -278,9 +288,9 @@ public function get_setting_drop_down_labels() * @param int $index * @return String[] $tags */ - private function tag_parser($tags = array(), $index = 0|1) + private function tag_parser($tags = array(), $index = 0 | 1) { - return array_map(function($value) use ($index) { + return array_map(function ($value) use ($index) { return explode($this->DELIMITER, $value)[$index]; }, $tags); } @@ -332,7 +342,7 @@ public function filter_requirements_list($requirements, $post) $blocked_tag_names = implode(', ', $blocked_tags); - if(empty($blocked_tag_names)) { + if (empty($blocked_tag_names)) { return $requirements; } diff --git a/core/Requirement/Required_categories.php b/core/Requirement/Required_categories.php index cfa180c9..a3debc82 100644 --- a/core/Requirement/Required_categories.php +++ b/core/Requirement/Required_categories.php @@ -1,4 +1,5 @@ @@ -46,6 +47,13 @@ class Required_categories extends Base_multiple */ private $cache_expiration = 10 * MINUTE_IN_SECONDS; + /** + * Flag to check if hooks have been initialized + * + * @var bool + */ + private $hooks_initialized = false; + public function __construct($module, $post_type) { parent::__construct($module, $post_type); @@ -57,9 +65,10 @@ public function __construct($module, $post_type) * * @return void */ - public function init_hooks() { + public function init_hooks() + { // Check if the hooks were already initialized - if (isset($this->hooks_initialized) && $this->hooks_initialized) return; + if ($this->hooks_initialized) return; // Add the AJAX action to get the list of categories add_action('wp_ajax_pp_checklists_required_category', [$this, 'get_list_category_ajax']); @@ -130,7 +139,7 @@ private function get_list_categories($args = array('page' => 1, 'per_page' => 10 // Retrieve selected categories only on the first page $categories_selected = array(); - if($args['page'] === 1 && !empty($selected_categories)) { + if ($args['page'] === 1 && !empty($selected_categories)) { $args_selected = array( 'orderby' => 'name', 'order' => 'ASC', @@ -156,7 +165,7 @@ private function get_list_categories($args = array('page' => 1, 'per_page' => 10 ); $categories_limited = $this->get_categories_hierarchical($args_limited); - // Merge the two arrays + // Merge the two arrays $categories = array_merge($categories_limited, $categories_selected); // Remove duplicates based on term_id @@ -181,16 +190,17 @@ private function get_list_categories($args = array('page' => 1, 'per_page' => 10 * @param array $args * @return int */ - private function get_total_count($args = array('search' => '', 'hide_empty' => 0)) { + private function get_total_count($args = array('search' => '', 'hide_empty' => 0)) + { $args_key = base64_encode($args['search']); - $cache_key = "total_require_category_count_${args_key}"; + $cache_key = 'total_require_category_count_' . $args_key; $total_categories = get_transient($cache_key); if ($total_categories === false) { $total_categories = wp_count_terms('category', $args); set_transient($cache_key, $total_categories, $this->cache_expiration); } - + return $total_categories; } @@ -203,14 +213,14 @@ public function get_list_category_ajax() { // Check if the request is valid check_ajax_referer('pp-checklists-rules', 'nonce'); - + // Get the search query and page number from the request $search = isset($_POST['q']) ? sanitize_text_field($_POST['q']) : ''; $page = isset($_POST['page']) ? intval($_POST['page']) : 1; $per_page = 10; - + // Get the categories - $categories = $this->get_list_categories(['page' => $page ,'per_page' => $per_page, 'q' => $search]); + $categories = $this->get_list_categories(['page' => $page, 'per_page' => $per_page, 'q' => $search]); $results = array(); foreach ($categories as $category) { @@ -218,7 +228,7 @@ public function get_list_category_ajax() 'id' => $category->term_id . $this->DELIMITER . $category->name, 'text' => $category->name, ); - if(isset($category->children)) { + if (isset($category->children)) { foreach ($category->children as $child) { $results[] = array( 'id' => $child->term_id . $this->DELIMITER . $child->name, @@ -231,7 +241,7 @@ public function get_list_category_ajax() // Check if there are more categories $total_categories = $this->get_total_count(array('search' => $search, 'hide_empty' => 0)); $has_next = ($page * $per_page) < $total_categories; - + wp_send_json_success(['items' => $results, 'has_next' => $has_next]); wp_die(); } @@ -244,15 +254,15 @@ public function get_list_category_ajax() */ private function get_categories_hierarchical($args = array()) { - if( !isset( $args[ 'parent' ] ) ) $args[ 'parent' ] = 0; + if (!isset($args['parent'])) $args['parent'] = 0; $cache_key = md5('required_category' . json_encode($args)); $categories = get_transient($cache_key); // if cache is empty, get value from database - if($categories === false) { - $categories = get_categories( $args ); - foreach( $categories as $key => $category ) { + if ($categories === false) { + $categories = get_categories($args); + foreach ($categories as $key => $category) { $args['parent'] = $category->term_id; $categories[$key]->children = $this->get_categories_hierarchical($args); } @@ -275,7 +285,7 @@ private function transform_categories($categories = array()) foreach ($categories as $cat => $category) { $labels[$category->term_id . $this->DELIMITER . $category->name] = $category->name; - if(isset($category->children)) { + if (isset($category->children)) { foreach ($category->children as $child) { $labels[$child->term_id . $this->DELIMITER . $child->name] = "— {$child->name}"; } @@ -284,7 +294,7 @@ private function transform_categories($categories = array()) return $labels; } - + /** * Gets settings drop down labels. * @@ -307,9 +317,9 @@ public function get_setting_drop_down_labels() * @param int $index * @return String[] $categories */ - private function category_parser($categories = array(), $index = 0|1) + private function category_parser($categories = array(), $index = 0 | 1) { - return array_map(function($value) use ($index) { + return array_map(function ($value) use ($index) { return explode($this->DELIMITER, $value)[$index]; }, $categories); } @@ -361,7 +371,7 @@ public function filter_requirements_list($requirements, $post) $required_category_names = implode(', ', $required_categories); - if(empty($required_category_names)) { + if (empty($required_category_names)) { return $requirements; } diff --git a/core/Requirement/Required_tags.php b/core/Requirement/Required_tags.php index c427283e..5c1911c5 100644 --- a/core/Requirement/Required_tags.php +++ b/core/Requirement/Required_tags.php @@ -1,4 +1,5 @@ @@ -46,6 +47,13 @@ class Required_tags extends Base_multiple */ private $cache_expiration = 10 * MINUTE_IN_SECONDS; + /** + * Flag to check if hooks have been initialized + * + * @var bool + */ + private $hooks_initialized = false; + public function __construct($module, $post_type) { parent::__construct($module, $post_type); @@ -57,9 +65,10 @@ public function __construct($module, $post_type) * * @return void */ - public function init_hooks() { + public function init_hooks() + { // Check if the hooks were already initialized - if (isset($this->hooks_initialized) && $this->hooks_initialized) return; + if ($this->hooks_initialized) return; // Add the AJAX action to get the list of tags add_action('wp_ajax_pp_checklists_required_tag', [$this, 'get_list_tag_ajax']); @@ -131,7 +140,7 @@ private function get_list_tags($args = array('page' => 1, 'per_page' => 10, 'q' // Retrieve selected tags only on the first page $tags_selected = array(); - if($args['page'] === 1 && !empty($selected_tags)) { + if ($args['page'] === 1 && !empty($selected_tags)) { $args_selected = array( 'taxonomy' => 'post_tag', 'hide_empty' => 0, @@ -140,7 +149,7 @@ private function get_list_tags($args = array('page' => 1, 'per_page' => 10, 'q' ); $cache_key_selected = md5('required_tag_selected' . json_encode($args_selected)); $tags_selected = get_transient($cache_key_selected); - if($tags_selected === false) { + if ($tags_selected === false) { $tags_selected = get_tags($args_selected); set_transient($cache_key_selected, $tags_selected, $this->cache_expiration); } @@ -157,7 +166,7 @@ private function get_list_tags($args = array('page' => 1, 'per_page' => 10, 'q' ); $cache_key = md5('required_tag' . json_encode($args_limited)); $tags_limited = get_transient($cache_key); - if($tags_limited === false) { + if ($tags_limited === false) { $tags_limited = get_tags($args_limited); set_transient($cache_key, $tags_limited, $this->cache_expiration); } @@ -187,16 +196,17 @@ private function get_list_tags($args = array('page' => 1, 'per_page' => 10, 'q' * @param array $args * @return int */ - private function get_total_count($args = array('search' => '', 'hide_empty' => 0)) { + private function get_total_count($args = array('search' => '', 'hide_empty' => 0)) + { $args_key = base64_encode($args['search']); - $cache_key = "total_required_tag_count_${args_key}"; + $cache_key = 'total_required_tag_count_' . $args_key; $total_tags = get_transient($cache_key); if ($total_tags === false) { $total_tags = wp_count_terms('post_tag', $args); set_transient($cache_key, $total_tags, $this->cache_expiration); } - + return $total_tags; } @@ -209,14 +219,14 @@ public function get_list_tag_ajax() { // Check if the request is valid check_ajax_referer('pp-checklists-rules', 'nonce'); - + // Get the search query and page number from the request $search = isset($_POST['q']) ? sanitize_text_field($_POST['q']) : ''; $page = isset($_POST['page']) ? intval($_POST['page']) : 1; $per_page = 10; - + // Get the tags - $tags = $this->get_list_tags(['page' => $page ,'per_page' => $per_page, 'q' => $search]); + $tags = $this->get_list_tags(['page' => $page, 'per_page' => $per_page, 'q' => $search]); $results = array(); foreach ($tags as $tag) { @@ -229,7 +239,7 @@ public function get_list_tag_ajax() // Check if there are more tags $total_tags = $this->get_total_count(array('search' => $search, 'hide_empty' => 0)); $has_next = ($page * $per_page) < $total_tags; - + wp_send_json_success(['items' => $results, 'has_next' => $has_next]); wp_die(); } @@ -246,7 +256,7 @@ private function transform_tags($tags = array()) foreach ($tags as $tag) { $labels[$tag->term_id . $this->DELIMITER . $tag->name] = $tag->name; - if(isset($tag->children)) { + if (isset($tag->children)) { foreach ($tag->children as $child) { $labels[$child->term_id . $this->DELIMITER . $child->name] = "— {$child->name}"; } @@ -255,7 +265,7 @@ private function transform_tags($tags = array()) return $labels; } - + /** * Gets settings drop down labels. * @@ -278,9 +288,9 @@ public function get_setting_drop_down_labels() * @param int $index * @return String[] $tags */ - private function tag_parser($tags = array(), $index = 0|1) + private function tag_parser($tags = array(), $index = 0 | 1) { - return array_map(function($value) use ($index) { + return array_map(function ($value) use ($index) { return explode($this->DELIMITER, $value)[$index]; }, $tags); } @@ -332,7 +342,7 @@ public function filter_requirements_list($requirements, $post) $required_tag_names = implode(', ', $required_tags); - if(empty($required_tag_names)) { + if (empty($required_tag_names)) { return $requirements; } diff --git a/modules/yoastseo/lib/Requirement/Readability_Analysis.php b/modules/yoastseo/lib/Requirement/Readability_Analysis.php index 06fb3b55..c66f44f1 100644 --- a/modules/yoastseo/lib/Requirement/Readability_Analysis.php +++ b/modules/yoastseo/lib/Requirement/Readability_Analysis.php @@ -37,6 +37,13 @@ class Readability_Analysis extends Base_dropdown */ public $name = 'yoast_readability_analysis'; + /** + * The name of the requirement, in a slug format + * + * @var string + */ + public $group = 'content'; + /** * Initialize the language strings for the instance * diff --git a/modules/yoastseo/lib/Requirement/Seo_Analysis.php b/modules/yoastseo/lib/Requirement/Seo_Analysis.php index f9a6cc6d..256954e6 100644 --- a/modules/yoastseo/lib/Requirement/Seo_Analysis.php +++ b/modules/yoastseo/lib/Requirement/Seo_Analysis.php @@ -36,6 +36,13 @@ class Seo_Analysis extends Base_dropdown */ public $name = 'yoast_seo_analysis'; + /** + * The name of the requirement, in a slug format + * + * @var string + */ + public $group = 'content'; + /** * Initialize the language strings for the instance *