Collaboration
3 weeks ago
Apps.php
3 weeks ago
Collection.php
1 month ago
Comments.php
2 months ago
DynamicContent.php
1 month ago
ExportImport.php
3 days ago
Form.php
3 weeks ago
Media.php
3 days ago
Page.php
3 days ago
PageSettings.php
3 weeks ago
RBAC.php
3 weeks ago
Symbol.php
3 weeks ago
Taxonomy.php
2 months ago
TemplateExportImport.php
2 months ago
UserData.php
3 weeks ago
Users.php
2 months ago
Walkthrough.php
2 months ago
WordpressData.php
2 months ago
WpAdmin.php
3 days ago
Collection.php
209 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Dynamic content/Collection API |
| 4 | * |
| 5 | * @package kirki |
| 6 | */ |
| 7 | |
| 8 | namespace Kirki\Ajax; |
| 9 | |
| 10 | if ( ! defined( 'ABSPATH' ) ) { |
| 11 | exit; // Exit if accessed directly. |
| 12 | } |
| 13 | |
| 14 | use Kirki\HelperFunctions; |
| 15 | |
| 16 | /** |
| 17 | * Collection API Class |
| 18 | */ |
| 19 | class Collection { |
| 20 | |
| 21 | /** |
| 22 | * Get collection return api response |
| 23 | * |
| 24 | * @return void wpjson response |
| 25 | */ |
| 26 | public static function get_collection() { |
| 27 | //phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 28 | $collection_type = HelperFunctions::sanitize_text( isset( $_GET['collectionType'] ) ? $_GET['collectionType'] : null ); |
| 29 | $name = HelperFunctions::sanitize_text( isset( $_GET['name'] ) ? $_GET['name'] : null ); |
| 30 | $taxonomy = HelperFunctions::sanitize_text( isset( $_GET['taxonomy'] ) ? $_GET['taxonomy'] : null ); |
| 31 | //phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 32 | $sorting_param = HelperFunctions::sanitize_text( isset( $_GET['sorting'] ) ? $_GET['sorting'] : null ); |
| 33 | //phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 34 | $filter_param = HelperFunctions::sanitize_text( isset( $_GET['filters'] ) ? $_GET['filters'] : null ); |
| 35 | $inherit = HelperFunctions::sanitize_text( $_GET['inherit'] ?? false ); |
| 36 | $related = HelperFunctions::sanitize_text( $_GET['related'] ?? false ); |
| 37 | $post_parent = HelperFunctions::sanitize_text( $_GET['post_parent'] ?? null ); |
| 38 | $related_post_parent = HelperFunctions::sanitize_text( $_GET['related_post_parent'] ?? false ); |
| 39 | $item_per_page = HelperFunctions::sanitize_text( $_GET['items'] ?? 3 ); |
| 40 | $current_page = HelperFunctions::sanitize_text( $_GET['current_page'] ?? 1 ); |
| 41 | $offset = HelperFunctions::sanitize_text( $_GET['offset'] ?? 0 ); |
| 42 | $context_param = HelperFunctions::sanitize_text( isset( $_GET['context'] ) ? $_GET['context'] : null ); |
| 43 | $query = HelperFunctions::sanitize_text( isset( $_GET['q'] ) ? $_GET['q'] : '' ); |
| 44 | |
| 45 | $data = self::resolve_collection( |
| 46 | $collection_type, |
| 47 | $name, |
| 48 | $taxonomy, |
| 49 | $sorting_param, |
| 50 | $filter_param, |
| 51 | $inherit, |
| 52 | $related, |
| 53 | $post_parent, |
| 54 | $related_post_parent, |
| 55 | $item_per_page, |
| 56 | $current_page, |
| 57 | $offset, |
| 58 | $context_param, |
| 59 | $query |
| 60 | ); |
| 61 | |
| 62 | wp_send_json( $data ); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Resolve collection data. |
| 67 | * |
| 68 | * @return mixed |
| 69 | */ |
| 70 | private static function resolve_collection( |
| 71 | $collection_type, |
| 72 | $name, |
| 73 | $taxonomy, |
| 74 | $sorting_param, |
| 75 | $filter_param, |
| 76 | $inherit, |
| 77 | $related, |
| 78 | $post_parent, |
| 79 | $related_post_parent, |
| 80 | $item_per_page, |
| 81 | $current_page, |
| 82 | $offset, |
| 83 | $context_param, |
| 84 | $query |
| 85 | ) { |
| 86 | $sorting = null; |
| 87 | $filters = null; |
| 88 | $context = null; |
| 89 | |
| 90 | // handle bool |
| 91 | $inherit = $inherit === 'true' ? true : false; |
| 92 | $related = $related === 'true' ? true : false; |
| 93 | |
| 94 | if ( isset( $sorting_param ) ) { |
| 95 | $sorting = $sorting_param; |
| 96 | } |
| 97 | |
| 98 | if ( isset( $filter_param ) ) { |
| 99 | $filters = $filter_param; |
| 100 | } |
| 101 | |
| 102 | if ( isset( $context_param ) ) { |
| 103 | $context = $context_param; |
| 104 | } |
| 105 | |
| 106 | $args = array( |
| 107 | 'name' => $name, |
| 108 | 'sorting' => $sorting, |
| 109 | 'filters' => $filters, |
| 110 | 'inherit' => $inherit, |
| 111 | 'post_parent' => $post_parent, |
| 112 | 'post_status' => 'any', |
| 113 | 'related' => $related, |
| 114 | 'related_post_parent' => $related_post_parent, |
| 115 | 'item_per_page' => $item_per_page, |
| 116 | 'current_page' => $current_page, |
| 117 | 'offset' => $offset, |
| 118 | 'context' => $context, |
| 119 | 'q' => $query, |
| 120 | ); |
| 121 | |
| 122 | if ( $collection_type === 'posts' ) { |
| 123 | return HelperFunctions::get_posts( $args ); |
| 124 | } elseif ( $collection_type === 'users' ) { |
| 125 | return Users::get_users( $args ); |
| 126 | } elseif ( $collection_type === 'terms' ) { |
| 127 | $args = array( |
| 128 | 'taxonomy' => $taxonomy, |
| 129 | 'hide_empty' => false, |
| 130 | 'inherit' => $inherit, |
| 131 | 'post_parent' => $post_parent, |
| 132 | 'item_per_page' => $item_per_page, |
| 133 | 'current_page' => $current_page, |
| 134 | 'offset' => $offset, |
| 135 | ); |
| 136 | return HelperFunctions::get_terms( $args ); |
| 137 | } else { |
| 138 | return apply_filters( 'kirki_collection_' . $collection_type, false, $args ); |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Get batched collection data. |
| 144 | * |
| 145 | * @return void wpjson response |
| 146 | */ |
| 147 | public static function get_collection_batch() { //phpcs:ignore WordPress.Security.NonceVerification.Missing,WordPress.Security.NonceVerification.Recommended,WordPress.Security.ValidatedSanitizedInput.MissingUnslash,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 148 | $items = HelperFunctions::sanitize_text( isset( $_POST['items'] ) ? $_POST['items'] : null ); |
| 149 | $items = json_decode( stripslashes( $items ), true ); |
| 150 | |
| 151 | if ( ! is_array( $items ) ) { |
| 152 | wp_send_json( array() ); |
| 153 | } |
| 154 | |
| 155 | $results = array(); |
| 156 | |
| 157 | foreach ( $items as $item ) { |
| 158 | $id = isset( $item['id'] ) ? $item['id'] : ''; |
| 159 | $payload = isset( $item['payload'] ) ? $item['payload'] : array(); |
| 160 | |
| 161 | $data = self::resolve_collection( |
| 162 | isset( $payload['collectionType'] ) ? $payload['collectionType'] : null, |
| 163 | isset( $payload['name'] ) ? $payload['name'] : null, |
| 164 | isset( $payload['taxonomy'] ) ? $payload['taxonomy'] : null, |
| 165 | isset( $payload['sorting'] ) ? $payload['sorting'] : null, |
| 166 | isset( $payload['filters'] ) ? $payload['filters'] : null, |
| 167 | isset( $payload['inherit'] ) ? $payload['inherit'] : false, |
| 168 | isset( $payload['related'] ) ? $payload['related'] : false, |
| 169 | isset( $payload['post_parent'] ) ? $payload['post_parent'] : null, |
| 170 | isset( $payload['related_post_parent'] ) ? $payload['related_post_parent'] : false, |
| 171 | isset( $payload['items'] ) ? $payload['items'] : 3, |
| 172 | isset( $payload['current_page'] ) ? $payload['current_page'] : 1, |
| 173 | isset( $payload['offset'] ) ? $payload['offset'] : 0, |
| 174 | isset( $payload['context'] ) ? $payload['context'] : null, |
| 175 | isset( $payload['q'] ) ? $payload['q'] : '' |
| 176 | ); |
| 177 | |
| 178 | $results[] = array( |
| 179 | 'id' => $id, |
| 180 | 'data' => $data, |
| 181 | ); |
| 182 | } |
| 183 | |
| 184 | wp_send_json( $results ); |
| 185 | } |
| 186 | |
| 187 | public static function get_external_collection_options() { |
| 188 | $collection_type = HelperFunctions::sanitize_text( isset( $_GET['collectionType'] ) ? $_GET['collectionType'] : null ); |
| 189 | $type = HelperFunctions::sanitize_text( isset( $_GET['type'] ) ? $_GET['type'] : null ); |
| 190 | |
| 191 | $args = array( |
| 192 | 'type' => $type, |
| 193 | 'collectionType' => $collection_type, |
| 194 | ); |
| 195 | |
| 196 | $collection = array(); |
| 197 | $collection = apply_filters( 'kirki_external_collection_options', $collection, $args ); |
| 198 | |
| 199 | wp_send_json( $collection ); |
| 200 | } |
| 201 | |
| 202 | public static function get_external_collection_item_type() { |
| 203 | $type = HelperFunctions::sanitize_text( isset( $_GET['type'] ) ? $_GET['type'] : null ); |
| 204 | $item_type = 'post'; |
| 205 | $item_type = apply_filters( 'kirki_external_collection_item_type', $item_type, $type ); |
| 206 | wp_send_json( $item_type ); |
| 207 | } |
| 208 | } |
| 209 |