| 1 |
<?php |
| 2 |
/** |
| 3 |
* Pluggable functions. |
| 4 |
* |
| 5 |
* @package WebberZone\Top_Ten |
| 6 |
*/ |
| 7 |
|
| 8 |
if ( ! function_exists( 'wz_more_recurrences' ) ) : |
| 9 |
|
| 10 |
/** |
| 11 |
* Function to add weekly and fortnightly recurrences. Filters `cron_schedules`. |
| 12 |
* |
| 13 |
* @param array $schedules Array of existing schedules. |
| 14 |
* @return array Filtered array with new schedules |
| 15 |
*/ |
| 16 |
function wz_more_recurrences( $schedules ) { |
| 17 |
// Add a 'weekly' interval. |
| 18 |
$schedules['weekly'] = array( |
| 19 |
'interval' => WEEK_IN_SECONDS, |
| 20 |
'display' => __( 'Once Weekly', 'top-10' ), |
| 21 |
); |
| 22 |
$schedules['fortnightly'] = array( |
| 23 |
'interval' => 2 * WEEK_IN_SECONDS, |
| 24 |
'display' => __( 'Once Fortnightly', 'top-10' ), |
| 25 |
); |
| 26 |
$schedules['monthly'] = array( |
| 27 |
'interval' => 30 * DAY_IN_SECONDS, |
| 28 |
'display' => __( 'Once Monthly', 'top-10' ), |
| 29 |
); |
| 30 |
$schedules['quarterly'] = array( |
| 31 |
'interval' => 90 * DAY_IN_SECONDS, |
| 32 |
'display' => __( 'Once quarterly', 'top-10' ), |
| 33 |
); |
| 34 |
$schedules['one_minute'] = array( |
| 35 |
'interval' => MINUTE_IN_SECONDS, |
| 36 |
'display' => __( 'Every Minute', 'top-10' ), |
| 37 |
); |
| 38 |
$schedules['two_minutes'] = array( |
| 39 |
'interval' => 2 * MINUTE_IN_SECONDS, |
| 40 |
'display' => __( 'Every 2 Minutes', 'top-10' ), |
| 41 |
); |
| 42 |
$schedules['three_minutes'] = array( |
| 43 |
'interval' => 3 * MINUTE_IN_SECONDS, |
| 44 |
'display' => __( 'Every 3 Minutes', 'top-10' ), |
| 45 |
); |
| 46 |
$schedules['five_minutes'] = array( |
| 47 |
'interval' => 5 * MINUTE_IN_SECONDS, |
| 48 |
'display' => __( 'Every 5 Minutes', 'top-10' ), |
| 49 |
); |
| 50 |
return $schedules; |
| 51 |
} |
| 52 |
add_filter( 'cron_schedules', 'wz_more_recurrences' ); |
| 53 |
|
| 54 |
endif; |
| 55 |
|
| 56 |
|
| 57 |
if ( ! function_exists( 'wz_switch_site_rewrite' ) ) : |
| 58 |
|
| 59 |
/** |
| 60 |
* Refreshes $wp_rewrite when switching sites. |
| 61 |
* |
| 62 |
* Deal with permalinks and cat and tag base structures. Can slow down your site loading - handle with care! |
| 63 |
* Use add_action( 'switch_blog', 'wz_switch_site_rewrite' ) when needed and remove_action after processing. |
| 64 |
* |
| 65 |
* @global object $wp_rewrite |
| 66 |
*/ |
| 67 |
function wz_switch_site_rewrite() { |
| 68 |
global $wp_rewrite; |
| 69 |
|
| 70 |
if ( is_object( $wp_rewrite ) ) { |
| 71 |
|
| 72 |
$permalink_structure = get_option( 'permalink_structure' ); |
| 73 |
|
| 74 |
if ( ! empty( $permalink_structure ) ) { |
| 75 |
$wp_rewrite->set_permalink_structure( $permalink_structure ); |
| 76 |
} |
| 77 |
|
| 78 |
$category_base = get_option( 'category_base' ); |
| 79 |
|
| 80 |
if ( ! empty( $category_base ) ) { |
| 81 |
$wp_rewrite->set_category_base( $category_base ); |
| 82 |
} |
| 83 |
|
| 84 |
$tag_base = get_option( 'tag_base' ); |
| 85 |
|
| 86 |
if ( ! empty( $tag_base ) ) { |
| 87 |
$wp_rewrite->set_tag_base( $tag_base ); |
| 88 |
} |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
endif; |
| 93 |
|
| 94 |
if ( ! function_exists( 'wz_get_all_parent_ids' ) ) : |
| 95 |
|
| 96 |
/** |
| 97 |
* Get all parent term_taxonomy_ids for a given array of term_taxonomy_ids. |
| 98 |
* |
| 99 |
* @param int|int[] $term_taxonomy_ids Array of term_taxonomy_ids or a single term_taxonomy_id. |
| 100 |
* @param string $levels Use 'all' for ancestors, 'parent' for the immediate parent. |
| 101 |
* @return int[] Array of all parent term_taxonomy_ids merged with $term_taxonomy_ids. |
| 102 |
*/ |
| 103 |
function wz_get_all_parent_ids( $term_taxonomy_ids, $levels = 'parent' ) { |
| 104 |
$all_ids = array(); |
| 105 |
$cache = array(); |
| 106 |
|
| 107 |
foreach ( (array) $term_taxonomy_ids as $term_taxonomy_id ) { |
| 108 |
if ( isset( $cache[ $term_taxonomy_id ] ) ) { |
| 109 |
$term = $cache[ $term_taxonomy_id ]; |
| 110 |
} else { |
| 111 |
$term = WP_Term::get_instance( $term_taxonomy_id ); |
| 112 |
$cache[ $term_taxonomy_id ] = $term; |
| 113 |
} |
| 114 |
|
| 115 |
if ( $term && ! is_wp_error( $term ) ) { |
| 116 |
$taxonomy = $term->taxonomy; |
| 117 |
|
| 118 |
if ( 'all' === $levels ) { |
| 119 |
$ancestors = get_ancestors( $term->term_id, $taxonomy, 'taxonomy' ); |
| 120 |
|
| 121 |
foreach ( $ancestors as $ancestor_term_id ) { |
| 122 |
if ( isset( $cache[ $ancestor_term_id ] ) ) { |
| 123 |
$ancestor_term = $cache[ $ancestor_term_id ]; |
| 124 |
} else { |
| 125 |
$ancestor_term = WP_Term::get_instance( $ancestor_term_id ); |
| 126 |
$cache[ $ancestor_term_id ] = $ancestor_term; |
| 127 |
} |
| 128 |
|
| 129 |
if ( $ancestor_term && ! is_wp_error( $ancestor_term ) ) { |
| 130 |
$all_ids[] = $ancestor_term->term_taxonomy_id; |
| 131 |
} |
| 132 |
} |
| 133 |
} else { |
| 134 |
$parent_id = $term->parent; |
| 135 |
if ( $parent_id > 0 ) { |
| 136 |
if ( isset( $cache[ $parent_id ] ) ) { |
| 137 |
$parent_term = $cache[ $parent_id ]; |
| 138 |
} else { |
| 139 |
$parent_term = WP_Term::get_instance( $parent_id ); |
| 140 |
$cache[ $parent_id ] = $parent_term; |
| 141 |
} |
| 142 |
|
| 143 |
if ( $parent_term && ! is_wp_error( $parent_term ) ) { |
| 144 |
$all_ids[] = $parent_term->term_taxonomy_id; |
| 145 |
} |
| 146 |
} |
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
// Perform array operations outside the loop for better performance. |
| 152 |
$result_ids = array_merge( $term_taxonomy_ids, $all_ids ); |
| 153 |
|
| 154 |
return array_unique( $result_ids ); |
| 155 |
} |
| 156 |
|
| 157 |
endif; |
| 158 |
|
| 159 |
if ( ! function_exists( 'wz_tags_search' ) ) : |
| 160 |
|
| 161 |
/** |
| 162 |
* Function to add an action to search for tags using Ajax. |
| 163 |
* |
| 164 |
* @return void |
| 165 |
*/ |
| 166 |
function wz_tags_search() { |
| 167 |
if ( ! isset( $_REQUEST['tax'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 168 |
wp_die(); |
| 169 |
} |
| 170 |
|
| 171 |
$requested_tax = sanitize_text_field( wp_unslash( $_REQUEST['tax'] ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 172 |
|
| 173 |
// Check if 'all' is requested. |
| 174 |
if ( 'all' === $requested_tax ) { |
| 175 |
$all_taxonomies = get_taxonomies( array( 'public' => true ), 'names' ); |
| 176 |
$taxonomies = array_keys( $all_taxonomies ); |
| 177 |
} else { |
| 178 |
$taxonomies = wp_parse_list( $requested_tax ); |
| 179 |
} |
| 180 |
|
| 181 |
// Validate taxonomies and check capabilities. |
| 182 |
foreach ( $taxonomies as $key => $taxonomy ) { |
| 183 |
$tax = get_taxonomy( $taxonomy ); |
| 184 |
if ( ! $tax || ! current_user_can( $tax->cap->assign_terms ) ) { |
| 185 |
unset( $taxonomies[ $key ] ); |
| 186 |
} |
| 187 |
} |
| 188 |
|
| 189 |
if ( empty( $taxonomies ) ) { |
| 190 |
wp_die(); |
| 191 |
} |
| 192 |
|
| 193 |
$s = isset( $_REQUEST['q'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['q'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended |
| 194 |
|
| 195 |
$comma = _x( ',', 'tag delimiter' ); |
| 196 |
if ( ',' !== $comma ) { |
| 197 |
$s = str_replace( $comma, ',', $s ); |
| 198 |
} |
| 199 |
if ( false !== strpos( $s, ',' ) ) { |
| 200 |
$s = explode( ',', $s ); |
| 201 |
$s = $s[ count( $s ) - 1 ]; |
| 202 |
} |
| 203 |
$s = trim( $s, " \t\n\r\0\x0B" ); |
| 204 |
|
| 205 |
/** This filter has been defined in /wp-admin/includes/ajax-actions.php */ |
| 206 |
$term_search_min_chars = (int) apply_filters( 'term_search_min_chars', 2, $taxonomies, $s ); |
| 207 |
|
| 208 |
/* |
| 209 |
* Require $term_search_min_chars chars for matching (default: 2) |
| 210 |
* ensure it's a non-negative, non-zero integer. |
| 211 |
*/ |
| 212 |
if ( ( 0 === $term_search_min_chars ) || ( strlen( $s ) < $term_search_min_chars ) ) { |
| 213 |
wp_die(); |
| 214 |
} |
| 215 |
|
| 216 |
$terms = get_terms( |
| 217 |
array( |
| 218 |
'taxonomy' => $taxonomies, |
| 219 |
'name__like' => $s, |
| 220 |
'hide_empty' => false, |
| 221 |
) |
| 222 |
); |
| 223 |
|
| 224 |
$results = array(); |
| 225 |
foreach ( (array) $terms as $term ) { |
| 226 |
$results[] = "{$term->name} ({$term->taxonomy}:{$term->term_taxonomy_id})"; |
| 227 |
} |
| 228 |
|
| 229 |
echo wp_json_encode( $results ); |
| 230 |
wp_die(); |
| 231 |
} |
| 232 |
add_action( 'wp_ajax_wz_tags_search', 'wz_tags_search' ); |
| 233 |
add_action( 'wp_ajax_nopriv_wz_tags_search', 'wz_tags_search' ); |
| 234 |
endif; |
| 235 |
|