| 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 |
@error_reporting(0); |
| 89 |
@set_time_limit(-1); |
| 90 |
|
| 91 |
$image = Utility::process_image_by_id(intval($_REQUEST['id'])); |
| 92 |
|
| 93 |
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()); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* @return string |
| 98 |
* @throws \Exception |
| 99 |
*/ |
| 100 |
public function action_stateless_process_file() { |
| 101 |
@error_reporting(0); |
| 102 |
@set_time_limit(-1); |
| 103 |
|
| 104 |
$file = Utility::process_file_by_id(intval($_REQUEST['id'])); |
| 105 |
|
| 106 |
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()); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Returns bucket folder (to check whether there is something to continue in JS) |
| 111 |
*/ |
| 112 |
public function action_stateless_get_bucket_folder() { |
| 113 |
return array('bucket_folder' => get_option('sm_root_dir')); |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
} |
| 118 |
|