PluginProbe ʕ •ᴥ•ʔ
The Post Grid – Shortcode, Gutenberg Blocks and Elementor Addon for Post Grid / 7.8.8
The Post Grid – Shortcode, Gutenberg Blocks and Elementor Addon for Post Grid v7.8.8
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 / Admin / MetaController.php
the-post-grid / app / Controllers / Admin Last commit date
Notice 8 months ago AdminAjaxController.php 8 months ago MetaController.php 8 months ago NoticeController.php 8 months ago PostTypeController.php 8 months ago SettingsController.php 8 months ago UpgradeController.php 8 months ago
MetaController.php
515 lines
1 <?php
2 /**
3 * Meta Controller class.
4 *
5 * @package RT_TPG
6 */
7
8 namespace RT\ThePostGrid\Controllers\Admin;
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 //phpcs:disable WordPress.Security.NonceVerification.Recommended
19
20 /**
21 * Meta Controller class.
22 */
23 class MetaController {
24 /**
25 * Class constructor
26 */
27 public function __construct() {
28 add_action( 'admin_head', [ $this, 'admin_head' ] );
29 add_action( 'edit_form_after_title', [ $this, 'tpg_sc_after_title' ] );
30 add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] );
31 add_action( 'save_post', [ $this, 'save_post' ], 10, 2 );
32 add_filter( 'manage_edit-rttpg_columns', [ $this, 'arrange_rttpg_columns' ] );
33 add_action( 'manage_rttpg_posts_custom_column', [ $this, 'manage_rttpg_columns' ], 10, 2 );
34 add_action( 'created_term', [ $this, 'save_taxonomy_fields' ], 10, 3 );
35 }
36
37 /**
38 * manage Column
39 *
40 * @param string $column Column.
41 *
42 * @return void
43 */
44 public function manage_rttpg_columns( $column ) {
45 switch ( $column ) {
46 case 'shortcode':
47 echo '<input type="text" onfocus="this.select();" readonly="readonly" value="[the-post-grid id=&quot;' . get_the_ID() . '&quot; title=&quot;' . get_the_title() . '&quot;]" class="large-text code rt-code-sc">'; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
48 break;
49 default:
50 break;
51 }
52 }
53
54 /**
55 * Arrange Columns
56 *
57 * @param array $columns Columns.
58 *
59 * @return array
60 */
61 public function arrange_rttpg_columns( $columns ) {
62 $shortcode = [ 'shortcode' => esc_html__( 'Shortcode', 'the-post-grid' ) ];
63
64 return array_slice( $columns, 0, 2, true ) + $shortcode + array_slice( $columns, 1, null, true );
65 }
66
67 /**
68 * Admin Scripts
69 *
70 * @return void
71 */
72 public function admin_enqueue_scripts() {
73
74 global $pagenow, $typenow;
75
76 if ( 'tpg_builder' === $typenow ) {
77 wp_enqueue_style( 'rt-tpg-admin' );
78 }
79
80 if ( ! in_array( $pagenow, [ 'post.php', 'post-new.php' ], true ) ) {
81 return;
82 }
83
84 if ( rtTPG()->post_type !== $typenow ) {
85 return;
86 }
87
88 wp_dequeue_script( 'autosave' );
89 wp_enqueue_media();
90
91 $select2Id = 'rt-select2';
92 if ( class_exists( 'Avada' ) ) {
93 $select2Id = 'select2-avada-js';
94 }
95
96 // scripts.
97 wp_enqueue_script(
98 [
99 'jquery',
100 'jquery-ui-datepicker',
101 'wp-color-picker',
102 $select2Id,
103 'imagesloaded',
104 'rt-isotope-js',
105 'rt-tpg-admin',
106 'rt-tpg-admin-preview',
107 ]
108 );
109
110 // styles.
111 wp_enqueue_style(
112 [
113 'wp-color-picker',
114 'rt-select2',
115 'rt-fontawsome',
116 'rt-flaticon',
117 'rt-tpg-admin',
118 'rt-tpg-admin-preview',
119 ]
120 );
121
122 wp_localize_script(
123 'rt-tpg-admin',
124 'rttpg',
125 [
126 'nonceID' => esc_attr( rtTPG()->nonceId() ),
127 'nonce' => esc_attr( wp_create_nonce( rtTPG()->nonceText() ) ),
128 'ajaxurl' => esc_url( admin_url( 'admin-ajax.php' ) ),
129 'uid' => get_current_user_id(),
130 ]
131 );
132 }
133
134 /**
135 * Add Metabox.
136 *
137 * @return void
138 */
139 public function admin_head() {
140 add_meta_box(
141 'rttpg_meta',
142 esc_html__( 'Short Code Generator', 'the-post-grid' ),
143 [ $this, 'rttpg_meta_settings_selection' ],
144 rtTPG()->post_type,
145 'normal',
146 'high'
147 );
148
149 add_meta_box(
150 rtTPG()->post_type . '_sc_preview_meta',
151 esc_html__( 'Layout Preview', 'the-post-grid' ),
152 [ $this, 'tpg_sc_preview_selection' ],
153 rtTPG()->post_type,
154 'normal',
155 'high'
156 );
157
158 add_meta_box(
159 'rt_plugin_sc_pro_information',
160 esc_html__( 'Documentation', 'the-post-grid' ),
161 [ $this, 'rt_plugin_sc_pro_information' ],
162 rtTPG()->post_type,
163 'side',
164 'low'
165 );
166 }
167
168 /**
169 * Marketing.
170 *
171 * @param string $post Post.
172 *
173 * @return void
174 */
175 public function rt_plugin_sc_pro_information( $post ) {
176 $html = '';
177
178 if ( 'settings' === $post ) {
179 $html .= '<div class="rt-document-box rt-update-pro-btn-wrap">
180 <a href="' . esc_url( rtTpg()->proLink() ) . '" target="_blank" class="rt-update-pro-btn">' . esc_html__( 'Update Pro To Get More Features', 'the-post-grid' ) . '</a>
181 </div>';
182 } else {
183 if ( ! rtTPG()->hasPro() ) {
184 $html .= sprintf(
185 '<div class="rt-document-box"><div class="rt-box-icon"><i class="dashicons dashicons-megaphone"></i></div><div class="rt-box-content"><h3 class="rt-box-title">%1$s</h3>%2$s</div></div>',
186 esc_html__( 'Pro Features', 'the-post-grid' ),
187 Options::get_pro_feature_list()
188 );
189 }
190 }
191
192 $html .= sprintf(
193 '<div class="rt-document-box">
194 <div class="rt-box-icon"><i class="dashicons dashicons-media-document"></i></div>
195 <div class="rt-box-content">
196 <h3 class="rt-box-title">%1$s</h3>
197 <p>%2$s</p>
198 <a href="' . esc_url( rtTpg()->docLink() ) . '" target="_blank" class="rt-admin-btn">%1$s</a>
199 </div>
200 </div>',
201 esc_html__( 'Documentation', 'the-post-grid' ),
202 esc_html__( 'Get started by spending some time with the documentation we included step by step process with screenshots with video.', 'the-post-grid' )
203 );
204
205 $rtContact = 'https://www.radiustheme.com/contact/';
206 $rtFb = 'https://www.facebook.com/groups/234799147426640/';
207 $rtsite = 'https://www.radiustheme.com/';
208
209 $html .= '<div class="rt-document-box">
210 <div class="rt-box-icon"><i class="dashicons dashicons-sos"></i></div>
211 <div class="rt-box-content">
212 <h3 class="rt-box-title">Need Help?</h3>
213 <p>Stuck with something? Please create a
214 <a href="' . esc_url( $rtContact ) . '">ticket here</a> or post on <a href="' . esc_url( $rtFb ) . '">facebook group</a>. For emergency case join our <a href="' . esc_url( $rtsite ) . '">live chat</a>.</p>
215 <a href="' . esc_url( $rtContact ) . '" target="_blank" class="rt-admin-btn">' . esc_html__( 'Get Support', 'the-post-grid' ) . '</a>
216 </div>
217 </div>';
218
219 Fns::print_html( $html );
220 }
221
222 /**
223 * Preview
224 *
225 * @return void
226 */
227 public function tpg_sc_preview_selection() {
228 $html = null;
229 $html .= "<div class='rt-response'></div>";
230 $html .= "<div id='tpg-preview-container'></div>";
231
232 Fns::print_html( $html, true );
233 }
234
235 /**
236 * Text after title
237 *
238 * @param object $post Post object.
239 *
240 * @return void
241 */
242 public function tpg_sc_after_title( $post ) {
243 if ( rtTPG()->post_type !== $post->post_type ) {
244 return;
245 }
246
247 $html = null;
248 $html .= '<div class="postbox rt-after-title" style="margin-bottom: 0;"><div class="inside">';
249 $html .= '<p>
250 <input type="text" onfocus="this.select();" readonly="readonly" value="[the-post-grid id=&quot;' . absint( $post->ID ) . '&quot; title=&quot;' . esc_attr( $post->post_title ) . '&quot;]" class="large-text code rt-code-sc">
251 <input type="text" onfocus="this.select();" readonly="readonly" value="&#60;&#63;php echo do_shortcode( &#39;[the-post-grid id=&quot;' . absint( $post->ID ) . '&quot; title=&quot;' . esc_attr( $post->post_title ) . '&quot;]&#39; ); &#63;&#62;" class="large-text code rt-code-sc">
252 </p>';
253 $html .= '</div></div>';
254
255 Fns::print_html( $html, true );
256 }
257
258 /**
259 * Meta settings
260 *
261 * @param object $post Post object.
262 *
263 * @return void
264 */
265 public function rttpg_meta_settings_selection( $post ) {
266 $last_tab = trim( get_post_meta( $post->ID, '_tpg_last_active_tab', true ) );
267 $last_tab = $last_tab ? $last_tab : 'sc-post-post-source';
268 $post = [
269 'post' => $post,
270 ];
271
272 wp_nonce_field( rtTPG()->nonceText(), rtTPG()->nonceId() );
273
274 $html = null;
275 $html .= '<div id="sc-tabs" class="rttpg-wrapper rt-tab-container rt-setting-holder">';
276 $html .= sprintf(
277 '<ul class="rt-tab-nav">
278 <li%s><a href="#sc-post-post-source">%s</a></li>
279 <li%s><a href="#sc-post-layout-settings">%s</a></li>
280 <li%s><a href="#sc-settings">%s</a></li>
281 <li%s><a href="#sc-field-selection">%s</a></li>
282 <li%s><a href="#sc-style">%s</a></li>
283 </ul>',
284 'sc-post-post-source' === $last_tab ? ' class="active"' : '',
285 esc_html__( 'Query Build', 'the-post-grid' ),
286 'sc-post-layout-settings' === $last_tab ? ' class="active"' : '',
287 esc_html__( 'Layout Settings', 'the-post-grid' ),
288 'sc-settings' === $last_tab ? ' class="active"' : '',
289 esc_html__( 'Settings', 'the-post-grid' ),
290 'sc-field-selection' === $last_tab ? ' class="active"' : '',
291 esc_html__( 'Field Selection', 'the-post-grid' ),
292 'sc-style' === $last_tab ? ' class="active"' : '',
293 esc_html__( 'Style', 'the-post-grid' )
294 );
295
296 // Query Build tab.
297 $html .= sprintf( '<div id="sc-post-post-source" class="rt-tab-content"%s>', 'sc-post-post-source' === $last_tab ? ' style="display:block"' : '' );
298 $html .= Fns::view( 'settings.post-source', $post, true );
299 $html .= '</div>';
300
301 // Layout Setting tab.
302 $html .= sprintf( '<div id="sc-post-layout-settings" class="rt-tab-content"%s>', 'sc-post-layout-settings' === $last_tab ? ' style="display:block"' : '' );
303 $html .= Fns::view( 'settings.layout-settings', $post, true );
304 $html .= '</div>';
305
306 // Settings tab.
307 $html .= sprintf( '<div id="sc-settings" class="rt-tab-content"%s>', 'sc-settings' === $last_tab ? ' style="display:block"' : '' );
308 $html .= Fns::view( 'settings.sc-settings', $post, true );
309 $html .= '</div>';
310
311 // Field Selection tab.
312 $html .= sprintf( '<div id="sc-field-selection" class="rt-tab-content"%s>', 'sc-field-selection' === $last_tab ? ' style="display:block"' : '' );
313 $html .= Fns::view( 'settings.item-fields', $post, true );
314 $html .= '</div>';
315
316 // Style tab.
317 $html .= sprintf( '<div id="sc-style" class="rt-tab-content"%s>', 'sc-style' === $last_tab ? ' style="display:block"' : '' );
318 $html .= Fns::view( 'settings.style', $post, true );
319 $html .= '</div>';
320 $html .= sprintf( '<input type="hidden" id="_tpg_last_active_tab" name="_tpg_last_active_tab" value="%s"/>', $last_tab );
321 $html .= '</div>';
322
323 Fns::print_html( $html, true );
324 }
325
326 /**
327 * Save meta box.
328 *
329 * @param int $post_id Post ID.
330 * @param object $post Post object.
331 *
332 * @return mixed
333 */
334 public function save_post( $post_id, $post ) {
335 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
336 return $post_id;
337 }
338
339 if ( ! current_user_can( 'edit_post', $post_id ) ) {
340 return false;
341 }
342
343 if ( ! Fns::verifyNonce() ) {
344 return $post_id;
345 }
346
347 if ( rtTPG()->post_type !== $post->post_type ) {
348 return $post_id;
349 }
350
351 $mates = Fns::rtAllOptionFields();
352
353 foreach ( $mates as $metaKey => $field ) {
354 $rValue = ! empty( $_REQUEST[ $metaKey ] ) ? $_REQUEST[ $metaKey ] : null; //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash
355 $value = Fns::sanitize( $field, $rValue );
356
357 if ( empty( $field['multiple'] ) ) {
358 update_post_meta( $post_id, $metaKey, $value );
359 } else {
360 delete_post_meta( $post_id, $metaKey );
361 if ( is_array( $value ) && ! empty( $value ) ) {
362 foreach ( $value as $item ) {
363 add_post_meta( $post_id, $metaKey, $item );
364 }
365 }
366 }
367 }
368
369 $post_filter = ( isset( $_REQUEST['post_filter'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_REQUEST['post_filter'] ) ) : [] );
370 $advFilter = Options::rtTPAdvanceFilters();
371
372 foreach ( $advFilter['post_filter']['options'] as $filter => $fValue ) {
373 if ( $filter == 'tpg_taxonomy' ) {
374 delete_post_meta( $post_id, $filter );
375
376 if ( ! empty( $_REQUEST[ $filter ] ) && is_array( $_REQUEST[ $filter ] ) ) {
377 foreach ( $_REQUEST[ $filter ] as $tax ) {
378 if ( in_array( $filter, $post_filter ) ) {
379 add_post_meta( $post_id, $filter, trim( $tax ) );
380 }
381
382 delete_post_meta( $post_id, 'term_' . $tax );
383
384 $tt = isset( $_REQUEST[ 'term_' . $tax ] ) ? $_REQUEST[ 'term_' . $tax ] : [];
385
386 if ( is_array( $tt ) && ! empty( $tt ) && in_array( $filter, $post_filter ) ) {
387 foreach ( $tt as $termID ) {
388 add_post_meta( $post_id, 'term_' . $tax, trim( $termID ) );
389 }
390 }
391
392 $tto = isset( $_REQUEST[ 'term_operator_' . $tax ] ) ? sanitize_text_field( wp_unslash( $_REQUEST[ 'term_operator_' . $tax ] ) ) : null;
393
394 if ( $tto ) {
395 update_post_meta( $post_id, 'term_operator_' . $tax, trim( $tto ) );
396 }
397 }
398
399 $filterCount = isset( $_REQUEST[ $filter ] ) ? $_REQUEST[ $filter ] : [];
400 $tr = isset( $_REQUEST['taxonomy_relation'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['taxonomy_relation'] ) ) : null;
401
402 if ( count( $filterCount ) > 1 && $tr ) {
403 update_post_meta( $post_id, 'taxonomy_relation', trim( $tr ) );
404 } else {
405 delete_post_meta( $post_id, 'taxonomy_relation' );
406 }
407 }
408 } elseif ( $filter == 'author' ) {
409 delete_post_meta( $post_id, 'author' );
410
411 $authors = ( isset( $_REQUEST['author'] ) ? array_map( 'sanitize_text_field', wp_unslash( $_REQUEST['author'] ) ) : [] );
412
413 if ( is_array( $authors ) && ! empty( $authors ) && in_array( 'author', $post_filter ) ) {
414 foreach ( $authors as $authorID ) {
415 add_post_meta( $post_id, 'author', trim( $authorID ) );
416 }
417 }
418 } elseif ( $filter == 's' ) {
419 delete_post_meta( $post_id, 's' );
420
421 $s = ( isset( $_REQUEST['s'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['s'] ) ) : null );
422
423 if ( $s && in_array( 's', $post_filter ) ) {
424 update_post_meta( $post_id, 's', sanitize_text_field( trim( $s ) ) );
425 }
426 } elseif ( $filter == 'order' ) {
427 if ( in_array( 'order', $post_filter ) ) {
428 $order = ( isset( $_REQUEST['order'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['order'] ) ) : null );
429 $order_by = ( isset( $_REQUEST['order_by'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['order_by'] ) ) : null );
430 $tpg_meta_key = isset( $_REQUEST['tpg_meta_key'] ) ? sanitize_text_field( wp_unslash( $_REQUEST['tpg_meta_key'] ) ) : null;
431
432 if ( $order && in_array( 'order', $post_filter ) ) {
433 update_post_meta( $post_id, 'order', sanitize_text_field( trim( $order ) ) );
434 }
435
436 if ( $order_by && in_array( 'order', $post_filter ) ) {
437 update_post_meta( $post_id, 'order_by', sanitize_text_field( trim( $order_by ) ) );
438 }
439
440 if ( in_array( $order_by, array_keys( Options::rtMetaKeyType() ) ) && $tpg_meta_key && in_array( 'order', $post_filter ) ) {
441 update_post_meta( $post_id, 'tpg_meta_key', sanitize_text_field( trim( $tpg_meta_key ) ) );
442 } else {
443 delete_post_meta( $post_id, 'tpg_meta_key' );
444 }
445 } else {
446 delete_post_meta( $post_id, 'order' );
447 delete_post_meta( $post_id, 'tpg_meta_key' );
448 delete_post_meta( $post_id, 'order_by' );
449 }
450 } elseif ( $filter == 'date_range' ) {
451 if ( in_array( 'date_range', $post_filter ) ) {
452 $start = ! empty( $_REQUEST[ $filter . '_start' ] ) ? sanitize_text_field( wp_unslash( $_REQUEST[ $filter . '_start' ] ) ) : null;
453 $end = ! empty( $_REQUEST[ $filter . '_end' ] ) ? sanitize_text_field( wp_unslash( $_REQUEST[ $filter . '_end' ] ) ) : null;
454
455 update_post_meta( $post_id, $filter . '_start', trim( $start ) );
456 update_post_meta( $post_id, $filter . '_end', trim( $end ) );
457 } else {
458 delete_post_meta( $post_id, $filter . '_start' );
459 delete_post_meta( $post_id, $filter . '_end' );
460 }
461 }
462 /*
463 * tpg_post_status has removed
464 * elseif ( $filter == 'tpg_post_status' ) {
465 delete_post_meta( $post_id, $filter );
466
467 $statuses = isset( $_REQUEST[ $filter ] ) ? $_REQUEST[ $filter ] : [];
468
469 if ( is_array( $statuses ) && ! empty( $statuses ) && in_array( $filter, $post_filter ) ) {
470 foreach ( $statuses as $post_status ) {
471 add_post_meta( $post_id, $filter, trim( $post_status ) );
472 }
473 }
474 } */
475 }
476
477 // Extra css.
478 $extraFields = Options::extraStyle();
479 $extraTypes = [ 'color', 'size', 'weight', 'alignment' ];
480
481 foreach ( $extraFields as $key => $title ) {
482 foreach ( $extraTypes as $type ) {
483 $newKew = $key . "_{$type}";
484 if ( isset( $_REQUEST[ $newKew ] ) ) {
485 $value = sanitize_text_field( wp_unslash( $_REQUEST[ $newKew ] ) );
486
487 update_post_meta( $post_id, $newKew, $value );
488 }
489 }
490 }
491
492 // phpcs:ignore WordPress.Security.NonceVerification.Missing
493 if ( isset( $_POST['_tpg_last_active_tab'] ) && $active_tab = sanitize_text_field( wp_unslash( $_POST['_tpg_last_active_tab'] ) ) ) {
494 update_post_meta( $post_id, '_tpg_last_active_tab', $active_tab );
495 }
496 }
497
498 /**
499 * save_taxonomy_fields function.
500 *
501 * @param mixed $term_id Term ID being saved
502 * @param mixed $tt_id
503 * @param string $taxonomy
504 */
505 public function save_taxonomy_fields( $term_id, $tt_id = '', $taxonomy = '' ) {
506
507 $nonce = ! empty( $_POST['_wpnonce_add-tag'] ) ? sanitize_text_field( wp_unslash( $_POST['_wpnonce_add-tag'] ) ) : '';
508 if ( ! wp_verify_nonce( $nonce, 'add-tag' ) ) {
509 return;
510 }
511
512 update_term_meta( $term_id, '_rt_order', 0 );
513 }
514 }
515