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