| 1 |
<?php |
| 2 |
/** |
| 3 |
* Functions |
| 4 |
* |
| 5 |
* @package GamiPress\Kadence_Blocks\Functions |
| 6 |
* @since 1.0.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
// Exit if accessed directly |
| 10 |
if( !defined( 'ABSPATH' ) ) exit; |
| 11 |
|
| 12 |
/** |
| 13 |
* Overrides GamiPress AJAX Helper for selecting posts |
| 14 |
* |
| 15 |
* @since 1.0.0 |
| 16 |
*/ |
| 17 |
function gamipress_kadence_blocks_ajax_get_posts() { |
| 18 |
|
| 19 |
// Security check, forces to die if not security passed |
| 20 |
check_ajax_referer( 'gamipress_admin', 'nonce' ); |
| 21 |
|
| 22 |
// Check if user can manage GamiPress |
| 23 |
if( ! current_user_can( gamipress_get_manager_capability() ) ) { |
| 24 |
wp_send_json_error( __( 'You\'re not allowed to perform this action.', 'gamipress' ) ); |
| 25 |
} |
| 26 |
|
| 27 |
global $wpdb; |
| 28 |
|
| 29 |
if( isset( $_REQUEST['post_type'] ) && in_array( 'kadence_form', $_REQUEST['post_type'] ) ) { |
| 30 |
|
| 31 |
// Pull back the search string |
| 32 |
$results = array(); |
| 33 |
|
| 34 |
$forms = gamipress_kadence_blocks_get_forms(); |
| 35 |
|
| 36 |
foreach ( $forms as $form ) { |
| 37 |
|
| 38 |
// Results should meet same structure like posts |
| 39 |
$results[] = array( |
| 40 |
'ID' => $form['id'], |
| 41 |
'post_title' => $form['name'], |
| 42 |
); |
| 43 |
|
| 44 |
} |
| 45 |
|
| 46 |
// Return our results |
| 47 |
wp_send_json_success( $results ); |
| 48 |
die; |
| 49 |
} |
| 50 |
|
| 51 |
} |
| 52 |
add_action( 'wp_ajax_gamipress_get_posts', 'gamipress_kadence_blocks_ajax_get_posts', 5 ); |
| 53 |
|
| 54 |
/** |
| 55 |
* Get all forms |
| 56 |
* |
| 57 |
* @since 1.0.0 |
| 58 |
* |
| 59 |
* @return string|null |
| 60 |
*/ |
| 61 |
function gamipress_kadence_blocks_get_forms( ) { |
| 62 |
|
| 63 |
global $wpdb; |
| 64 |
|
| 65 |
$all_forms = array(); |
| 66 |
|
| 67 |
$forms = $wpdb->get_results( "select id,post_title,post_content from {$wpdb->posts} where post_content like '%<!-- wp:kadence/form%' and post_status = 'publish'" ); |
| 68 |
|
| 69 |
foreach ($forms as $post) { |
| 70 |
$form_id = $post->id; |
| 71 |
$form_title = $post->post_title; |
| 72 |
$form_content = $post->post_content; |
| 73 |
|
| 74 |
$contentArray = explode('<!--', $form_content); |
| 75 |
$content = []; |
| 76 |
|
| 77 |
foreach ($contentArray as $key => $value) { |
| 78 |
if (str_contains($value, ' wp:kadence/form')) { |
| 79 |
$temp = str_replace(' wp:kadence/form', '', $value); |
| 80 |
$temp1 = explode('-->', $temp, 2); |
| 81 |
$content[] = json_decode($temp1[0]); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
if (is_array($content)) { |
| 86 |
foreach ($content as $form) { |
| 87 |
$parent_id = $form->postID; |
| 88 |
$unique_id = $form->uniqueID; |
| 89 |
$all_forms[] = array( |
| 90 |
'id' => $unique_id, |
| 91 |
'name' => $form_title . '_' . $unique_id, |
| 92 |
); |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
return $all_forms; |
| 98 |
|
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* Get the form title |
| 103 |
* |
| 104 |
* @since 1.0.0 |
| 105 |
* |
| 106 |
* @param int $form_id |
| 107 |
* |
| 108 |
* @return string|null |
| 109 |
*/ |
| 110 |
function gamipress_kadence_blocks_get_form_title( $form_id ) { |
| 111 |
|
| 112 |
// Empty title if no ID provided |
| 113 |
if( empty( $form_id ) ) { |
| 114 |
return ''; |
| 115 |
} |
| 116 |
|
| 117 |
$complete_form_id = explode( '_', $form_id ); |
| 118 |
$post_title = get_the_title( $complete_form_id[0] ); |
| 119 |
|
| 120 |
return $post_title . '_' . $form_id; |
| 121 |
|
| 122 |
} |