Admin
4 years ago
Hooks
4 years ago
AjaxController.php
4 years ago
ElementorController.php
4 years ago
GutenBergController.php
4 years ago
ScriptController.php
4 years ago
ShortcodeController.php
4 years ago
WidgetController.php
4 years ago
AjaxController.php
228 lines
| 1 | <?php |
| 2 | |
| 3 | namespace RT\ThePostGrid\Controllers; |
| 4 | |
| 5 | use RT\ThePostGrid\Helpers\Fns; |
| 6 | use RT\ThePostGrid\Helpers\Options; |
| 7 | |
| 8 | class AjaxController { |
| 9 | function __construct() { |
| 10 | add_action( 'wp_ajax_rtTPGSettings', array( $this, 'rtTPGSaveSettings' ) ); |
| 11 | add_action( 'wp_ajax_rtTPGShortCodeList', array( $this, 'shortCodeList' ) ); |
| 12 | add_action( 'wp_ajax_rtTPGTaxonomyListByPostType', array( $this, 'rtTPGTaxonomyListByPostType' ) ); |
| 13 | add_action( 'wp_ajax_rtTPGIsotopeFilter', array( $this, 'rtTPGIsotopeFilter' ) ); |
| 14 | add_action( 'wp_ajax_rtTPGTermListByTaxonomy', array( $this, 'rtTPGTermListByTaxonomy' ) ); |
| 15 | add_action( 'wp_ajax_defaultFilterItem', array( $this, 'defaultFilterItem' ) ); |
| 16 | add_action( 'wp_ajax_getCfGroupListAsField', array( $this, 'getCfGroupListAsField' ) ); |
| 17 | } |
| 18 | |
| 19 | function getCfGroupListAsField() { |
| 20 | |
| 21 | $error = true; |
| 22 | $data = $msg = null; |
| 23 | if ( Fns::verifyNonce() ) { |
| 24 | $fields = array(); |
| 25 | $post_type = ! empty( $_REQUEST['post_type'] ) ? $_REQUEST['post_type'] : null; |
| 26 | if ( $cf = Fns::is_acf() && $post_type ) { |
| 27 | $fields['cf_group'] = array( |
| 28 | "type" => "checkbox", |
| 29 | "name" => "cf_group", |
| 30 | "holderClass" => "tpg-hidden cf-fields cf-group", |
| 31 | "label" => "Custom Field group", |
| 32 | "multiple" => true, |
| 33 | "alignment" => "vertical", |
| 34 | "id" => "cf_group", |
| 35 | "options" => Fns::get_groups_by_post_type( $post_type, $cf ) |
| 36 | ); |
| 37 | $error = false; |
| 38 | $data = Fns::rtFieldGenerator( $fields ); |
| 39 | } |
| 40 | } else { |
| 41 | $msg = __( 'Server Error !!', 'the-post-grid' ); |
| 42 | } |
| 43 | $response = array( |
| 44 | 'error' => $error, |
| 45 | 'msg' => $msg, |
| 46 | 'data' => $data |
| 47 | ); |
| 48 | wp_send_json( $response ); |
| 49 | die(); |
| 50 | } |
| 51 | |
| 52 | function defaultFilterItem() { |
| 53 | |
| 54 | $error = true; |
| 55 | $data = $msg = null; |
| 56 | if ( Fns::verifyNonce() ) { |
| 57 | if ( $filter = $_REQUEST['filter'] ) { |
| 58 | $include = []; |
| 59 | if ( isset( $_REQUEST['include'] ) && $term = $_REQUEST['include'] ) { |
| 60 | $include = explode( ',', $term ); |
| 61 | } |
| 62 | $error = false; |
| 63 | $msg = __( 'Success', 'the-post-grid' ); |
| 64 | $data .= "<option value=''>" . __( 'Show All', 'the-post-grid' ) . "</option>"; |
| 65 | $items = Fns::rt_get_selected_term_by_taxonomy( $filter, $include, '', 0 ); |
| 66 | if ( ! empty( $items ) ) { |
| 67 | foreach ( $items as $id => $item ) { |
| 68 | $data .= "<option value='{$id}'>{$item}</option>"; |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 | } else { |
| 73 | $msg = __( 'Session Error !!', 'the-post-grid' ); |
| 74 | } |
| 75 | $response = array( |
| 76 | 'error' => $error, |
| 77 | 'msg' => $msg, |
| 78 | 'data' => $data |
| 79 | ); |
| 80 | wp_send_json( $response ); |
| 81 | die(); |
| 82 | } |
| 83 | |
| 84 | function rtTPGSaveSettings() { |
| 85 | |
| 86 | $error = true; |
| 87 | if ( Fns::verifyNonce() ) { |
| 88 | unset( $_REQUEST['action'] ); |
| 89 | unset( $_REQUEST[ rtTPG()->nonceId() ] ); |
| 90 | unset( $_REQUEST['_wp_http_referer'] ); |
| 91 | |
| 92 | update_option( rtTPG()->options['settings'], $_REQUEST ); |
| 93 | $response = array( |
| 94 | 'error' => false, |
| 95 | 'msg' => __( 'Settings successfully updated', 'the-post-grid' ) |
| 96 | ); |
| 97 | } else { |
| 98 | $response = array( |
| 99 | 'error' => $error, |
| 100 | 'msg' => __( 'Session Error !!', 'the-post-grid' ) |
| 101 | ); |
| 102 | } |
| 103 | wp_send_json( $response ); |
| 104 | die(); |
| 105 | } |
| 106 | |
| 107 | function rtTPGTaxonomyListByPostType() { |
| 108 | |
| 109 | $error = true; |
| 110 | $msg = $data = null; |
| 111 | if ( Fns::verifyNonce() ) { |
| 112 | $error = false; |
| 113 | $taxonomies = Fns::rt_get_all_taxonomy_by_post_type( isset( $_REQUEST['post_type'] ) ? $_REQUEST['post_type'] : null ); |
| 114 | if ( is_array( $taxonomies ) && ! empty( $taxonomies ) ) { |
| 115 | $data .= Fns::rtFieldGenerator( |
| 116 | array( |
| 117 | 'tpg_taxonomy' => array( |
| 118 | 'type' => 'checkbox', |
| 119 | 'label' => 'Taxonomy', |
| 120 | 'id' => 'post-taxonomy', |
| 121 | "multiple" => true, |
| 122 | "value" => isset( $_REQUEST['taxonomy'] ) ? $_REQUEST['taxonomy'] : [], |
| 123 | 'options' => $taxonomies |
| 124 | ) |
| 125 | ) |
| 126 | ); |
| 127 | } else { |
| 128 | $data = __( '<div class="field-holder">No Taxonomy found</div>', 'the-post-grid' ); |
| 129 | } |
| 130 | |
| 131 | } else { |
| 132 | $msg = __( 'Security error', 'the-post-grid' ); |
| 133 | } |
| 134 | wp_send_json( array( 'error' => $error, 'msg' => $msg, 'data' => $data ) ); |
| 135 | die(); |
| 136 | } |
| 137 | |
| 138 | function rtTPGIsotopeFilter() { |
| 139 | |
| 140 | $error = true; |
| 141 | $msg = $data = null; |
| 142 | if ( Fns::verifyNonce() ) { |
| 143 | $error = false; |
| 144 | $taxonomies = Fns::rt_get_taxonomy_for_filter( isset( $_REQUEST['post_type'] ) ? $_REQUEST['post_type'] : null ); |
| 145 | if ( is_array( $taxonomies ) && ! empty( $taxonomies ) ) { |
| 146 | foreach ( $taxonomies as $tKey => $tax ) { |
| 147 | $data .= "<option value='{$tKey}'>{$tax}</option>"; |
| 148 | } |
| 149 | } |
| 150 | } else { |
| 151 | $msg = __( 'Security error', 'the-post-grid' ); |
| 152 | } |
| 153 | wp_send_json( array( 'error' => $error, 'msg' => $msg, 'data' => $data ) ); |
| 154 | die(); |
| 155 | } |
| 156 | |
| 157 | function rtTPGTermListByTaxonomy() { |
| 158 | |
| 159 | $error = true; |
| 160 | $msg = $data = null; |
| 161 | if ( Fns::verifyNonce() ) { |
| 162 | $error = false; |
| 163 | $taxonomy = isset( $_REQUEST['taxonomy'] ) ? $_REQUEST['taxonomy'] : null; |
| 164 | $data .= "<div class='term-filter-item-container {$taxonomy}'>"; |
| 165 | $data .= Fns::rtFieldGenerator( |
| 166 | array( |
| 167 | 'term_' . $taxonomy => array( |
| 168 | 'type' => 'select', |
| 169 | 'label' => ucfirst( str_replace( '_', ' ', $taxonomy ) ), |
| 170 | 'class' => 'rt-select2 full', |
| 171 | 'id' => 'term-' . mt_rand(), |
| 172 | 'holderClass' => "term-filter-item {$taxonomy}", |
| 173 | 'value' => null, |
| 174 | "multiple" => true, |
| 175 | 'options' => Fns::rt_get_all_term_by_taxonomy( $taxonomy ) |
| 176 | ) |
| 177 | ) |
| 178 | ); |
| 179 | $data .= Fns::rtFieldGenerator( |
| 180 | array( |
| 181 | 'term_operator_' . $taxonomy => array( |
| 182 | 'type' => 'select', |
| 183 | 'label' => 'Operator', |
| 184 | 'class' => 'rt-select2 full', |
| 185 | 'holderClass' => "term-filter-item-operator {$taxonomy}", |
| 186 | 'options' => Options::rtTermOperators() |
| 187 | ) |
| 188 | ) |
| 189 | ); |
| 190 | $data .= "</div>"; |
| 191 | } else { |
| 192 | $msg = __( 'Security error', 'the-post-grid' ); |
| 193 | } |
| 194 | wp_send_json( array( 'error' => $error, 'msg' => $msg, 'data' => $data ) ); |
| 195 | die(); |
| 196 | } |
| 197 | |
| 198 | function shortCodeList() { |
| 199 | $html = null; |
| 200 | $scQ = new \WP_Query( apply_filters( 'tpg_sc_list_query_args', |
| 201 | array( 'post_type' => rtTPG()->post_type, |
| 202 | 'order_by' => 'title', |
| 203 | 'order' => 'DESC', |
| 204 | 'post_status' => 'publish', |
| 205 | 'posts_per_page' => - 1 |
| 206 | ) |
| 207 | ) ); |
| 208 | if ( $scQ->have_posts() ) { |
| 209 | |
| 210 | $html .= "<div class='mce-container mce-form'>"; |
| 211 | $html .= "<div class='mce-container-body'>"; |
| 212 | $html .= '<label class="mce-widget mce-label" style="padding: 20px;font-weight: bold;" for="scid">' . __( 'Select Short code', 'the-post-grid' ) . '</label>'; |
| 213 | $html .= "<select name='id' id='scid' style='width: 150px;margin: 15px;'>"; |
| 214 | $html .= "<option value=''>" . __( 'Default', 'the-post-grid' ) . "</option>"; |
| 215 | while ( $scQ->have_posts() ) { |
| 216 | $scQ->the_post(); |
| 217 | $html .= "<option value='" . get_the_ID() . "'>" . get_the_title() . "</option>"; |
| 218 | } |
| 219 | $html .= "</select>"; |
| 220 | $html .= "</div>"; |
| 221 | $html .= "</div>"; |
| 222 | } else { |
| 223 | $html .= "<div>" . __( 'No shortCode found.', 'the-post-grid' ) . "</div>"; |
| 224 | } |
| 225 | echo $html; |
| 226 | die(); |
| 227 | } |
| 228 | } |