PluginProbe
WP-Stateless – Google Cloud Storage / 3.2.1
WP-Stateless – Google Cloud Storage v3.2.1
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 3.2.1, at lib/classes/class-ajax.php

116 lines 3.1 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 global $doing_manual_sync;
52
53 $response = array(
54 'message' => '',
55 'html' => '',
56 );
57
58 try {
59 $doing_manual_sync = true;
60
61 $action = $_REQUEST['action'];
62
63 /** Determine if the current class has the method to handle request */
64 if (is_callable(array($this, 'action_' . $action))) {
65 $response = call_user_func_array(array($this, 'action_' . $action), array($_REQUEST));
66 }
67 /** Determine if external function exists to handle request */
68 elseif (is_callable('action_' . $action)) {
69 $response = call_user_func_array($action, array($_REQUEST));
70 } elseif (is_callable($action)) {
71 $response = call_user_func_array($action, array($_REQUEST));
72 }
73 /** Oops! */
74 else {
75 throw new \Exception(__('Incorrect Request'));
76 }
77 } catch (\Exception $e) {
78 wp_send_json_error($e->getMessage());
79 }
80
81 wp_send_json_success($response);
82 }
83
84 /**
85 * Regenerate image sizes.
86 */
87 public function action_stateless_process_image() {
88 @set_time_limit(-1);
89
90 $image = Utility::process_image_by_id(intval($_REQUEST['id']));
91
92 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());
93 }
94
95 /**
96 * @return string
97 * @throws \Exception
98 */
99 public function action_stateless_process_file() {
100 @set_time_limit(-1);
101
102 $file = Utility::process_file_by_id(intval($_REQUEST['id']));
103
104 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());
105 }
106
107 /**
108 * Returns bucket folder (to check whether there is something to continue in JS)
109 */
110 public function action_stateless_get_bucket_folder() {
111 return array('bucket_folder' => get_option('sm_root_dir'));
112 }
113 }
114 }
115 }
116