templates
2 months ago
DataHelper.php
2 months ago
ExceptionalElements.php
1 month ago
Preview.php
1 month ago
Utils.php
1 month ago
Utils.php
374 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Preivew scripts data and styles helper |
| 5 | * |
| 6 | * @package kirki |
| 7 | */ |
| 8 | |
| 9 | namespace Kirki\Frontend\Preview; |
| 10 | |
| 11 | use Kirki\Ajax\Users; |
| 12 | use Kirki\API\ContentManager\ContentManagerHelper; |
| 13 | use Kirki\HelperFunctions; |
| 14 | |
| 15 | if ( ! defined( 'ABSPATH' ) ) { |
| 16 | exit; // Exit if accessed directly. |
| 17 | } |
| 18 | |
| 19 | /** |
| 20 | * DataHelper Class |
| 21 | */ |
| 22 | class Utils { |
| 23 | |
| 24 | public static function get_items_data_from_dynamic_contents( $dynamic_content, $options ) { |
| 25 | $collection_type = $dynamic_content['collectionType']; |
| 26 | if ( $collection_type === 'users' ) { |
| 27 | return self::get_users_collection( $dynamic_content, $options ); |
| 28 | } elseif ( $collection_type === 'posts' ) { |
| 29 | return self::get_posts_collection( $dynamic_content, $options ); |
| 30 | } elseif ( $collection_type === 'terms' ) { |
| 31 | return self::get_terms_collection( $dynamic_content, $options ); |
| 32 | } else { |
| 33 | $args = self::get_common_args( $dynamic_content, $options ); |
| 34 | $collection = apply_filters( 'kirki_collection_' . $dynamic_content['collectionType'], false, $args ); |
| 35 | $data = $collection ? $collection['data'] : array(); |
| 36 | $pagination = $collection ? $collection['pagination'] : array(); |
| 37 | $item_type = $collection && isset( $collection['itemType'] ) ? $collection['itemType'] : 'post'; |
| 38 | return array( |
| 39 | 'data' => $data, |
| 40 | 'pagination' => $pagination, |
| 41 | 'itemType' => $item_type, |
| 42 | ); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | private static function get_posts_collection( $dynamic_content, $options ) { |
| 47 | $args = self::get_common_args( $dynamic_content, $options ); |
| 48 | $collection = HelperFunctions::get_posts( $args ); |
| 49 | $data = $collection['data']; |
| 50 | $pagination = $collection['pagination']; |
| 51 | return array( |
| 52 | 'data' => $data, |
| 53 | 'pagination' => $pagination, |
| 54 | 'itemType' => 'post', |
| 55 | ); |
| 56 | } |
| 57 | private static function get_terms_collection( $dynamic_content, $options ) { |
| 58 | $args = array( |
| 59 | 'taxonomy' => $dynamic_content['taxonomy'], |
| 60 | 'hide_empty' => false, |
| 61 | 'current_page' => isset( $options['page'] ) ? $options['page'] : 1, |
| 62 | 'item_per_page' => isset( $dynamic_content['items'] ) ? $dynamic_content['items'] : 3, |
| 63 | 'offset' => isset( $dynamic_content['offset'] ) ? $dynamic_content['offset'] : 0, |
| 64 | ); |
| 65 | |
| 66 | if ( isset( $dynamic_content['inherit'] ) && $dynamic_content['inherit'] ) { |
| 67 | if ( isset( $options['itemType'] ) && $options['itemType'] === 'post' ) { |
| 68 | $post_parent = $options['post']->ID; |
| 69 | } else { |
| 70 | $post_parent = HelperFunctions::get_post_id_if_possible_from_url(); |
| 71 | } |
| 72 | $args['post_parent'] = $post_parent; |
| 73 | $args['inherit'] = true; |
| 74 | } |
| 75 | |
| 76 | $terms = HelperFunctions::get_terms( $args ); |
| 77 | |
| 78 | $data = $terms['data']; |
| 79 | $pagination = $terms['pagination']; |
| 80 | return array( |
| 81 | 'data' => $data, |
| 82 | 'pagination' => $pagination, |
| 83 | 'itemType' => 'term', |
| 84 | ); |
| 85 | } |
| 86 | |
| 87 | private static function get_users_collection( $dynamic_content, $options ) { |
| 88 | $args = array(); |
| 89 | |
| 90 | if ( isset( $dynamic_content['sorting'], $dynamic_content['sorting']['type'], $dynamic_content['sorting']['value'] ) ) { |
| 91 | $args['sorting']['order'] = $dynamic_content['sorting']['type']; |
| 92 | $args['sorting']['orderby'] = $dynamic_content['sorting']['value']; |
| 93 | } |
| 94 | |
| 95 | if ( isset( $options['filters'] ) ) { |
| 96 | $args['filters'] = $options['filters']; |
| 97 | } elseif ( isset( $dynamic_content['filters'] ) ) { |
| 98 | $args['filters'] = $dynamic_content['filters']; |
| 99 | } |
| 100 | |
| 101 | if ( isset( $dynamic_content['inherit'] ) && $dynamic_content['inherit'] === true ) { |
| 102 | $args['inherit'] = true; |
| 103 | $args['post_parent'] = $options['post']->ID ?? HelperFunctions::get_post_id_if_possible_from_url(); |
| 104 | } |
| 105 | |
| 106 | $item_per_page = isset( $dynamic_content['items'] ) ? $dynamic_content['items'] : 3; |
| 107 | $offset = isset( $dynamic_content['offset'] ) ? $dynamic_content['offset'] : 0; |
| 108 | |
| 109 | $current_page = 1; |
| 110 | if ( isset( $options['page'] ) ) { |
| 111 | $current_page = $options['page']; |
| 112 | } |
| 113 | |
| 114 | $args['current_page'] = $current_page; |
| 115 | $args['item_per_page'] = $item_per_page; |
| 116 | $args['offset'] = $offset; |
| 117 | |
| 118 | if ( isset( $options['q'] ) ) { |
| 119 | $args['q'] = $options['q']; |
| 120 | } |
| 121 | |
| 122 | $users = Users::get_users( $args ); |
| 123 | $data = $users['data']; |
| 124 | $pagination = $users['pagination']; |
| 125 | |
| 126 | return array( |
| 127 | 'data' => $data, |
| 128 | 'pagination' => $pagination, |
| 129 | 'itemType' => 'user', |
| 130 | ); |
| 131 | } |
| 132 | |
| 133 | |
| 134 | private static function get_common_args( $dynamic_content, $options ) { |
| 135 | $args = array(); |
| 136 | if ( isset( $dynamic_content['sorting'], $dynamic_content['sorting']['type'], $dynamic_content['sorting']['value'] ) ) { |
| 137 | $args['sorting']['order'] = $dynamic_content['sorting']['type']; |
| 138 | $args['sorting']['orderby'] = $dynamic_content['sorting']['value']; |
| 139 | } |
| 140 | |
| 141 | if ( isset( $options['filters'] ) ) { |
| 142 | $args['filters'] = $options['filters']; |
| 143 | } elseif ( isset( $dynamic_content['filters'] ) ) { |
| 144 | $args['filters'] = $dynamic_content['filters']; |
| 145 | } |
| 146 | |
| 147 | $search_related_collection_ids = isset( $options['search_related_collection_ids'] ) ? $options['search_related_collection_ids'] : array(); |
| 148 | $is_searchable_collection_element = isset($search_related_collection_ids[$options['element_id']]); |
| 149 | |
| 150 | if ( ! isset( $dynamic_content['relatedCollection'] ) && $is_searchable_collection_element ) { |
| 151 | |
| 152 | // This code is for taxonomy filter START |
| 153 | $filters = array(); |
| 154 | $taxonomies = array(); |
| 155 | if ( isset( $_GET['taxonomy'] ) && is_array( $_GET['taxonomy'] ) ) { |
| 156 | $taxonomies = $_GET['taxonomy']; |
| 157 | } elseif ( isset( $options['collection_param_filters'], $options['collection_param_filters']['taxonomy'] ) ) { |
| 158 | // This code came from pagination, filter, search api from frontend |
| 159 | $taxonomies = $options['collection_param_filters']['taxonomy']; |
| 160 | } |
| 161 | foreach ( $taxonomies as $name => $value ) { |
| 162 | if ( ! empty( $value ) ) { |
| 163 | $filters[] = array( |
| 164 | 'id' => 'taxonomy', |
| 165 | 'type' => 'taxonomy', |
| 166 | 'taxonomy' => $name, |
| 167 | 'terms' => $value, |
| 168 | ); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | $args['filters'] = array_merge( $args['filters'], $filters ); |
| 173 | // This code is for taxonomy filter END |
| 174 | |
| 175 | // This code is for post/cm filter START |
| 176 | $ref_post_ids = array(); |
| 177 | if ( isset( $_GET['post'], $_GET['post']['cm_post'] ) && is_array( $_GET['post']['cm_post'] ) ) { |
| 178 | $ref_post_ids = array_map( 'intval', $_GET['post']['cm_post'] ); |
| 179 | } elseif ( isset( $options['collection_param_filters'], $options['collection_param_filters']['post'], $options['collection_param_filters']['post']['cm_post'] ) ) { |
| 180 | // This code came from pagination, filter, search api from frontend |
| 181 | $ref_post_ids = $options['collection_param_filters']['post']['cm_post']; |
| 182 | } |
| 183 | |
| 184 | if ( ! empty( $ref_post_ids ) ) { |
| 185 | |
| 186 | $included_post_ids = ContentManagerHelper::get_post_ids_by_ref_post_ids( $ref_post_ids ); |
| 187 | |
| 188 | $args['IDs'] = $included_post_ids; |
| 189 | // Make sure all post ids are integers |
| 190 | } |
| 191 | // This code is for post/cm filter END |
| 192 | } |
| 193 | |
| 194 | if ( isset( $dynamic_content['type'] ) ) { |
| 195 | $args['name'] = $dynamic_content['type']; |
| 196 | } |
| 197 | |
| 198 | if ( isset( $dynamic_content['inherit'] ) && $dynamic_content['inherit'] === true ) { |
| 199 | $args['inherit'] = true; |
| 200 | |
| 201 | if ( isset( $options['itemType'], $options[ $options['itemType'] ], $options[ $options['itemType'] ]->ID ) ) { |
| 202 | // if itemType is set and it has ID property then use it as post_parent |
| 203 | $args['post_parent'] = $options[ $options['itemType'] ]->ID; |
| 204 | } else { |
| 205 | $args['post_parent'] = $options['post']->ID ?? HelperFunctions::get_post_id_if_possible_from_url(); |
| 206 | } |
| 207 | |
| 208 | if ( isset( $options['user'], $options['user']['ID'] ) ) { |
| 209 | // update $filter data with this user author in property |
| 210 | |
| 211 | $args['context'] = array( |
| 212 | 'id' => $options['user']['ID'], |
| 213 | 'collectionType' => 'user', |
| 214 | ); |
| 215 | } |
| 216 | if ( isset( $options['term'] ) ) { |
| 217 | // update $filter data with this term in property |
| 218 | $args['context'] = array( |
| 219 | 'id' => $options['term']['term_id'], |
| 220 | 'slug' => $options['term']['slug'], |
| 221 | 'taxonomy' => $options['term']['taxonomy'], |
| 222 | 'collectionType' => 'term', |
| 223 | ); |
| 224 | } |
| 225 | |
| 226 | if ( isset( $options['comment'] ) ) { |
| 227 | $comment = $options['comment']; |
| 228 | |
| 229 | if ( isset( $comment['id'] ) ) { |
| 230 | |
| 231 | $args['context'] = array( |
| 232 | 'comment_ID' => $comment['id'], // comment_ID |
| 233 | ); |
| 234 | } elseif ( isset( $comment['comment_ID'] ) ) { |
| 235 | $args['context'] = array( |
| 236 | 'comment_ID' => $comment['comment_ID'], // comment_ID |
| 237 | ); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | // for external plugin support |
| 242 | if ( isset( $options['itemType'], $options[ $options['itemType'] ] ) ) { |
| 243 | $args['parent_item_type'] = $options['itemType']; |
| 244 | $args['parent_item'] = $options[ $options['itemType'] ]; |
| 245 | } |
| 246 | } |
| 247 | if ( isset( $dynamic_content['related'] ) && $dynamic_content['related'] === true ) { |
| 248 | $args['related'] = true; |
| 249 | $args['related_post_parent'] = $options['post']->ID ?? HelperFunctions::get_post_id_if_possible_from_url(); |
| 250 | } |
| 251 | |
| 252 | if ( ! empty( $dynamic_content['type'] ) && false !== strpos( $dynamic_content['type'], KIRKI_CONTENT_MANAGER_PREFIX ) ) { |
| 253 | $parent_id = str_replace( KIRKI_CONTENT_MANAGER_PREFIX . '_', '', $dynamic_content['type'] ); |
| 254 | $args['post_parent'] = $parent_id; |
| 255 | } |
| 256 | |
| 257 | $item_per_page = isset( $dynamic_content['items'] ) ? $dynamic_content['items'] : 3; |
| 258 | $offset = isset( $dynamic_content['offset'] ) ? $dynamic_content['offset'] : 0; |
| 259 | |
| 260 | $current_page = 1; |
| 261 | if ( isset( $options['page'] ) ) { |
| 262 | $current_page = $options['page']; |
| 263 | } |
| 264 | |
| 265 | $args['current_page'] = $current_page; |
| 266 | $args['item_per_page'] = $item_per_page; |
| 267 | $args['offset'] = $offset; |
| 268 | |
| 269 | // if options has query value |
| 270 | if ( isset( $options['q'] ) ) { |
| 271 | $args['q'] = $options['q']; |
| 272 | } |
| 273 | |
| 274 | if(empty($args['q'])){ |
| 275 | $args['q'] = HelperFunctions::sanitize_text(isset($_REQUEST['q']) ?$_REQUEST['q']:'' ); |
| 276 | } |
| 277 | |
| 278 | if( (isset($options['inside_collection']) && $options['inside_collection'] === true) || !$is_searchable_collection_element ) { |
| 279 | // this is for nested collection like: terms, multiref etc |
| 280 | $args['current_page'] = 1; |
| 281 | $args['pagination'] = false; |
| 282 | $args['q'] = ''; |
| 283 | } |
| 284 | |
| 285 | return $args; |
| 286 | } |
| 287 | |
| 288 | public static function getDynamicRichTextValue( $dynamic_content, $options ) { |
| 289 | $html = ''; |
| 290 | if ( ! $dynamic_content ) { |
| 291 | return $html; |
| 292 | } |
| 293 | // Post excerpt length for frontend. If post excerpt is used. |
| 294 | if (isset($dynamic_content['value']) && $dynamic_content['value'] === 'post_excerpt' && isset( $dynamic_content['postExcerptLength'] ) ) { |
| 295 | $post_excerpt_length = $dynamic_content['postExcerptLength'] ?? 55; |
| 296 | $GLOBALS['kirki_post_excerpt_length'] = $post_excerpt_length; |
| 297 | } |
| 298 | |
| 299 | $contentInfo = array( |
| 300 | 'dynamicContent' => $dynamic_content, |
| 301 | 'options' => $options, |
| 302 | ); |
| 303 | |
| 304 | if ( isset( $options['itemType'], $options[ $options['itemType'] ], $options[ $options['itemType'] ]->ID ) ) { |
| 305 | $contentInfo['collectionItem'] = array( |
| 306 | 'ID' => $options[ $options['itemType'] ]->ID, |
| 307 | ); |
| 308 | } |
| 309 | $content = apply_filters( 'kirki_dynamic_content', false, $contentInfo ); |
| 310 | if ( $content ) { |
| 311 | return $content; |
| 312 | } |
| 313 | |
| 314 | // fix warning |
| 315 | if(!isset($dynamic_content['value'])){ |
| 316 | $dynamic_content['value'] = false; |
| 317 | } |
| 318 | |
| 319 | if ( $dynamic_content['type'] === 'post' ) { |
| 320 | $dynamic_options = array(); |
| 321 | $cm_field_type = isset( $dynamic_content['cmFieldType'] ) ? $dynamic_content['cmFieldType'] : ''; |
| 322 | if ( isset( $dynamic_content['format'] ) && 'post_date' === $dynamic_content['value'] ) { |
| 323 | $dynamic_options['format'] = $dynamic_content['format']; |
| 324 | } elseif ( ( 'post_time' === $dynamic_content['value'] || 'time' === $cm_field_type ) && isset( $dynamic_content['timeFormat'] ) ) { |
| 325 | $dynamic_options['timeFormat'] = $dynamic_content['timeFormat']; |
| 326 | } |
| 327 | |
| 328 | $content = HelperFunctions::get_post_dynamic_content( |
| 329 | $dynamic_content['value'], |
| 330 | $options['post'] ?? null, |
| 331 | $dynamic_content['meta'] ?? '', |
| 332 | $dynamic_options |
| 333 | ); |
| 334 | |
| 335 | $html .= $content; |
| 336 | } elseif ( $dynamic_content['type'] === 'term' && isset( $options['term'] ) && isset( $options['term'][ $dynamic_content['value'] ] ) ) { |
| 337 | $html .= $options['term'][ $dynamic_content['value'] ]; |
| 338 | } elseif ( $dynamic_content['type'] === 'user' ) { |
| 339 | // $html .= $options['user'][ $dynamic_content['value'] ]; |
| 340 | $content = HelperFunctions::get_user_dynamic_content( |
| 341 | $dynamic_content['value'], |
| 342 | $options['user']['ID'] ?? null, |
| 343 | $dynamic_content['meta'] ?? '' |
| 344 | ); |
| 345 | |
| 346 | // Date may need to format |
| 347 | if ( 'registered_date' === $dynamic_content['value'] && isset( $dynamic_content['format'] ) ) { |
| 348 | $content = HelperFunctions::format_date( $content, $dynamic_content['format'] ); |
| 349 | } |
| 350 | |
| 351 | $html .= $content; |
| 352 | |
| 353 | } elseif ( $dynamic_content['type'] === 'menu' && isset( $options['menu'] ) && isset( $options['menu']->{$dynamic_content['value']} ) ) { |
| 354 | $html .= $options['menu']->{$dynamic_content['value']}; |
| 355 | } elseif ( $dynamic_content['type'] === 'others' && isset( $dynamic_content['value'] ) ) { |
| 356 | // Post count is used in collection item index. |
| 357 | if ( $dynamic_content['value'] === 'item_index' && isset( $options['item_index'] ) ) { |
| 358 | $content = $options['item_index']; |
| 359 | |
| 360 | $html .= $content; |
| 361 | } |
| 362 | } else { |
| 363 | $item_type = $dynamic_content['type']; |
| 364 | if ( isset( $options[ $item_type ] ) ) { |
| 365 | $data = $options[ $item_type ] ?? array(); |
| 366 | $value = isset( $data[ $dynamic_content['value'] ] ) ? $data[ $dynamic_content['value'] ] : ''; |
| 367 | $html .= $value; |
| 368 | } |
| 369 | } |
| 370 | |
| 371 | return $html; |
| 372 | } |
| 373 | } |
| 374 |