Skip to content

Commit

Permalink
fix freeze webpage because dropdown
Browse files Browse the repository at this point in the history
composer update

composer update

fix code scanning alert

revert global-checklists.js
  • Loading branch information
richaferry committed Jul 30, 2024
1 parent 1f9af47 commit e17ff91
Show file tree
Hide file tree
Showing 12 changed files with 650 additions and 50 deletions.
28 changes: 14 additions & 14 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
{
"printWidth": 120,
"tabWidth": 2,
"useTabs": false,
"semi": true,
"singleQuote": true,
"quoteProps": "as-needed",
"jsxSingleQuote": false,
"trailingComma": "all",
"bracketSpacing": true,
"jsxBracketSameLine": false,
"arrowParens": "always",
"proseWrap": "preserve",
"htmlWhitespaceSensitivity": "css",
"endOfLine": "lf"
"printWidth": 120,
"tabWidth": 4,
"useTabs": false,
"semi": true,
"singleQuote": true,
"quoteProps": "as-needed",
"jsxSingleQuote": false,
"trailingComma": "all",
"bracketSpacing": true,
"jsxBracketSameLine": false,
"arrowParens": "always",
"proseWrap": "preserve",
"htmlWhitespaceSensitivity": "css",
"endOfLine": "lf"
}
5 changes: 3 additions & 2 deletions core/Requirement/Base_multiple.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,10 @@ public function get_setting_field_html($css_class = '')
}

$html .= sprintf(
'<select id="%s" name="%s" multiple="multiple">',
'<select id="%s" name="%s" class="%s" multiple="multiple">',
$id,
$name
$name,
$css_class
);

$labels = $this->get_setting_drop_down_labels();
Expand Down
159 changes: 153 additions & 6 deletions core/Requirement/Prohibited_categories.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,28 @@ class Prohibited_categories extends Base_multiple
*/
private $DELIMITER = '__';

public function __construct($module, $post_type)
{
parent::__construct($module, $post_type);
$this->init_hooks();
}

/**
* Initialize the hooks for the requirement
*
* @return void
*/
public function init_hooks() {
// Check if the hooks were already initialized
if (isset($this->hooks_initialized) && $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']);

// Set the initialization flag to true
$this->hooks_initialized = true;
}

/**
* Initialize the language strings for the instance
*
Expand Down Expand Up @@ -66,6 +88,129 @@ public function get_current_status($post, $option_value)
return empty(array_intersect($option_ids, $categories));
}

/**
* Get selected values from the settings
*
* @return array
*/
public function get_selected_values()
{
// Option names
$option_name_multiple = $this->name . '_' . $this->field_name;
$option_value = array();
if (isset($this->module->options->{$option_name_multiple}[$this->post_type])) {
$option_value = $this->module->options->{$option_name_multiple}[$this->post_type];
}
$selected_categories = array();
foreach ($option_value as $category_str) {
[$category_id, $category_name] = explode($this->DELIMITER, $category_str);
$selected_categories[] = $category_id;
}

return $selected_categories;
}

/**
* Get the list of categories
*
* @param array $args
* @return WP_Term[]
*/
public function get_list_categories($args = array('page' => 1, 'per_page' => 10, 'q' => ''))
{
// Get selected categories from the settings
$selected_categories = $this->get_selected_values();

// Retrieve selected categories only on the first page
$categories_selected = array();
if($args['page'] === 1) {
$args_selected = array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 0,
'include' => $selected_categories,
'search' => $args['q'],
);
$categories_selected = $this->get_categories_hierarchical($args_selected);
}

// Retrieve categories with a limit of 10 rows
$args_limited = array(
'type' => 'post',
'hide_empty' => 0,
'hierarchical' => 1,
'taxonomy' => 'category',
'pad_counts' => false,
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 0,
'search' => $args['q'],
'number' => $args['per_page'],
'offset' => ($args['page'] - 1) * $args['per_page']
);
$categories_limited = $this->get_categories_hierarchical($args_limited);

// Merge the two arrays
$categories = array_merge($categories_limited, $categories_selected);

// Remove duplicates based on term_id
$categories = array_values(array_reduce($categories, function ($carry, $item) {
if (!isset($carry[$item->term_id])) {
$carry[$item->term_id] = $item;
}
return $carry;
}, []));

// Sort the array by name
usort($categories, function ($a, $b) {
return strcasecmp($a->name, $b->name);
});

return $categories;
}

/**
* Get the list of categories via AJAX
*
* @return void
*/
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]);
$results = array();

foreach ($categories as $category) {
$results[] = array(
'id' => $category->term_id . $this->DELIMITER . $category->name,
'text' => $category->name,
);
if(isset($category->children)) {
foreach ($category->children as $child) {
$results[] = array(
'id' => $child->term_id . $this->DELIMITER . $child->name,
'text' => '' . $child->name,
);
}
}
}

// Check if there are more categories
$total_categories = wp_count_terms('category', 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();
}

/**
* Get Categories Hierarchical List
*
Expand Down Expand Up @@ -99,7 +244,7 @@ private function transform_categories($categories = array())
$labels[$category->term_id . $this->DELIMITER . $category->name] = $category->name;
if(isset($category->children)) {
foreach ($category->children as $child) {
$labels[$child->term_id . $this->DELIMITER . $child->name] = "- {$child->name}";
$labels[$child->term_id . $this->DELIMITER . $child->name] = " {$child->name}";
}
}
}
Expand All @@ -114,11 +259,7 @@ private function transform_categories($categories = array())
*/
public function get_setting_drop_down_labels()
{
$categories = $this->get_categories_hierarchical(array(
'orderby' => 'name',
'order' => 'ASC',
'hide_empty' => 0,
));
$categories = $this->get_list_categories();

return $this->transform_categories($categories);
}
Expand Down Expand Up @@ -196,4 +337,10 @@ public function filter_requirements_list($requirements, $post)

return $requirements;
}


public function get_setting_field_html($css_class = '')
{
return parent::get_setting_field_html('pp-checklists-full-width');
}
}
Loading

0 comments on commit e17ff91

Please sign in to comment.