Skip to content

Commit

Permalink
fix/#752_php_82_deprecated_message (#754)
Browse files Browse the repository at this point in the history
* fix deprecated message

* fix group name on Yoast checklists

* change interpolation to string concat

---------

Co-authored-by: Richa Ferry Setyawan <[email protected]>
  • Loading branch information
rizaardiyanto1412 and richaferry authored Sep 20, 2024
1 parent c3004ad commit bf7b0f5
Show file tree
Hide file tree
Showing 6 changed files with 132 additions and 78 deletions.
54 changes: 32 additions & 22 deletions core/Requirement/Prohibited_categories.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @package PublishPress\Checklists
* @author PublishPress <[email protected]>
Expand All @@ -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
Expand All @@ -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);
Expand All @@ -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']);
Expand Down Expand Up @@ -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',
Expand All @@ -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
Expand All @@ -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;
}

Expand All @@ -203,22 +213,22 @@ 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) {
$results[] = array(
'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,
Expand All @@ -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();
}
Expand All @@ -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);
}
Expand All @@ -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}";
}
Expand All @@ -284,7 +294,7 @@ private function transform_categories($categories = array())

return $labels;
}

/**
* Gets settings drop down labels.
*
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
}

Expand Down
46 changes: 28 additions & 18 deletions core/Requirement/Prohibited_tags.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @package PublishPress\Checklists
* @author PublishPress <[email protected]>
Expand Down Expand Up @@ -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)
{
Expand All @@ -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']);
Expand Down Expand Up @@ -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,
Expand All @@ -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);
}
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
}

Expand All @@ -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) {
Expand All @@ -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();
}
Expand All @@ -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}";
}
Expand All @@ -255,7 +265,7 @@ private function transform_tags($tags = array())

return $labels;
}

/**
* Gets settings drop down labels.
*
Expand All @@ -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);
}
Expand Down Expand Up @@ -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;
}

Expand Down
Loading

0 comments on commit bf7b0f5

Please sign in to comment.