PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.33.1
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.33.1
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
formidable / classes / helpers / FrmFormsListHelper.php

FrmFormsListHelper.php in Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More 6.33.1, at classes/helpers/FrmFormsListHelper.php

747 lines 20.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 die( 'You are not allowed to call this page directly.' );
4 }
5
6 class FrmFormsListHelper extends FrmListHelper {
7
8 /**
9 * The transient name that stores data for which posts a form is embedded in.
10 *
11 * @since 6.32
12 *
13 * @var string
14 */
15 private static $embed_posts_transient_name = 'frm_posts_contain_form';
16
17 /**
18 * @var string
19 */
20 public $status = '';
21
22 public $total_items = 0;
23
24 /**
25 * @param array $args
26 */
27 public function __construct( $args ) {
28 $this->status = self::get_param( array( 'param' => 'form_type' ) );
29
30 parent::__construct( $args );
31 $this->screen->set_screen_reader_content(
32 array(
33 'heading_list' => esc_html__( 'Forms list', 'formidable' ),
34 )
35 );
36 }
37
38 /**
39 * @return void
40 */
41 public function prepare_items() {
42 global $per_page, $mode;
43
44 $page = $this->get_pagenum();
45 $per_page = $this->get_items_per_page( 'formidable_page_formidable_per_page' );
46
47 $mode = self::get_param(
48 array(
49 'param' => 'mode',
50 'default' => 'list',
51 )
52 );
53
54 $orderby = self::get_param(
55 array(
56 'param' => 'orderby',
57 'default' => 'name',
58 )
59 );
60 $order = self::get_param(
61 array(
62 'param' => 'order',
63 'default' => 'ASC',
64 )
65 );
66
67 FrmAppController::apply_saved_sort_preference( $orderby, $order );
68
69 $start = self::get_param(
70 array(
71 'param' => 'start',
72 'default' => ( $page - 1 ) * $per_page,
73 )
74 );
75
76 $s_query = array(
77 array(
78 'or' => 1,
79 'parent_form_id' => null,
80 'parent_form_id <' => 1,
81 ),
82 );
83 switch ( $this->status ) {
84 case 'draft':
85 $s_query['is_template'] = 0;
86 $s_query['status'] = 'draft';
87 break;
88 case 'trash':
89 $s_query['status'] = 'trash';
90 break;
91 default:
92 $s_query['is_template'] = 0;
93 $s_query['status !'] = 'trash';
94 break;
95 }
96
97 $s = self::get_param(
98 array(
99 'param' => 's',
100 'sanitize' => 'sanitize_text_field',
101 )
102 );
103
104 if ( $s !== '' ) {
105 preg_match_all( '/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $s, $matches );
106 $search_terms = array_map( 'trim', $matches[0] );
107
108 foreach ( $search_terms as $term ) {
109 $s_query[] = array(
110 'or' => true,
111 'name LIKE' => $term,
112 'description LIKE' => $term,
113 'created_at LIKE' => $term,
114 'form_key LIKE' => $term,
115 'id' => $term,
116 );
117 unset( $term );
118 }
119 }
120
121 $this->items = FrmForm::getAll( $s_query, $orderby . ' ' . $order, $start . ',' . $per_page );
122 $total_items = FrmDb::get_count( 'frm_forms', $s_query );
123 $this->total_items = $total_items;
124
125 $this->set_pagination_args(
126 array(
127 'total_items' => $total_items,
128 'per_page' => $per_page,
129 )
130 );
131 }
132
133 /**
134 * @return void
135 */
136 public function no_items() {
137 if ( $this->status === 'trash' ) {
138 echo '<p>';
139 esc_html_e( 'No forms found in the trash.', 'formidable' );
140 // phpcs:disable Generic.WhiteSpace.ScopeIndent
141 ?>
142 <a href="<?php echo esc_url( admin_url( 'admin.php?page=formidable' ) ); ?>">
143 <?php esc_html_e( 'See all forms.', 'formidable' ); ?>
144 </a>
145 <?php
146 // phpcs:enable Generic.WhiteSpace.ScopeIndent
147 echo '</p>';
148 } else {
149 $title = __( 'No Forms Found', 'formidable' );
150 include FrmAppHelper::plugin_path() . '/classes/views/frm-forms/_no_forms.php';
151 }
152 }
153
154 public function get_bulk_actions() {
155 $actions = array();
156
157 if ( 'trash' === $this->status ) {
158 if ( current_user_can( 'frm_edit_forms' ) ) {
159 $actions['bulk_untrash'] = __( 'Restore', 'formidable' );
160 }
161
162 if ( current_user_can( 'frm_delete_forms' ) ) {
163 $actions['bulk_delete'] = __( 'Delete Permanently', 'formidable' );
164 }
165 } elseif ( EMPTY_TRASH_DAYS && current_user_can( 'frm_delete_forms' ) ) {
166 $actions['bulk_trash'] = __( 'Move to Trash', 'formidable' );
167 } elseif ( current_user_can( 'frm_delete_forms' ) ) {
168 $actions['bulk_delete'] = __( 'Delete', 'formidable' );
169 }
170
171 return $actions;
172 }
173
174 /**
175 * @param string $which
176 *
177 * @return void
178 */
179 public function extra_tablenav( $which ) {
180 if ( 'top' !== $which ) {
181 return;
182 }
183
184 if ( 'trash' !== $this->status || ! current_user_can( 'frm_delete_forms' ) ) {
185 return;
186 }
187
188 // phpcs:disable Generic.WhiteSpace.ScopeIndent
189 ?>
190 <div class="alignleft actions frm_visible_overflow">
191 <?php submit_button( __( 'Empty Trash', 'formidable' ), 'apply', 'delete_all', false ); ?>
192 </div>
193 <?php
194 // phpcs:enable Generic.WhiteSpace.ScopeIndent
195 }
196
197 /**
198 * @return array
199 */
200 public function get_views() {
201 $statuses = array(
202 'published' => __( 'My Forms', 'formidable' ),
203 'draft' => __( 'Drafts', 'formidable' ),
204 'trash' => __( 'Trash', 'formidable' ),
205 );
206
207 $links = array();
208 $counts = FrmForm::get_count();
209 $form_type = FrmAppHelper::simple_get( 'form_type', 'sanitize_title', 'published' );
210
211 if ( isset( $statuses[ $form_type ] ) ) {
212 $counts->$form_type = $this->total_items;
213 }
214
215 $form_type = self::get_param(
216 array(
217 'param' => 'form_type',
218 'default' => 'published',
219 )
220 );
221
222 foreach ( $statuses as $status => $name ) {
223 $class = $status == $form_type ? ' class="current"' : ''; // phpcs:ignore Universal.Operators.StrictComparisons
224
225 if ( $counts->{$status} || 'draft' !== $status ) {
226 /* translators: %1$s: Status, %2$s: Number of items */
227 $links[ $status ] = '<a href="' . esc_url( '?page=formidable&form_type=' . $status ) . '" ' . $class . '>' . sprintf( __( '%1$s <span class="count">(%2$s)</span>', 'formidable' ), $name, number_format_i18n( $counts->{$status} ) ) . '</a>'; // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
228 }
229
230 unset( $status, $name );
231 }
232
233 return $links;
234 }
235
236 /**
237 * @param stdClass $item
238 * @param string $style
239 *
240 * @return string
241 */
242 public function single_row( $item, $style = '' ) {
243 global $mode;
244
245 // Set up the hover actions for this user
246 $actions = array();
247 $edit_link = FrmForm::get_edit_link( $item->id );
248
249 $this->get_actions( $actions, $item, $edit_link );
250
251 $action_links = $this->row_actions( $actions );
252 $checkbox = $this->get_row_checkbox( $item );
253 $r = '<tr id="item-action-' . absint( $item->id ) . '"' . $style . '>';
254
255 list( $columns, $hidden ) = $this->get_column_info();
256
257 $format = 'Y/m/d';
258
259 if ( 'list' !== $mode ) {
260 $format .= ' \<\b\r \/\> g:i:s a';
261 }
262
263 foreach ( $columns as $column_name => $column_display_name ) {
264 $class = $column_name . ' column-' . $column_name . ( 'name' === $column_name ? ' post-title page-title column-title' : '' );
265 $style = '';
266
267 if ( in_array( $column_name, $hidden, true ) ) {
268 $class .= ' hidden';
269 }
270
271 if ( $column_name === 'name' ) {
272 $class .= ' column-primary';
273 }
274
275 $class = 'class="' . esc_attr( $class ) . '"';
276 $attributes = $class . $style . $this->get_column_data_attr( $column_name, $column_display_name );
277
278 switch ( $column_name ) {
279 case 'cb':
280 $r .= '<th scope="row" class="check-column">' . $checkbox . '</th>';
281 break;
282 case 'id':
283 case 'form_key':
284 $val = $item->{$column_name};
285 break;
286 case 'name':
287 $val = $this->get_form_name( $item, $actions, $edit_link, $mode );
288 $val .= $action_links;
289 break;
290 case 'created_at':
291 $date = gmdate( $format, strtotime( $item->created_at ) );
292 $val = '<abbr title="' . esc_attr( gmdate( 'Y/m/d g:i:s A', strtotime( $item->created_at ) ) ) . '">' . $date . '</abbr>';
293 break;
294 case 'entries':
295 $val = $this->get_entries_column_value( $item );
296 break;
297 default:
298 if ( method_exists( $this, 'column_' . $column_name ) ) {
299 $val = call_user_func( array( $this, 'column_' . $column_name ), $item ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
300 }
301 break;
302 }//end switch
303
304 if ( isset( $val ) ) {
305 $r .= "<td $attributes>";
306 $r .= $val;
307 $r .= '</td>';
308 }
309 unset( $val );
310 }//end foreach
311 return $r . '</tr>';
312 }
313
314 /**
315 * @param string $column_name
316 * @param string $column_display_name
317 *
318 * @return string
319 */
320 private function get_column_data_attr( $column_name, $column_display_name ) {
321 if ( 'settings' === $column_name ) {
322 return ' data-colname="' . esc_attr( trim( strip_tags( $column_display_name ) ) ) . '"';
323 }
324 return ' data-colname="' . esc_attr( $column_display_name ) . '"';
325 }
326
327 /**
328 * @param object $item
329 *
330 * @return string
331 */
332 private function get_entries_column_value( $item ) {
333 if ( ! empty( $item->options['no_save'] ) ) {
334 return (string) FrmAppHelper::icon_by_class(
335 'frmfont frm_forbid_icon frm_bstooltip',
336 array(
337 'title' => __( 'Saving entries is disabled for this form', 'formidable' ),
338 'echo' => false,
339 )
340 );
341 }
342
343 $text = intval( FrmEntry::getRecordCount( $item->id ) );
344 return current_user_can( 'frm_view_entries' ) ? '<a href="' . esc_url( admin_url( 'admin.php?page=formidable-entries&form=' . $item->id ) ) . '">' . $text . '</a>' : (string) $text; // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
345 }
346
347 /**
348 * @param object $item
349 *
350 * @return string
351 */
352 private function get_row_checkbox( $item ) {
353 // Set up the checkbox (because the user is editable, otherwise it's empty).
354 $checkbox = '<input type="checkbox" name="item-action[]" id="cb-item-action-' . absint( $item->id ) . '" value="' . esc_attr( $item->id ) . '" />';
355 $checkbox_label_text = sprintf(
356 // translators: Form title
357 __( 'Select %s', 'formidable' ),
358 ! empty( $item->name ) ? $item->name : FrmFormsHelper::get_no_title_text()
359 );
360
361 return $checkbox . '<label for="cb-item-action-' . absint( $item->id ) . '"><span class="screen-reader-text">' . esc_html( $checkbox_label_text ) . '</span></label>';
362 }
363
364 /**
365 * Get the HTML for the Actions column in the form list.
366 * This includes multiple icons for triggering the embed modal, the visual styler, and an active landing page.
367 *
368 * @since 6.0
369 * @deprecated 6.32 We moved these actions to other places. This column will show if there is a filter added to the hook.
370 *
371 * @param stdClass $form
372 *
373 * @return string
374 */
375 protected function column_shortcode( $form ) {
376 $val = '<a href="#" class="frm-embed-form" role="button" aria-label="' . esc_attr__( 'Embed Form', 'formidable' ) . '">' . FrmAppHelper::icon_by_class( 'frmfont frm_code_icon', array( 'echo' => false ) ) . '</a>'; // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
377 $val .= $this->column_style( $form );
378 $val .= $this->column_views( $form );
379 $val = apply_filters( 'frm_form_list_actions', $val, array( 'form' => $form ) );
380 // Remove the space hard coded in Landing pages.
381 $val = str_replace( '&nbsp;', '', $val );
382 return '<div>' . $val . '</div>';
383 }
384
385 /**
386 * Get the HTML for the Style column in the form list.
387 *
388 * @since 6.0
389 *
390 * @param stdClass $form
391 *
392 * @return string
393 */
394 protected function column_style( $form ) {
395 $style_setting = $form->options['custom_style'] ?? '';
396 $frm_settings = FrmAppHelper::get_settings();
397
398 if ( $style_setting === '0' || 'none' === $frm_settings->load_style ) {
399 // Don't show a link if styling is off.
400 return '';
401 }
402
403 $style = FrmStylesController::get_form_style( $form );
404
405 if ( ! $style ) {
406 // Do a second pass to avoid null values.
407 $frm_style = new FrmStyle( 'default' );
408 $style = $frm_style->get_one();
409 }
410
411 $href = FrmStylesHelper::get_edit_url( $style, $form->id );
412 return '<a href="' . esc_url( $href ) . '" title="' . esc_attr( $style->post_title ) . '">' . FrmAppHelper::icon_by_class( 'frmfont frm_pallet_icon', array( 'echo' => false ) ) . '</a>'; // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
413 }
414
415 /**
416 * Generate the HTML for the form Views page.
417 *
418 * @since 6.19
419 *
420 * @param stdClass $form Form object.
421 *
422 * @return string
423 */
424 protected function column_views( $form ) {
425 $attributes = array(
426 'href' => admin_url( 'admin.php?page=formidable-views&form=' . absint( $form->id ) . '&show_nav=1' ),
427 'title' => __( 'Link to list of all views for this form.', 'formidable' ),
428 'target' => '_blank',
429 );
430
431 if ( class_exists( 'FrmViewsDisplay' ) ) {
432 $view_ids = FrmViewsDisplay::get_display_ids_by_form( $form->id );
433 $view_count = $view_ids ? count( $view_ids ) : 0;
434 } else {
435 $view_count = 0;
436 }
437
438 // phpcs:disable Generic.WhiteSpace.ScopeIndent
439 return '<a ' . FrmAppHelper::array_to_html_params( $attributes ) . '>
440 ' . intval( $view_count ) .
441 '</a>';
442 // phpcs:enable Generic.WhiteSpace.ScopeIndent
443 }
444
445 /**
446 * Get the HTML for the Settings column in the form list.
447 *
448 * @since 6.32
449 *
450 * @return string
451 */
452 protected function column_settings() {
453 return '&nbsp;';
454 }
455
456 /**
457 * @param array $actions
458 * @param object $item
459 * @param string $edit_link
460 *
461 * @return void
462 */
463 private function get_actions( &$actions, $item, $edit_link ) {
464 $new_actions = FrmFormsHelper::get_action_links( $item->id, $item );
465
466 foreach ( $new_actions as $link => $action ) {
467 $new_actions[ $link ] = FrmFormsHelper::format_link_html( $action, 'short' );
468 }
469
470 if ( 'trash' === $this->status ) {
471 $actions = $new_actions;
472 return;
473 }
474
475 if ( current_user_can( 'frm_edit_forms' ) ) {
476 $actions['frm_edit'] = '<a href="' . esc_url( $edit_link ) . '">' . esc_html__( 'Edit', 'formidable' ) . '</a>';
477 $actions['frm_settings'] = '<a href="' . esc_url( '?page=formidable&frm_action=settings&id=' . $item->id ) . '">' . esc_html__( 'Settings', 'formidable' ) . '</a>';
478 }
479
480 $actions = array_merge( $actions, $new_actions );
481 $actions['embed'] = '<a href="#" class="frm-embed-form" role="button" aria-label="' . esc_attr__( 'Embed Form', 'formidable' ) . '">' . esc_html__( 'Embed', 'formidable' ) . '</a>'; // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
482 $actions['view'] = '<a href="' . esc_url( FrmFormsHelper::get_direct_link( $item->form_key, $item ) ) . '" target="_blank">' . esc_html__( 'Preview', 'formidable' ) . '</a>'; // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
483 }
484
485 /**
486 * @param object $item
487 * @param array $actions
488 * @param string $edit_link
489 * @param string $mode
490 *
491 * @return string
492 */
493 private function get_form_name( $item, $actions, $edit_link, $mode = 'list' ) {
494 $form_name = $item->name;
495
496 if ( is_null( $form_name ) || trim( $form_name ) === '' ) {
497 $form_name = FrmFormsHelper::get_no_title_text();
498 }
499
500 $form_name = FrmAppHelper::kses( $form_name );
501
502 if ( 'excerpt' !== $mode ) {
503 $form_name = FrmAppHelper::truncate( $form_name, 50 );
504 }
505
506 $val = '<strong>';
507
508 if ( 'trash' === $this->status ) {
509 $val .= $form_name;
510 } else {
511 $val .= '<a href="' . esc_url( isset( $actions['frm_edit'] ) ? $edit_link : FrmFormsHelper::get_direct_link( $item->form_key, $item ) ) . '" class="row-title">' . FrmAppHelper::kses( $form_name ) . '</a> '; // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
512 }
513
514 $this->add_draft_label( $item, $val );
515 $val .= '</strong>';
516
517 $this->add_form_description( $item, $val );
518
519 return $val;
520 }
521
522 /**
523 * @param object $item
524 * @param string $val
525 *
526 * @return void
527 */
528 private function add_draft_label( $item, &$val ) {
529 if ( 'draft' === $item->status && 'draft' !== $this->status ) {
530 $val .= ' - <span class="post-state">' . esc_html__( 'Draft', 'formidable' ) . '</span>';
531 }
532 }
533
534 /**
535 * @param object $item
536 * @param string $val
537 *
538 * @return void
539 */
540 private function add_form_description( $item, &$val ) {
541 global $mode;
542
543 if ( 'excerpt' === $mode && ! is_null( $item->description ) ) {
544 $val .= FrmAppHelper::truncate( strip_tags( $item->description ), 50 );
545 }
546 }
547
548 /**
549 * @return string
550 */
551 protected function confirm_bulk_delete() {
552 return __( 'ALL selected forms and their entries will be permanently deleted. Want to proceed?', 'formidable' );
553 }
554
555 /**
556 * @param stdClass $form
557 *
558 * @return string
559 */
560 public function column_embeds( $form ) {
561 $posts = $this->get_posts_contain_form( $form );
562
563 if ( ! $posts ) {
564 return '<span class="frm-forms-list-embeds-zero">0</span>';
565 }
566
567 return FrmAppHelper::clip(
568 function () use ( $posts ) {
569 ?>
570 <a href="#" class="frm-forms-list-embeds-btn" data-posts="<?php echo esc_attr( wp_json_encode( $posts ) ); ?>">
571 <?php
572 FrmAppHelper::icon_by_class( 'frmfont frm_arrowdown6_icon' );
573 echo intval( count( $posts ) );
574 ?>
575 </a>
576 <?php
577 }
578 );
579 }
580
581 /**
582 * Gets posts or pages that contain the form shortcode.
583 *
584 * @since 6.32
585 *
586 * @param stdClass $form Form object.
587 *
588 * @return array
589 */
590 private function get_posts_contain_form( $form ) {
591 $cached_posts = get_transient( self::$embed_posts_transient_name );
592
593 if ( isset( $cached_posts[ $form->id ] ) && is_array( $cached_posts[ $form->id ] ) ) {
594 return $cached_posts[ $form->id ];
595 }
596
597 $posts = $this->query_posts_contain_form( $form );
598
599 if ( ! is_array( $posts ) ) {
600 return array();
601 }
602
603 foreach ( $posts as $post ) {
604 if ( ! property_exists( $post, 'permalink' ) ) {
605 $post->permalink = get_permalink( $post->ID );
606 }
607
608 if ( ! property_exists( $post, 'edit_link' ) ) {
609 $post->edit_link = get_edit_post_link( $post->ID );
610 }
611
612 // Ensure post_name is not null or the string "null"
613 if ( ! isset( $post->post_name ) ) {
614 $post->post_name = '';
615 }
616
617 // Ensure post_title is not null or the string "null"
618 if ( ! isset( $post->post_title ) ) {
619 $post->post_title = '';
620 }
621
622 if ( '' === $post->post_title ) {
623 $post->post_title = __( '(no title)', 'formidable' );
624 }
625 }//end foreach
626
627 if ( ! is_array( $cached_posts ) ) {
628 $cached_posts = array();
629 }
630
631 $cached_posts[ $form->id ] = $posts;
632 set_transient( self::$embed_posts_transient_name, $cached_posts, DAY_IN_SECONDS );
633
634 return $posts;
635 }
636
637 /**
638 * Gets search strings for a form inside a post.
639 *
640 * @since 6.32
641 *
642 * @param int $form_id Form ID.
643 *
644 * @return string[]
645 */
646 protected function get_search_strings_for_form( $form_id ) {
647 return $this->get_base_search_strings_for_form( $form_id );
648 }
649
650 /**
651 * Gets the base search strings for a form inside a post.
652 *
653 * @since 6.32
654 *
655 * @param int $form_id Form ID.
656 *
657 * @return string[]
658 */
659 protected function get_base_search_strings_for_form( $form_id ) {
660 return array(
661 '[formidable id=' . $form_id . ']',
662 '[formidable id=' . $form_id . ' ',
663 '[formidable id="' . $form_id . '"',
664 "[formidable id='" . $form_id . "'",
665 '<!-- wp:formidable/simple-form {"formId":"' . $form_id . '"',
666 );
667 }
668
669 /**
670 * Queries for posts that contain the form shortcode.
671 *
672 * @param stdClass $form Form object.
673 *
674 * @return array
675 */
676 private function query_posts_contain_form( $form ) {
677 $form_id = $form->id;
678 global $wpdb;
679 $query_strings = $this->get_search_strings_for_form( $form_id );
680 $like_where = array();
681
682 foreach ( $query_strings as $query_string ) {
683 $like_where[] = $wpdb->remove_placeholder_escape( $wpdb->prepare( 'post_content LIKE %s', '%' . $query_string . '%' ) );
684 }
685
686 $like_where = implode( ' OR ', $like_where );
687 $where = "post_type IN ('post', 'page') AND ($like_where)";
688
689 // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
690 $posts = $wpdb->get_results( "SELECT ID,post_title,post_name FROM $wpdb->posts WHERE $where" );
691
692 if ( ! is_array( $posts ) ) {
693 return array();
694 }
695
696 /**
697 * @since 6.32
698 *
699 * @param stdClass[] $posts
700 * @param array $args
701 */
702 $filtered_posts = apply_filters( 'frm_get_posts_contain_form', $posts, compact( 'form' ) );
703
704 if ( ! is_array( $filtered_posts ) ) {
705 _doing_it_wrong( 'frm_get_posts_contain_form', 'Filter should return an array.', '6.32' );
706 return $posts;
707 }
708
709 return $filtered_posts;
710 }
711
712 /**
713 * Maybe clear the embed posts transient.
714 *
715 * @since 6.32
716 *
717 * @param int $post_id Post ID.
718 * @param WP_Post $post Post object.
719 *
720 * @return void
721 */
722 public static function maybe_clear_embed_posts_transient( $post_id, $post ) {
723 if ( str_contains( $post->post_content, '[formidable ' ) || str_contains( $post->post_content, '<!-- wp:formidable/simple-form ' ) ) {
724 // New post contains the form shortcode, so clear the embed posts transient.
725 delete_transient( self::$embed_posts_transient_name );
726 return;
727 }
728
729 $cached_posts = get_transient( self::$embed_posts_transient_name );
730
731 if ( ! is_array( $cached_posts ) ) {
732 return;
733 }
734
735 // If the new post data of a cached post doesn't contain the Formidable forms, clear the transient.
736 foreach ( $cached_posts as $posts ) {
737 foreach ( $posts as $post_data ) {
738 if ( intval( $post_data->ID ) === intval( $post_id ) ) {
739 // This post contains the form shortcode before updating, so clear the embed posts transient.
740 delete_transient( self::$embed_posts_transient_name );
741 return;
742 }
743 }
744 }
745 }
746 }
747