PluginProbe
WP-Stateless – Google Cloud Storage / trunk
WP-Stateless – Google Cloud Storage vtrunk
4.4.3 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.3.0 2.3.1 2.3.2 3.0 3.0.1 3.0.2 3.0.3 3.0.4 3.1.0 3.1.1 3.2.0 3.2.1 3.2.2 All 62 releases
wp-stateless / lib / classes / class-ajax.php

class-ajax.php in WP-Stateless – Google Cloud Storage trunk, at lib/classes/class-ajax.php

122 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * AJAX Handler
5 */
6 namespace wpCloud\StatelessMedia {
7
8 if (!class_exists('wpCloud\StatelessMedia\Ajax')) {
9
10 final class Ajax {
11
12 /**
13 * The list of wp_ajax_{name} actions
14 *
15 * @var array
16 */
17 var $actions = array(
18 'stateless_process_image',
19 'stateless_process_file',
20 'stateless_get_bucket_folder'
21 );
22
23 /**
24 * The list of wp_ajax_nopriv_{name} actions
25 *
26 * @var array
27 */
28 var $nopriv_actions = array();
29
30 /**
31 * Init AJAX actions
32 *
33 * @author peshkov@UD
34 */
35 public function __construct() {
36 foreach ($this->actions as $action) {
37 add_action('wp_ajax_' . $action, array($this, 'request'));
38 }
39
40 foreach ($this->nopriv_actions as $action) {
41 add_action('wp_ajax_nopriv_' . $action, array($this, 'request'));
42 }
43 }
44
45 /**
46 * Handles AJAX request
47 *
48 * @author peshkov@UD
49 */
50 public function request() {
51 check_ajax_referer('sm_inline_sync');
52
53 if ( !is_user_logged_in() ) {
54 wp_send_json_error( array( 'error' => __( 'You are not allowed to do this action.', ud_get_stateless_media()->domain ) ) );
55 }
56
57 global $doing_manual_sync;
58
59 $response = array(
60 'message' => '',
61 'html' => '',
62 );
63
64 try {
65 $doing_manual_sync = true;
66
67 $action = $_REQUEST['action'];
68
69 /** Determine if the current class has the method to handle request */
70 if (is_callable(array($this, 'action_' . $action))) {
71 $response = call_user_func_array(array($this, 'action_' . $action), array($_REQUEST));
72 }
73 /** Determine if external function exists to handle request */
74 elseif (is_callable('action_' . $action)) {
75 $response = call_user_func_array($action, array($_REQUEST));
76 } elseif (is_callable($action)) {
77 $response = call_user_func_array($action, array($_REQUEST));
78 }
79 /** Oops! */
80 else {
81 throw new \Exception(__('Incorrect Request'));
82 }
83 } catch (\Exception $e) {
84 wp_send_json_error($e->getMessage());
85 }
86
87 wp_send_json_success($response);
88 }
89
90 /**
91 * Regenerate image sizes.
92 */
93 public function action_stateless_process_image() {
94 set_time_limit(0);
95
96 $image = Utility::process_image_by_id(intval($_REQUEST['id']));
97
98 return sprintf(__('%1$s (ID %2$s) was successfully synced in %3$s seconds.', ud_get_stateless_media()->domain), esc_html(get_the_title($image->ID)), $image->ID, timer_stop());
99 }
100
101 /**
102 * @return string
103 * @throws \Exception
104 */
105 public function action_stateless_process_file() {
106 set_time_limit(0);
107
108 $file = Utility::process_file_by_id(intval($_REQUEST['id']));
109
110 return sprintf(__('%1$s (ID %2$s) was successfully synchronised in %3$s seconds.', ud_get_stateless_media()->domain), esc_html(get_the_title($file->ID)), $file->ID, timer_stop());
111 }
112
113 /**
114 * Returns bucket folder (to check whether there is something to continue in JS)
115 */
116 public function action_stateless_get_bucket_folder() {
117 return array('bucket_folder' => get_option('sm_root_dir'));
118 }
119 }
120 }
121 }
122