1: <?php
2:
3: 4: 5:
6: class Papi_Admin_Ajax {
7:
8: 9: 10: 11: 12:
13: private $action_prefix = 'papi/ajax/';
14:
15: 16: 17:
18: public function __construct() {
19: $this->setup_actions();
20: }
21:
22: 23: 24:
25: private function setup_actions() {
26: add_action( 'init', [$this, 'add_endpoint'] );
27: add_action( 'parse_query', [$this, 'handle_papi_ajax'] );
28: add_action( 'admin_enqueue_scripts', [$this, 'ajax_url'], 10 );
29:
30:
31: add_action( $this->action_prefix . 'get_property', [$this, 'get_property'] );
32: add_action( $this->action_prefix . 'get_properties', [$this, 'get_properties'] );
33: add_action( $this->action_prefix . 'get_rules_result', [$this, 'get_rules_result'] );
34: }
35:
36: 37: 38:
39: public function add_endpoint() {
40: add_rewrite_tag( '%action%', '([^/]*)' );
41: add_rewrite_rule( 'papi-ajax/([^/]*)/?', 'index.php?action=$matches[1]', 'top' );
42: }
43:
44: 45: 46:
47: public function ajax_url() {
48: $url = esc_url( trailingslashit( get_bloginfo( 'url' ) ) . 'papi-ajax/' );
49: ?>
50: <script type="text/javascript">
51: var papi = papi || {};
52: papi.ajaxUrl = '<?php echo $url; ?>';
53: </script>
54: <?php
55: }
56:
57: 58: 59:
60: public function handle_papi_ajax() {
61: global $wp_query;
62:
63: if ( ! is_object( $wp_query ) ) {
64: return;
65: }
66:
67: if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
68: return;
69: }
70:
71: if ( ! papi_is_empty( papi_get_qs( 'action' ) ) ) {
72: $wp_query->set(
73: 'papi_ajax_action',
74: papi_get_qs( 'action' )
75: );
76: }
77:
78: $ajax_action = $wp_query->get( 'papi_ajax_action' );
79:
80: if ( is_user_logged_in() && has_action( $this->action_prefix . $ajax_action ) !== false ) {
81: if ( ! defined( 'DOING_AJAX' ) ) {
82: define( 'DOING_AJAX', true );
83: }
84:
85: if ( ! defined( 'DOING_PAPI_AJAX' ) ) {
86: define( 'DOING_PAPI_AJAX', true );
87: }
88:
89: status_header( 200 );
90: do_action( $this->action_prefix . $ajax_action );
91: wp_die();
92: }
93: }
94:
95: 96: 97: 98: 99:
100: public function get_property() {
101: $default_options = Papi_Core_Property::create()->get_options();
102: $keys = array_keys( get_object_vars( $default_options ) );
103: $options = papi_get_qs( $keys, true );
104:
105: if ( $property = papi_property( $options ) ) {
106: ob_start();
107:
108: $property->render_ajax_request();
109:
110: $html = ob_get_clean();
111:
112: wp_send_json( [
113: 'html' => utf8_encode( $html )
114: ] );
115: } else {
116: $this->render_error( 'No property found' );
117: }
118: }
119:
120: 121: 122: 123: 124:
125: public function get_properties() {
126: if ( ! papi_get_sanitized_post( 'properties' ) ) {
127: $this->render_error( 'No properties found' );
128: return;
129: }
130:
131: $items = json_decode(
132: stripslashes( papi_get_sanitized_post( 'properties' ) ),
133: true
134: );
135:
136: if ( empty( $items ) || ! is_array( $items ) ) {
137: $this->render_error( 'No properties found' );
138: return;
139: }
140:
141: foreach ( $items as $key => $item ) {
142: $property = papi_property( (array) $item );
143:
144: if ( ! papi_is_property( $property ) ) {
145: unset( $items[$key] );
146: continue;
147: }
148:
149: ob_start();
150:
151: $property->render_ajax_request();
152:
153: $items[$key] = trim( ob_get_clean() );
154: }
155:
156: $items = array_filter( $items );
157:
158: if ( empty( $items ) ) {
159: $this->render_error( 'No properties found' );
160: } else {
161: wp_send_json( [
162: 'html' => $items
163: ] );
164: }
165: }
166:
167: 168: 169: 170: 171:
172: public function get_rules_result() {
173: if ( ! papi_get_sanitized_post( 'data' ) ) {
174: $this->render_error( 'No rule found' );
175: return;
176: }
177:
178: $data = json_decode(
179: stripslashes( papi_get_sanitized_post( 'data' ) ),
180: true
181: );
182:
183: if ( empty( $data ) || ! is_array( $data ) || ! isset( $data['slug'] ) ) {
184: $this->render_error( 'No rule found' );
185: return;
186: }
187:
188: $page_type = papi_get_page_type_by_post_id();
189:
190: if ( $page_type instanceof Papi_Page_Type === false ) {
191: $this->render_error( 'No rule found' );
192: return;
193: }
194:
195: if ( preg_match( '/\[\]$/', $data['slug'] ) ) {
196: $data['slug'] = preg_replace( '/\[\]$/', '', $data['slug'] );
197: }
198:
199: if ( $property = $page_type->get_property( $data['slug'] ) ) {
200: wp_send_json( [
201: 'render' => $property->render_is_allowed_by_rules(
202: $data['rules']
203: )
204: ] );
205: } else {
206: $this->render_error( 'No rule found' );
207: }
208: }
209:
210: 211: 212: 213: 214:
215: public function render_error( $message ) {
216: wp_send_json( [
217: 'error' => $message
218: ] );
219: }
220: }
221:
222: new Papi_Admin_Ajax;
223: