AJAX.php
1 year ago
AJAX_Manager.php
1 year ago
Archive_Link.php
1 year ago
Click_Tracking_Cache_Cleared.php
1 year ago
Configure_Pruner.php
1 year ago
Copy_Report.php
1 year ago
Create_Campaign.php
2 years ago
Create_Report.php
1 year ago
Delete_Campaign.php
2 years ago
Delete_Data.php
1 year ago
Delete_Link.php
1 year ago
Delete_Module.php
1 year ago
Delete_Report.php
1 year ago
Dismiss_Notice.php
1 year ago
Edit_Link.php
1 year ago
Edit_Module.php
1 year ago
Export_Report_Statistics.php
11 months ago
Export_Report_Table.php
11 months ago
Export_Reports.php
10 months ago
Filter.php
11 months ago
Get_Markup_For_Module.php
1 year ago
Get_Markup_For_Modules.php
1 year ago
Import_Reports.php
1 year ago
Migration_Status.php
1 year ago
Preview_Email.php
2 years ago
Real_Time_Data.php
2 years ago
Refresh_Modules.php
1 year ago
Rename_Report.php
1 year ago
Reorder_Modules.php
1 year ago
Reset_Analytics.php
2 years ago
Reset_Overview.php
1 year ago
Save_Module.php
1 year ago
Save_Report.php
1 year ago
Set_Favorite_Report.php
1 year ago
Set_WooCommerce_Statuses_To_Track.php
1 year ago
Sort_Links.php
1 year ago
Sort_Reports.php
1 year ago
Test_Email.php
2 years ago
Update_Capabilities.php
1 year ago
Update_User_Settings.php
2 years ago
AJAX.php
166 lines
| 1 | <?php |
| 2 | |
| 3 | namespace IAWP\AJAX; |
| 4 | |
| 5 | use IAWP\Capability_Manager; |
| 6 | use IAWP\Migrations; |
| 7 | use IAWP\Utils\Security; |
| 8 | use IAWP\Utils\Server; |
| 9 | /** @internal */ |
| 10 | abstract class AJAX |
| 11 | { |
| 12 | public function __construct() |
| 13 | { |
| 14 | if (\IAWPSCOPED\iawp_is_pro() || !$this->requires_pro()) { |
| 15 | \add_action('wp_ajax_' . $this->action_name(), [$this, 'intercept_ajax']); |
| 16 | } |
| 17 | } |
| 18 | /** |
| 19 | * Classes must define an action name for the ajax request |
| 20 | * |
| 21 | * The required nonce for the ajax request will be the action name with a "_nonce" postfix |
| 22 | * Example: "iawp_delete_data" require a "iawp_delete_data_nonce" nonce field |
| 23 | * |
| 24 | * @return string |
| 25 | */ |
| 26 | protected abstract function action_name() : string; |
| 27 | /** |
| 28 | * Classes must define an action callback to run when an ajax request is made |
| 29 | * |
| 30 | * @return void |
| 31 | */ |
| 32 | protected abstract function action_callback() : void; |
| 33 | /** |
| 34 | * This is the direct handler for ajax requests. |
| 35 | * Permissions and nonce values are checked before executing the ajax action_callback function. |
| 36 | * |
| 37 | * @return void |
| 38 | */ |
| 39 | public function intercept_ajax() : void |
| 40 | { |
| 41 | // Todo - Should this be can_edit() instead? |
| 42 | $is_not_migrating = $this->allowed_during_migrations() || !Migrations\Migrations::is_migrating(); |
| 43 | $valid_fields = !$this->missing_fields(); |
| 44 | $can_view = Capability_Manager::can_view(); |
| 45 | \check_ajax_referer($this->action_name(), 'nonce'); |
| 46 | if ($is_not_migrating && $valid_fields && $can_view) { |
| 47 | Server::increase_max_execution_time(); |
| 48 | $this->action_callback(); |
| 49 | } else { |
| 50 | \wp_send_json_error(['errorMessage' => 'Unable to process IAWP AJAX request'], 400); |
| 51 | } |
| 52 | \wp_die(); |
| 53 | } |
| 54 | public function get_action_signature() : array |
| 55 | { |
| 56 | $shorthand_name = $this->action_name(); |
| 57 | if (\strpos($this->action_name(), "iawp_") === 0) { |
| 58 | $shorthand_name = \substr($this->action_name(), 5); |
| 59 | } |
| 60 | return [$shorthand_name => ['action' => $this->action_name(), 'nonce' => \wp_create_nonce($this->action_name())]]; |
| 61 | } |
| 62 | /** |
| 63 | * Classes can define a set of required fields for an ajax request |
| 64 | * |
| 65 | * @return array |
| 66 | */ |
| 67 | protected function action_required_fields() : array |
| 68 | { |
| 69 | return []; |
| 70 | } |
| 71 | /** |
| 72 | * Override method to allow the AJAX request to run during migrations |
| 73 | * |
| 74 | * @return bool false by default |
| 75 | */ |
| 76 | protected function allowed_during_migrations() : bool |
| 77 | { |
| 78 | return \false; |
| 79 | } |
| 80 | protected function requires_pro() : bool |
| 81 | { |
| 82 | return \false; |
| 83 | } |
| 84 | /** |
| 85 | * Get a field value. This method supports text and arrays. Returns array if no field found. |
| 86 | * |
| 87 | * @param $field |
| 88 | * |
| 89 | * @return array|string|null |
| 90 | */ |
| 91 | protected function get_field($field) |
| 92 | { |
| 93 | if (!\array_key_exists($field, $_POST)) { |
| 94 | return null; |
| 95 | } |
| 96 | $type = \gettype($_POST[$field]); |
| 97 | if ($type == 'array') { |
| 98 | if ($this->array_is_list($_POST[$field])) { |
| 99 | return \rest_sanitize_array($_POST[$field]); |
| 100 | } else { |
| 101 | return \rest_sanitize_object($_POST[$field]); |
| 102 | } |
| 103 | } else { |
| 104 | return \stripslashes(\sanitize_text_field($_POST[$field])); |
| 105 | } |
| 106 | } |
| 107 | protected function get_int_field($field) : ?int |
| 108 | { |
| 109 | if (!\array_key_exists($field, $_POST)) { |
| 110 | return null; |
| 111 | } |
| 112 | $type = \gettype($_POST[$field]); |
| 113 | if ($type === 'int') { |
| 114 | return $_POST[$field]; |
| 115 | } elseif ($type === 'string') { |
| 116 | return \ctype_digit($_POST[$field]) ? (int) $_POST[$field] : null; |
| 117 | } else { |
| 118 | return null; |
| 119 | } |
| 120 | } |
| 121 | protected function get_boolean_field($field) : ?bool |
| 122 | { |
| 123 | if (!\array_key_exists($field, $_POST)) { |
| 124 | return null; |
| 125 | } |
| 126 | $type = \gettype($_POST[$field]); |
| 127 | if ($type === 'string') { |
| 128 | return $_POST[$field] === 'true'; |
| 129 | } else { |
| 130 | return null; |
| 131 | } |
| 132 | } |
| 133 | protected function get_array_field($field) : ?array |
| 134 | { |
| 135 | if (!\array_key_exists($field, $_POST)) { |
| 136 | return null; |
| 137 | } |
| 138 | $value = $_POST[$field]; |
| 139 | if (!\is_array($value)) { |
| 140 | return null; |
| 141 | } |
| 142 | return Security::array($value); |
| 143 | } |
| 144 | // This is the recommended polyfill: https://wiki.php.net/rfc/is_list |
| 145 | private function array_is_list(array $array) : bool |
| 146 | { |
| 147 | $expectedKey = 0; |
| 148 | foreach ($array as $i => $_) { |
| 149 | if ($i !== $expectedKey) { |
| 150 | return \false; |
| 151 | } |
| 152 | $expectedKey++; |
| 153 | } |
| 154 | return \true; |
| 155 | } |
| 156 | private function missing_fields() : bool |
| 157 | { |
| 158 | foreach ($this->action_required_fields() as $required_field) { |
| 159 | if (!\array_key_exists($required_field, $_POST)) { |
| 160 | return \true; |
| 161 | } |
| 162 | } |
| 163 | return \false; |
| 164 | } |
| 165 | } |
| 166 |