| 1 |
<?php |
| 2 |
|
| 3 |
class SGPMHelper |
| 4 |
{ |
| 5 |
public static function createSelectBox($data, $selectedValue, $attrs) |
| 6 |
{ |
| 7 |
$attrString = ''; |
| 8 |
$selected = ''; |
| 9 |
$selectBoxCloseTag = '</select>'; |
| 10 |
|
| 11 |
if (!empty($attrs) && isset($attrs)) { |
| 12 |
foreach ($attrs as $attrName => $attrValue) { |
| 13 |
$attrString .= ''.$attrName.'="'.$attrValue.'" '; |
| 14 |
} |
| 15 |
} |
| 16 |
|
| 17 |
$selectBox = '<select '.$attrString.'>'; |
| 18 |
|
| 19 |
if (empty($data)) { |
| 20 |
$selectBox .= $selectBoxCloseTag; |
| 21 |
return $selectBox; |
| 22 |
} |
| 23 |
foreach ($data as $value => $label) { |
| 24 |
/*When is multiSelect*/ |
| 25 |
if (is_array($selectedValue)) { |
| 26 |
$isSelected = in_array($value, $selectedValue); |
| 27 |
if ($isSelected) { |
| 28 |
$selected = 'selected'; |
| 29 |
} |
| 30 |
} |
| 31 |
else if ($selectedValue == $value) { |
| 32 |
$selected = 'selected'; |
| 33 |
} |
| 34 |
else if (is_array($value) && in_array($selectedValue, $value)) { |
| 35 |
$selected = 'selected'; |
| 36 |
} |
| 37 |
|
| 38 |
if (is_array($label)) { |
| 39 |
$selectBox .= '<optgroup label="'.$value.'">'; |
| 40 |
foreach ($label as $key => $optionLabel) { |
| 41 |
$selected = ''; |
| 42 |
if (is_array($selectedValue)) { |
| 43 |
$isSelected = in_array($key, $selectedValue); |
| 44 |
if ($isSelected) { |
| 45 |
$selected = 'selected'; |
| 46 |
} |
| 47 |
} |
| 48 |
else if ($selectedValue == $key) { |
| 49 |
$selected = 'selected'; |
| 50 |
} |
| 51 |
else if (is_array($key) && in_array($selectedValue, $key)) { |
| 52 |
$selected = 'selected'; |
| 53 |
} |
| 54 |
|
| 55 |
$selectBox .= '<option value="'.$key.'" '.$selected.'>'.$optionLabel.'</option>'; |
| 56 |
} |
| 57 |
$selectBox .= '</optgroup>'; |
| 58 |
} |
| 59 |
else { |
| 60 |
$selectBox .= '<option value="'.$value.'" '.$selected.'>'.$label.'</option>'; |
| 61 |
} |
| 62 |
|
| 63 |
$selected = ''; |
| 64 |
} |
| 65 |
|
| 66 |
$selectBox .= $selectBoxCloseTag; |
| 67 |
|
| 68 |
return $selectBox; |
| 69 |
} |
| 70 |
|
| 71 |
public static function createInput($data, $selectedValue, $attrs) |
| 72 |
{ |
| 73 |
$attrString = ''; |
| 74 |
$savedData = $data; |
| 75 |
|
| 76 |
if (!empty($selectedValue)) { |
| 77 |
$savedData = $selectedValue; |
| 78 |
} |
| 79 |
if (!empty($attrs) && isset($attrs)) { |
| 80 |
foreach ($attrs as $attrName => $attrValue) { |
| 81 |
$attrString .= ''.$attrName.'="'.$attrValue.'" '; |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
$input = "<input $attrString value=\"$savedData\">"; |
| 86 |
|
| 87 |
return $input; |
| 88 |
} |
| 89 |
|
| 90 |
public static function createCheckBox($data, $selectedValue, $attrs) |
| 91 |
{ |
| 92 |
$attrString = ''; |
| 93 |
$checked = ''; |
| 94 |
|
| 95 |
if (!empty($selectedValue)) { |
| 96 |
$checked = 'checked'; |
| 97 |
} |
| 98 |
if (!empty($attrs) && isset($attrs)) { |
| 99 |
foreach ($attrs as $attrName => $attrValue) { |
| 100 |
$attrString .= ''.$attrName.'="'.$attrValue.'" '; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
$input = "<input $attrString $checked>"; |
| 105 |
|
| 106 |
return $input; |
| 107 |
} |
| 108 |
|
| 109 |
public static function getPostTypeData($args = array()) |
| 110 |
{ |
| 111 |
$defaultArgs = array( |
| 112 |
'offset' => 0, |
| 113 |
'orderby' => 'date', |
| 114 |
'order' => 'DESC', |
| 115 |
'post_status' => 'publish', |
| 116 |
'suppress_filters' => false, |
| 117 |
'post_type' => 'post', |
| 118 |
'posts_per_page' => 1000 |
| 119 |
); |
| 120 |
|
| 121 |
$args = wp_parse_args($args, $defaultArgs); |
| 122 |
|
| 123 |
$query = new WP_Query($args); |
| 124 |
|
| 125 |
$posts = array(); |
| 126 |
foreach ($query->posts as $post) { |
| 127 |
$posts[$post->ID] = $post->post_title; |
| 128 |
} |
| 129 |
|
| 130 |
return $posts; |
| 131 |
} |
| 132 |
|
| 133 |
private static function getAllCustomPosts() |
| 134 |
{ |
| 135 |
$args = array( |
| 136 |
'public' => true, |
| 137 |
'_builtin' => false |
| 138 |
); |
| 139 |
|
| 140 |
$allCustomPosts = get_post_types($args); |
| 141 |
|
| 142 |
return $allCustomPosts; |
| 143 |
} |
| 144 |
|
| 145 |
public static function addFilters() |
| 146 |
{ |
| 147 |
self::addPostTypeToFilters(); |
| 148 |
} |
| 149 |
|
| 150 |
private static function addPostTypeToFilters() |
| 151 |
{ |
| 152 |
add_filter('sgpmPopupTargetParams', array(__CLASS__, 'addPopupTargetParams'), 1, 1); |
| 153 |
add_filter('sgpmPopupTargetData', array(__CLASS__, 'addPopupTargetData'), 1, 1); |
| 154 |
add_filter('sgpmPopupTargetTypes', array(__CLASS__, 'addPopupTargetTypes'), 1, 1); |
| 155 |
add_filter('sgpmPopupTargetAttrs', array(__CLASS__, 'addPopupTargetAttrs'), 1, 1); |
| 156 |
add_filter('sgpmPopupPageTemplates', array(__CLASS__, 'addPopupPageTemplates'), 1, 1); |
| 157 |
} |
| 158 |
|
| 159 |
public static function addPopupTargetParams($targetParams) |
| 160 |
{ |
| 161 |
$allCustomPostTypes = self::getAllCustomPosts(); |
| 162 |
|
| 163 |
foreach ($allCustomPostTypes as $customPostType) { |
| 164 |
$targetParams[$customPostType] = array( |
| 165 |
$customPostType.'_all' => 'All '.ucfirst($customPostType).'s', |
| 166 |
$customPostType.'_selected' => 'Selected '.ucfirst($customPostType).'s', |
| 167 |
); |
| 168 |
|
| 169 |
if ($customPostType == 'product') { |
| 170 |
$targetParams[$customPostType][$customPostType.'_selected_category'] = 'Category '.ucfirst($customPostType).'s'; |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
return $targetParams; |
| 175 |
} |
| 176 |
|
| 177 |
public static function addPopupTargetData($targetData) |
| 178 |
{ |
| 179 |
$allCustomPostTypes = self::getAllCustomPosts(); |
| 180 |
|
| 181 |
foreach ($allCustomPostTypes as $customPostType) { |
| 182 |
$targetData[$customPostType.'_all'] = null; |
| 183 |
$targetData[$customPostType.'_selected'] = ''; |
| 184 |
$targetData[$customPostType.'_selected_category'] = ''; |
| 185 |
} |
| 186 |
|
| 187 |
return $targetData; |
| 188 |
} |
| 189 |
|
| 190 |
public static function addPopupTargetTypes($targetTypes) |
| 191 |
{ |
| 192 |
$allCustomPostTypes = self::getAllCustomPosts(); |
| 193 |
|
| 194 |
foreach ($allCustomPostTypes as $customPostType) { |
| 195 |
$targetTypes[$customPostType.'_selected'] = 'select'; |
| 196 |
$targetTypes[$customPostType.'_selected_category'] = 'select'; |
| 197 |
} |
| 198 |
|
| 199 |
return $targetTypes; |
| 200 |
} |
| 201 |
|
| 202 |
public static function addPopupTargetAttrs($targetAttrs) |
| 203 |
{ |
| 204 |
$allCustomPostTypes = self::getAllCustomPosts(); |
| 205 |
|
| 206 |
foreach ($allCustomPostTypes as $customPostType) { |
| 207 |
|
| 208 |
$targetAttrs[$customPostType.'_selected'] = array('class' => 'js-sgpm-select2 js-select-ajax', 'data-select-class' => 'js-select-ajax', 'data-select-type' => 'ajax', 'data-value-param' => $customPostType, 'multiple' => 'multiple'); |
| 209 |
|
| 210 |
if ($customPostType == 'product') { |
| 211 |
$targetAttrs[$customPostType.'_selected_category'] = array('class' => 'js-sgpm-select2 js-select-ajax', 'data-select-class' => 'js-select-ajax', 'data-select-type' => 'ajax', 'data-value-param' => $customPostType.'_cat', 'data-value-type' => 'category', 'multiple' => 'multiple'); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
return $targetAttrs; |
| 216 |
} |
| 217 |
|
| 218 |
public static function addPopupPageTemplates($templates) |
| 219 |
{ |
| 220 |
$pageTemplates = self::getPageTemplates(); |
| 221 |
$pageTemplates += $templates; |
| 222 |
|
| 223 |
return $pageTemplates; |
| 224 |
} |
| 225 |
|
| 226 |
public static function getAllCustomPostTypes() |
| 227 |
{ |
| 228 |
$args = array( |
| 229 |
'public' => true, |
| 230 |
'_builtin' => false |
| 231 |
); |
| 232 |
|
| 233 |
$allCustomPosts = get_post_types($args); |
| 234 |
|
| 235 |
return $allCustomPosts; |
| 236 |
} |
| 237 |
|
| 238 |
public static function getPostsAllCategories() |
| 239 |
{ |
| 240 |
$categories = get_categories( |
| 241 |
array( |
| 242 |
'hide_empty' => 0, |
| 243 |
'type' => 'post', |
| 244 |
'orderby' => 'name', |
| 245 |
'order' => 'ASC' |
| 246 |
) |
| 247 |
); |
| 248 |
$categoriesParams = array(); |
| 249 |
foreach ($categories as $category) { |
| 250 |
$id = $category->term_id; |
| 251 |
$name = $category->name; |
| 252 |
$categoriesParams[$id] = $name; |
| 253 |
} |
| 254 |
|
| 255 |
return $categoriesParams; |
| 256 |
} |
| 257 |
|
| 258 |
public static function getPageTypes() |
| 259 |
{ |
| 260 |
$postTypes = array(); |
| 261 |
$postTypes['is_home_page'] = __('Home Page', 'sgpmPopupMaker'); |
| 262 |
$postTypes['is_home'] = __('Posts Page', 'sgpmPopupMaker'); |
| 263 |
$postTypes['is_search'] = __('Search Page', 'sgpmPopupMaker'); |
| 264 |
$postTypes['is_404'] = __('404 Page', 'sgpmPopupMaker'); |
| 265 |
|
| 266 |
return $postTypes; |
| 267 |
} |
| 268 |
|
| 269 |
public static function getPageTemplates() |
| 270 |
{ |
| 271 |
$pageTemplates = array( |
| 272 |
'page.php' => __('Default Template', 'sgpmPopupMaker') |
| 273 |
); |
| 274 |
|
| 275 |
$templates = wp_get_theme()->get_page_templates(); |
| 276 |
if (empty($templates)) { |
| 277 |
return $pageTemplates; |
| 278 |
} |
| 279 |
|
| 280 |
foreach ($templates as $key => $value) { |
| 281 |
|
| 282 |
$templateName = explode('/', $key); |
| 283 |
if (is_array($templateName)) { |
| 284 |
if (count($templateName) > 1) { |
| 285 |
$templateName = $templateName[1]; |
| 286 |
} |
| 287 |
else { |
| 288 |
$templateName = $templateName[0]; |
| 289 |
} |
| 290 |
} |
| 291 |
$pageTemplates[$templateName] = $value; |
| 292 |
|
| 293 |
} |
| 294 |
|
| 295 |
return $pageTemplates; |
| 296 |
} |
| 297 |
|
| 298 |
public static function taxonomySelectlist( $taxonomies = array(), $args = array(), $include_total = false ) { |
| 299 |
if ( empty ( $taxonomies ) ) { |
| 300 |
$taxonomies = array( 'category' ); |
| 301 |
} |
| 302 |
|
| 303 |
$args = wp_parse_args( $args, array( |
| 304 |
'hide_empty' => false, |
| 305 |
'number' => 10, |
| 306 |
'search' => '', |
| 307 |
'include' => null, |
| 308 |
'offset' => 0, |
| 309 |
'page' => null, |
| 310 |
) ); |
| 311 |
|
| 312 |
if ( $args['page'] ) { |
| 313 |
$args['offset'] = ( $args['page'] - 1 ) * $args['number']; |
| 314 |
} |
| 315 |
|
| 316 |
// Query Caching. |
| 317 |
static $queries = array(); |
| 318 |
|
| 319 |
$key = md5( serialize( $args ) ); |
| 320 |
|
| 321 |
if ( ! isset( $queries[ $key ] ) ) { |
| 322 |
// Fetch terms using the updated approach |
| 323 |
$terms_array = get_terms( array( |
| 324 |
'taxonomy' => $taxonomies, // Specify the taxonomy or array of taxonomies |
| 325 |
'hide_empty' => $args['hide_empty'], |
| 326 |
'number' => $args['number'], |
| 327 |
'search' => $args['search'], |
| 328 |
'include' => $args['include'], |
| 329 |
'offset' => $args['offset'], |
| 330 |
'paged' => $args['paged'], // Correct parameter name is 'paged' |
| 331 |
) ); |
| 332 |
|
| 333 |
$terms = array(); |
| 334 |
foreach ( $terms_array as $term ) { |
| 335 |
$terms[ $term->name ] = $term->term_id; |
| 336 |
} |
| 337 |
|
| 338 |
$results = array( |
| 339 |
'items' => $terms, |
| 340 |
'total_count' => $include_total ? wp_count_terms( array( |
| 341 |
'taxonomy' => $taxonomies, // Specify the taxonomy or array of taxonomies |
| 342 |
'hide_empty' => $args['hide_empty'], |
| 343 |
'search' => $args['search'], |
| 344 |
'include' => $args['include'], |
| 345 |
'page' => $args['page'], // Correct parameter name is 'page' |
| 346 |
)) : null, |
| 347 |
); |
| 348 |
|
| 349 |
$queries[ $key ] = $results; |
| 350 |
} else { |
| 351 |
$results = $queries[ $key ]; |
| 352 |
} |
| 353 |
|
| 354 |
return ! $include_total ? $results['items'] : $results; |
| 355 |
} |
| 356 |
|
| 357 |
/** |
| 358 |
* Validates custom HTML tags |
| 359 |
* |
| 360 |
* @param string $tagsString String of tags separated by commas |
| 361 |
* @return array Valid tags |
| 362 |
*/ |
| 363 |
public static function validateCustomTags($tagsString) |
| 364 |
{ |
| 365 |
if (empty($tagsString)) { |
| 366 |
return array(); |
| 367 |
} |
| 368 |
|
| 369 |
$tags = array_map('trim', explode(',', $tagsString)); |
| 370 |
$validTags = array(); |
| 371 |
|
| 372 |
foreach ($tags as $tag) { |
| 373 |
// Clean the tag |
| 374 |
$tag = preg_replace('/[^a-zA-Z0-9\-_]/', '', $tag); |
| 375 |
|
| 376 |
// Check that the tag is not empty and starts with a letter |
| 377 |
if (!empty($tag) && preg_match('/^[a-zA-Z]/', $tag)) { |
| 378 |
$validTags[] = strtolower($tag); |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
return array_unique($validTags); |
| 383 |
} |
| 384 |
|
| 385 |
/** |
| 386 |
* Validates custom HTML attributes |
| 387 |
* |
| 388 |
* @param string $attrsString String of attributes separated by commas |
| 389 |
* @return array Valid attributes |
| 390 |
*/ |
| 391 |
public static function validateCustomAttrs($attrsString) |
| 392 |
{ |
| 393 |
if (empty($attrsString)) { |
| 394 |
return array(); |
| 395 |
} |
| 396 |
|
| 397 |
$attrs = array_map('trim', explode(',', $attrsString)); |
| 398 |
$validAttrs = array(); |
| 399 |
|
| 400 |
foreach ($attrs as $attr) { |
| 401 |
// Clean the attribute |
| 402 |
$attr = preg_replace('/[^a-zA-Z0-9\-_:]/', '', $attr); |
| 403 |
|
| 404 |
// Check that the attribute is not empty and starts with a letter |
| 405 |
if (!empty($attr) && preg_match('/^[a-zA-Z]/', $attr)) { |
| 406 |
$validAttrs[] = strtolower($attr); |
| 407 |
} |
| 408 |
} |
| 409 |
|
| 410 |
return array_unique($validAttrs); |
| 411 |
} |
| 412 |
} |
| 413 |
|