PluginProbe ʕ •ᴥ•ʔ
The Post Grid – Shortcode, Gutenberg Blocks and Elementor Addon for Post Grid / 7.7.0
The Post Grid – Shortcode, Gutenberg Blocks and Elementor Addon for Post Grid v7.7.0
7.9.3 7.9.2 trunk 4.0.0 4.0.1 4.0.2 4.0.3 4.0.4 4.0.5 4.1.0 4.1.1 4.1.2 4.1.3 4.1.4 4.1.5 4.2.0 4.2.1 4.2.2 4.2.3 5.0.0 5.0.1 5.0.2 5.0.3 5.0.4 5.0.5 6.0.0 7.0.0 7.0.1 7.0.2 7.1.0 7.2.0 7.2.1 7.2.10 7.2.11 7.2.2 7.2.3 7.2.4 7.2.5 7.2.6 7.2.7 7.2.8 7.2.9 7.3.0 7.3.1 7.4.0 7.4.1 7.4.2 7.4.3 7.5.0 7.6.0 7.6.1 7.7.0 7.7.1 7.7.10 7.7.11 7.7.12 7.7.13 7.7.14 7.7.15 7.7.16 7.7.17 7.7.18 7.7.19 7.7.2 7.7.20 7.7.21 7.7.22 7.7.3 7.7.4 7.7.5 7.7.6 7.7.7 7.7.8 7.7.9 7.8.0 7.8.1 7.8.2 7.8.3 7.8.4 7.8.5 7.8.6 7.8.7 7.8.8 7.8.9 7.9.0 7.9.1
the-post-grid / app / Controllers / AjaxController.php
the-post-grid / app / Controllers Last commit date
Admin 2 years ago Api 2 years ago Blocks 2 years ago Hooks 2 years ago AjaxController.php 2 years ago BlocksController.php 2 years ago ElementorController.php 2 years ago GutenBergController.php 2 years ago MyAccountController.php 2 years ago PageTemplateController.php 2 years ago ScriptController.php 2 years ago ShortcodeController.php 2 years ago WidgetController.php 2 years ago
AjaxController.php
345 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
46 if ( Fns::verifyNonce() ) {
47 $fields = [];
48 $post_type = isset( $_REQUEST['post_type'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['post_type'] ) ) : null;
49
50 if ( $cf = Fns::is_acf() && $post_type ) {
51 $fields['cf_group'] = [
52 'type' => 'checkbox',
53 'name' => 'cf_group',
54 'holderClass' => 'tpg-hidden cf-fields cf-group',
55 'label' => esc_html__( 'Custom Field group', 'the-post-grid' ),
56 'multiple' => true,
57 'alignment' => 'vertical',
58 'id' => 'cf_group',
59 'options' => Fns::get_groups_by_post_type( $post_type, $cf ),
60 ];
61 $error = false;
62 $data = Fns::rtFieldGenerator( $fields );
63 }
64 } else {
65 $msg = esc_html__( 'Server Error !!', 'the-post-grid' );
66 }
67
68 $response = [
69 'error' => $error,
70 'msg' => $msg,
71 'data' => $data,
72 ];
73
74 wp_send_json( $response );
75 die();
76 }
77
78 /**
79 * Default filter.
80 *
81 * @return void
82 */
83 public function defaultFilterItem() {
84 $error = true;
85 $data = $msg = null;
86
87 if ( Fns::verifyNonce() ) {
88 $filter = isset( $_REQUEST['filter'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['filter'] ) ) : null;
89 $term = isset( $_REQUEST['include'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['include'] ) ) : null;
90
91 if ( ! empty( $filter ) ) {
92 $include = [];
93
94 if ( ! empty( $term ) ) {
95 $include = explode( ',', $term );
96 }
97
98 $error = false;
99 $msg = esc_html__( 'Success', 'the-post-grid' );
100 $data .= "<option value=''>" . esc_html__( 'Show All', 'the-post-grid' ) . '</option>';
101 $items = Fns::rt_get_selected_term_by_taxonomy( $filter, $include, '', 0 );
102
103 if ( ! empty( $items ) ) {
104 foreach ( $items as $id => $item ) {
105 $data .= '<option value="' . absint( $id ) . '">' . esc_html( $item ) . '</option>';
106 }
107 }
108 }
109 } else {
110 $msg = esc_html__( 'Session Error !!', 'the-post-grid' );
111 }
112 $response = [
113 'error' => $error,
114 'msg' => $msg,
115 'data' => $data,
116 ];
117
118 wp_send_json( $response );
119 die();
120 }
121
122 /**
123 * Save settings.
124 *
125 * @return void
126 */
127 public function rtTPGSaveSettings() {
128 $error = true;
129 $userId = get_current_user_id();
130
131 if ( $userId != $_REQUEST['uid'] ) {
132 wp_send_json(
133 [
134 'error' => true,
135 'msg' => esc_html__( 'You have no permission to modification.', 'the-post-grid' ),
136 ]
137 );
138 die();
139 }
140
141 if ( ! ( current_user_can( 'manage_options' ) ) ) {
142 wp_send_json(
143 [
144 'error' => true,
145 'msg' => esc_html__( 'You have no permission to modification.', 'the-post-grid' ),
146 ]
147 );
148 die();
149 }
150
151 if ( Fns::verifyNonce() ) {
152 unset( $_REQUEST['action'] );
153 unset( $_REQUEST[ rtTPG()->nonceId() ] );
154 unset( $_REQUEST['_wp_http_referer'] );
155 unset( $_REQUEST['uid'] );
156
157 update_option( rtTPG()->options['settings'], wp_unslash( $_REQUEST ) );
158
159 $response = [
160 'error' => false,
161 'msg' => esc_html__( 'Settings successfully updated', 'the-post-grid' ),
162 ];
163 } else {
164 $response = [
165 'error' => $error,
166 'msg' => esc_html__( 'Session Error !!', 'the-post-grid' ),
167 ];
168 }
169
170 wp_send_json( $response );
171 die();
172 }
173
174 /**
175 * Taxonomy list.
176 *
177 * @return void
178 */
179 public function rtTPGTaxonomyListByPostType() {
180 $error = true;
181 $msg = $data = null;
182
183 if ( Fns::verifyNonce() ) {
184 $error = false;
185 $taxonomies = Fns::rt_get_all_taxonomy_by_post_type( $_REQUEST['post_type'] );
186
187 if ( is_array( $taxonomies ) && ! empty( $taxonomies ) ) {
188 $data .= Fns::rtFieldGenerator(
189 [
190 'tpg_taxonomy' => [
191 'type' => 'checkbox',
192 'label' => esc_html__( 'Taxonomy', 'the-post-grid' ),
193 'id' => 'post-taxonomy',
194 'multiple' => true,
195 'value' => isset( $_REQUEST['taxonomy'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_REQUEST['taxonomy'] ) ) : [],
196 'options' => $taxonomies,
197 ],
198 ]
199 );
200 } else {
201 $data = '<div class="field-holder">' . esc_html__( 'No Taxonomy found', 'the-post-grid' ) . '</div>';
202 }
203 } else {
204 $msg = esc_html__( 'Security error', 'the-post-grid' );
205 }
206
207 wp_send_json(
208 [
209 'error' => $error,
210 'msg' => $msg,
211 'data' => $data,
212 ]
213 );
214 die();
215 }
216
217 /**
218 * Isotope Filter
219 *
220 * @return void
221 */
222 public function rtTPGIsotopeFilter() {
223 $error = true;
224 $msg = $data = null;
225
226 if ( Fns::verifyNonce() ) {
227 $error = false;
228 $post_type = isset( $_REQUEST['post_type'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['post_type'] ) ) : null;
229 $taxonomies = Fns::rt_get_taxonomy_for_filter( $post_type );
230
231 if ( is_array( $taxonomies ) && ! empty( $taxonomies ) ) {
232 foreach ( $taxonomies as $tKey => $tax ) {
233 $data .= '<option value="' . absint( $tKey ) . '">' . esc_html( $tax ) . '</option>';
234 }
235 }
236 } else {
237 $msg = esc_html__( 'Security error', 'the-post-grid' );
238 }
239
240 wp_send_json(
241 [
242 'error' => $error,
243 'msg' => $msg,
244 'data' => $data,
245 ]
246 );
247 die();
248 }
249
250 /**
251 * Term list
252 *
253 * @return void
254 */
255 public function rtTPGTermListByTaxonomy() {
256 $error = true;
257 $msg = $data = null;
258
259 if ( Fns::verifyNonce() ) {
260 $error = false;
261 $taxonomy = isset( $_REQUEST['taxonomy'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['taxonomy'] ) ) : null;
262
263 $data .= "<div class='term-filter-item-container {$taxonomy}'>";
264 $data .= Fns::rtFieldGenerator(
265 [
266 'term_' . $taxonomy => [
267 'type' => 'select',
268 'label' => ucfirst( str_replace( '_', ' ', $taxonomy ) ),
269 'class' => 'rt-select2 full',
270 'id' => 'term-' . wp_rand(),
271 'holderClass' => "term-filter-item {$taxonomy}",
272 'value' => null,
273 'multiple' => true,
274 'options' => Fns::rt_get_all_term_by_taxonomy( $taxonomy ),
275 ],
276 ]
277 );
278 $data .= Fns::rtFieldGenerator(
279 [
280 'term_operator_' . $taxonomy => [
281 'type' => 'select',
282 'label' => esc_html__( 'Operator', 'the-post-grid' ),
283 'class' => 'rt-select2 full',
284 'holderClass' => "term-filter-item-operator {$taxonomy}",
285 'options' => Options::rtTermOperators(),
286 ],
287 ]
288 );
289 $data .= '</div>';
290 } else {
291 $msg = esc_html__( 'Security error', 'the-post-grid' );
292 }
293 wp_send_json(
294 [
295 'error' => $error,
296 'msg' => $msg,
297 'data' => $data,
298 ]
299 );
300 die();
301 }
302
303 /**
304 * Shortcode list
305 *
306 * @return void
307 */
308 public function shortCodeList() {
309 $html = null;
310 $scQ = new \WP_Query(
311 apply_filters(
312 'tpg_sc_list_query_args',
313 [
314 'post_type' => rtTPG()->post_type,
315 'order_by' => 'title',
316 'order' => 'DESC',
317 'post_status' => 'publish',
318 'posts_per_page' => - 1,
319 ]
320 )
321 );
322 if ( $scQ->have_posts() ) {
323 $html .= "<div class='mce-container mce-form'>";
324 $html .= "<div class='mce-container-body'>";
325 $html .= '<label class="mce-widget mce-label" style="padding: 20px;font-weight: bold;" for="scid">' . esc_html__( 'Select Short code', 'the-post-grid' ) . '</label>';
326 $html .= "<select name='id' id='scid' style='width: 150px;margin: 15px;'>";
327 $html .= "<option value=''>" . esc_html__( 'Default', 'the-post-grid' ) . '</option>';
328
329 while ( $scQ->have_posts() ) {
330 $scQ->the_post();
331 $html .= "<option value='" . get_the_ID() . "'>" . get_the_title() . '</option>';
332 }
333
334 $html .= '</select>';
335 $html .= '</div>';
336 $html .= '</div>';
337 } else {
338 $html .= '<div>' . esc_html__( 'No shortCode found.', 'the-post-grid' ) . '</div>';
339 }
340
341 Fns::print_html( $html, true );
342 die();
343 }
344 }
345