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