Admin
1 month ago
Api
1 month ago
Blocks
2 months ago
Hooks
1 month ago
AjaxController.php
1 month ago
BlocksController.php
1 month ago
DiviController.php
1 year ago
ElementorController.php
1 year ago
GutenBergController.php
1 month ago
PageTemplateController.php
2 years ago
ScriptController.php
1 month ago
ShortcodeController.php
5 months ago
WidgetController.php
2 years ago
AjaxController.php
390 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Ajax Controller class. |
| 4 | * |
| 5 | * @package RT_TPG |
| 6 | */ |
| 7 | |
| 8 | namespace RT\ThePostGrid\Controllers; |
| 9 | |
| 10 | use RT\ThePostGrid\Helpers\Fns; |
| 11 | use RT\ThePostGrid\Helpers\Options; |
| 12 | |
| 13 | //phpcs:disable WordPress.Security.NonceVerification.Recommended |
| 14 | |
| 15 | // Do not allow directly accessing this file. |
| 16 | if ( ! defined( 'ABSPATH' ) ) { |
| 17 | exit( 'This script cannot be accessed directly.' ); |
| 18 | } |
| 19 | |
| 20 | /** |
| 21 | * Ajax Controller class. |
| 22 | */ |
| 23 | class AjaxController { |
| 24 | /** |
| 25 | * Class constructor |
| 26 | */ |
| 27 | public function __construct() { |
| 28 | add_action( 'wp_ajax_rtTPGSettings', [ $this, 'rtTPGSaveSettings' ] ); |
| 29 | add_action( 'wp_ajax_rtTPGShortCodeList', [ $this, 'shortCodeList' ] ); |
| 30 | add_action( 'wp_ajax_rtTPGTaxonomyListByPostType', [ $this, 'rtTPGTaxonomyListByPostType' ] ); |
| 31 | add_action( 'wp_ajax_rtTPGIsotopeFilter', [ $this, 'rtTPGIsotopeFilter' ] ); |
| 32 | add_action( 'wp_ajax_rtTPGTermListByTaxonomy', [ $this, 'rtTPGTermListByTaxonomy' ] ); |
| 33 | add_action( 'wp_ajax_defaultFilterItem', [ $this, 'defaultFilterItem' ] ); |
| 34 | add_action( 'wp_ajax_getCfGroupListAsField', [ $this, 'getCfGroupListAsField' ] ); |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * Render |
| 39 | * |
| 40 | * @return void |
| 41 | */ |
| 42 | public function getCfGroupListAsField() { |
| 43 | $error = true; |
| 44 | $data = $msg = null; |
| 45 | $is_ok = true; |
| 46 | if ( ! current_user_can( 'edit_posts' ) ) { |
| 47 | $is_ok = false; |
| 48 | } |
| 49 | if ( ! Fns::verifyNonce() ) { |
| 50 | $is_ok = false; |
| 51 | } |
| 52 | if ( $is_ok ) { |
| 53 | $fields = []; |
| 54 | $post_type = isset( $_REQUEST['post_type'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['post_type'] ) ) : null; |
| 55 | |
| 56 | if ( $cf = Fns::is_acf() && $post_type ) { |
| 57 | $fields['cf_group'] = [ |
| 58 | 'type' => 'checkbox', |
| 59 | 'name' => 'cf_group', |
| 60 | 'holderClass' => 'tpg-hidden cf-fields cf-group', |
| 61 | 'label' => esc_html__( 'Custom Field group', 'the-post-grid' ), |
| 62 | 'multiple' => true, |
| 63 | 'alignment' => 'vertical', |
| 64 | 'id' => 'cf_group', |
| 65 | 'options' => Fns::get_groups_by_post_type( $post_type, $cf ), |
| 66 | ]; |
| 67 | $error = false; |
| 68 | $data = Fns::rtFieldGenerator( $fields ); |
| 69 | } |
| 70 | } else { |
| 71 | $msg = esc_html__( 'Server Error !!', 'the-post-grid' ); |
| 72 | } |
| 73 | |
| 74 | $response = [ |
| 75 | 'error' => $error, |
| 76 | 'msg' => $msg, |
| 77 | 'data' => $data, |
| 78 | ]; |
| 79 | |
| 80 | wp_send_json( $response ); |
| 81 | die(); |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Default filter. |
| 86 | * |
| 87 | * @return void |
| 88 | */ |
| 89 | public function defaultFilterItem() { |
| 90 | $error = true; |
| 91 | $data = $msg = null; |
| 92 | $is_ok = true; |
| 93 | if ( ! current_user_can( 'edit_posts' ) ) { |
| 94 | $is_ok = false; |
| 95 | } |
| 96 | if ( ! Fns::verifyNonce() ) { |
| 97 | $is_ok = false; |
| 98 | } |
| 99 | if ( $is_ok ) { |
| 100 | $filter = isset( $_REQUEST['filter'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['filter'] ) ) : null; |
| 101 | $term = isset( $_REQUEST['include'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['include'] ) ) : null; |
| 102 | |
| 103 | if ( ! empty( $filter ) ) { |
| 104 | $include = []; |
| 105 | |
| 106 | if ( ! empty( $term ) ) { |
| 107 | $include = explode( ',', $term ); |
| 108 | } |
| 109 | |
| 110 | $error = false; |
| 111 | $msg = esc_html__( 'Success', 'the-post-grid' ); |
| 112 | $data .= "<option value=''>" . esc_html__( 'Show All', 'the-post-grid' ) . '</option>'; |
| 113 | $items = Fns::rt_get_selected_term_by_taxonomy( $filter, $include, '', 0 ); |
| 114 | |
| 115 | if ( ! empty( $items ) ) { |
| 116 | foreach ( $items as $id => $item ) { |
| 117 | $data .= '<option value="' . absint( $id ) . '">' . esc_html( $item ) . '</option>'; |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | } else { |
| 122 | $msg = esc_html__( 'Session Error !!', 'the-post-grid' ); |
| 123 | } |
| 124 | $response = [ |
| 125 | 'error' => $error, |
| 126 | 'msg' => $msg, |
| 127 | 'data' => $data, |
| 128 | ]; |
| 129 | |
| 130 | wp_send_json( $response ); |
| 131 | die(); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Save settings. |
| 136 | * |
| 137 | * @return void |
| 138 | */ |
| 139 | public function rtTPGSaveSettings() { |
| 140 | $error = true; |
| 141 | $userId = get_current_user_id(); |
| 142 | |
| 143 | if ( $userId != $_REQUEST['uid'] ) { |
| 144 | wp_send_json( |
| 145 | [ |
| 146 | 'error' => true, |
| 147 | 'msg' => esc_html__( 'You are not a valid user to modification the options.', 'the-post-grid' ), |
| 148 | ] |
| 149 | ); |
| 150 | die(); |
| 151 | } |
| 152 | |
| 153 | if ( ! ( current_user_can( 'manage_options' ) ) ) { |
| 154 | wp_send_json( |
| 155 | [ |
| 156 | 'error' => true, |
| 157 | 'msg' => esc_html__( 'You have no permission to modification.', 'the-post-grid' ), |
| 158 | ] |
| 159 | ); |
| 160 | die(); |
| 161 | } |
| 162 | |
| 163 | if ( Fns::verifyNonce() ) { |
| 164 | unset( $_REQUEST['action'] ); |
| 165 | unset( $_REQUEST[ rtTPG()->nonceId() ] ); |
| 166 | unset( $_REQUEST['_wp_http_referer'] ); |
| 167 | unset( $_REQUEST['uid'] ); |
| 168 | |
| 169 | update_option( rtTPG()->options['settings'], wp_unslash( $_REQUEST ) ); |
| 170 | |
| 171 | $response = [ |
| 172 | 'error' => false, |
| 173 | 'msg' => esc_html__( 'Settings successfully updated', 'the-post-grid' ), |
| 174 | ]; |
| 175 | } else { |
| 176 | $response = [ |
| 177 | 'error' => $error, |
| 178 | 'msg' => esc_html__( 'Session Error !!', 'the-post-grid' ), |
| 179 | ]; |
| 180 | } |
| 181 | |
| 182 | wp_send_json( $response ); |
| 183 | die(); |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Taxonomy list. |
| 188 | * |
| 189 | * @return void |
| 190 | */ |
| 191 | public function rtTPGTaxonomyListByPostType() { |
| 192 | $error = true; |
| 193 | $msg = $data = null; |
| 194 | $is_ok = true; |
| 195 | if ( ! current_user_can( 'edit_posts' ) ) { |
| 196 | $is_ok = false; |
| 197 | } |
| 198 | if ( ! Fns::verifyNonce() ) { |
| 199 | $is_ok = false; |
| 200 | } |
| 201 | if ( $is_ok ) { |
| 202 | $error = false; |
| 203 | $taxonomies = Fns::rt_get_all_taxonomy_by_post_type( $_REQUEST['post_type'] ); |
| 204 | |
| 205 | if ( is_array( $taxonomies ) && ! empty( $taxonomies ) ) { |
| 206 | $data .= Fns::rtFieldGenerator( |
| 207 | [ |
| 208 | 'tpg_taxonomy' => [ |
| 209 | 'type' => 'checkbox', |
| 210 | 'label' => esc_html__( 'Taxonomy', 'the-post-grid' ), |
| 211 | 'id' => 'post-taxonomy', |
| 212 | 'multiple' => true, |
| 213 | 'value' => isset( $_REQUEST['taxonomy'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_REQUEST['taxonomy'] ) ) : [], |
| 214 | 'options' => $taxonomies, |
| 215 | ], |
| 216 | ] |
| 217 | ); |
| 218 | } else { |
| 219 | $data = '<div class="field-holder">' . esc_html__( 'No Taxonomy found', 'the-post-grid' ) . '</div>'; |
| 220 | } |
| 221 | } else { |
| 222 | $msg = esc_html__( 'Security error', 'the-post-grid' ); |
| 223 | } |
| 224 | |
| 225 | wp_send_json( |
| 226 | [ |
| 227 | 'error' => $error, |
| 228 | 'msg' => $msg, |
| 229 | 'data' => $data, |
| 230 | ] |
| 231 | ); |
| 232 | die(); |
| 233 | } |
| 234 | |
| 235 | /** |
| 236 | * Isotope Filter |
| 237 | * |
| 238 | * @return void |
| 239 | */ |
| 240 | public function rtTPGIsotopeFilter() { |
| 241 | $error = true; |
| 242 | $msg = $data = null; |
| 243 | |
| 244 | $is_ok = true; |
| 245 | if ( ! current_user_can( 'edit_posts' ) ) { |
| 246 | $is_ok = false; |
| 247 | } |
| 248 | if ( ! Fns::verifyNonce() ) { |
| 249 | $is_ok = false; |
| 250 | } |
| 251 | if ( $is_ok ) { |
| 252 | $error = false; |
| 253 | $post_type = isset( $_REQUEST['post_type'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['post_type'] ) ) : null; |
| 254 | $taxonomies = Fns::rt_get_taxonomy_for_filter( $post_type ); |
| 255 | |
| 256 | if ( is_array( $taxonomies ) && ! empty( $taxonomies ) ) { |
| 257 | foreach ( $taxonomies as $tKey => $tax ) { |
| 258 | $data .= '<option value="' . absint( $tKey ) . '">' . esc_html( $tax ) . '</option>'; |
| 259 | } |
| 260 | } |
| 261 | } else { |
| 262 | $msg = esc_html__( 'Security error', 'the-post-grid' ); |
| 263 | } |
| 264 | |
| 265 | wp_send_json( |
| 266 | [ |
| 267 | 'error' => $error, |
| 268 | 'msg' => $msg, |
| 269 | 'data' => $data, |
| 270 | ] |
| 271 | ); |
| 272 | die(); |
| 273 | } |
| 274 | |
| 275 | /** |
| 276 | * Term list |
| 277 | * |
| 278 | * @return void |
| 279 | */ |
| 280 | public function rtTPGTermListByTaxonomy() { |
| 281 | $error = true; |
| 282 | $msg = $data = null; |
| 283 | |
| 284 | $is_ok = true; |
| 285 | if ( ! current_user_can( 'edit_posts' ) ) { |
| 286 | $is_ok = false; |
| 287 | } |
| 288 | if ( ! Fns::verifyNonce() ) { |
| 289 | $is_ok = false; |
| 290 | } |
| 291 | if ( $is_ok ) { |
| 292 | $error = false; |
| 293 | $taxonomy = isset( $_REQUEST['taxonomy'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['taxonomy'] ) ) : null; |
| 294 | |
| 295 | $data .= "<div class='term-filter-item-container {$taxonomy}'>"; |
| 296 | $data .= Fns::rtFieldGenerator( |
| 297 | [ |
| 298 | 'term_' . $taxonomy => [ |
| 299 | 'type' => 'select', |
| 300 | 'label' => ucfirst( str_replace( '_', ' ', $taxonomy ) ), |
| 301 | 'class' => 'rt-select2 full', |
| 302 | 'id' => 'term-' . wp_rand(), |
| 303 | 'holderClass' => "term-filter-item {$taxonomy}", |
| 304 | 'value' => null, |
| 305 | 'multiple' => true, |
| 306 | 'options' => Fns::rt_get_all_term_by_taxonomy( $taxonomy ), |
| 307 | ], |
| 308 | ] |
| 309 | ); |
| 310 | $data .= Fns::rtFieldGenerator( |
| 311 | [ |
| 312 | 'term_operator_' . $taxonomy => [ |
| 313 | 'type' => 'select', |
| 314 | 'label' => esc_html__( 'Operator', 'the-post-grid' ), |
| 315 | 'class' => 'rt-select2 full', |
| 316 | 'holderClass' => "term-filter-item-operator {$taxonomy}", |
| 317 | 'options' => Options::rtTermOperators(), |
| 318 | ], |
| 319 | ] |
| 320 | ); |
| 321 | $data .= '</div>'; |
| 322 | } else { |
| 323 | $msg = esc_html__( 'Security error', 'the-post-grid' ); |
| 324 | } |
| 325 | wp_send_json( |
| 326 | [ |
| 327 | 'error' => $error, |
| 328 | 'msg' => $msg, |
| 329 | 'data' => $data, |
| 330 | ] |
| 331 | ); |
| 332 | die(); |
| 333 | } |
| 334 | |
| 335 | /** |
| 336 | * Shortcode list |
| 337 | * |
| 338 | * @return void |
| 339 | */ |
| 340 | public function shortCodeList() { |
| 341 | |
| 342 | $is_ok = true; |
| 343 | if ( ! current_user_can( 'edit_posts' ) ) { |
| 344 | $is_ok = false; |
| 345 | } |
| 346 | if ( ! Fns::verifyNonce() ) { |
| 347 | $is_ok = false; |
| 348 | } |
| 349 | if ( $is_ok ) { |
| 350 | |
| 351 | $html = null; |
| 352 | $scQ = new \WP_Query( |
| 353 | apply_filters( |
| 354 | 'tpg_sc_list_query_args', |
| 355 | [ |
| 356 | 'post_type' => rtTPG()->post_type, |
| 357 | 'order_by' => 'title', |
| 358 | 'order' => 'DESC', |
| 359 | 'post_status' => 'publish', |
| 360 | 'posts_per_page' => - 1, |
| 361 | ] |
| 362 | ) |
| 363 | ); |
| 364 | if ( $scQ->have_posts() ) { |
| 365 | $html .= "<div class='mce-container mce-form'>"; |
| 366 | $html .= "<div class='mce-container-body'>"; |
| 367 | $html .= '<label class="mce-widget mce-label" style="padding: 20px;font-weight: bold;" for="scid">' . esc_html__( 'Select Short code', 'the-post-grid' ) . '</label>'; |
| 368 | $html .= "<select name='id' id='scid' style='width: 150px;margin: 15px;'>"; |
| 369 | $html .= "<option value=''>" . esc_html__( 'Default', 'the-post-grid' ) . '</option>'; |
| 370 | |
| 371 | while ( $scQ->have_posts() ) { |
| 372 | $scQ->the_post(); |
| 373 | $html .= "<option value='" . get_the_ID() . "'>" . get_the_title() . '</option>'; |
| 374 | } |
| 375 | |
| 376 | $html .= '</select>'; |
| 377 | $html .= '</div>'; |
| 378 | $html .= '</div>'; |
| 379 | } else { |
| 380 | $html .= '<div>' . esc_html__( 'No shortCode found.', 'the-post-grid' ) . '</div>'; |
| 381 | } |
| 382 | |
| 383 | Fns::print_html( $html, true ); |
| 384 | } else { |
| 385 | echo esc_html__( 'Security error', 'the-post-grid' ); |
| 386 | } |
| 387 | die(); |
| 388 | } |
| 389 | } |
| 390 |