EasyDigitalDownloads
3 years ago
Elementor
2 years ago
Integrations
3 years ago
MemberPress
2 years ago
Plugins
2 years ago
Promos
3 years ago
Rules
2 years ago
Shortcodes
2 years ago
WPForms
1 year ago
WooCommerce
1 year ago
Actions.php
1 year ago
Ajax.php
4 years ago
Api.php
1 year ago
ApiAuth.php
4 years ago
ApiKey.php
1 year ago
AssetLoader.php
5 years ago
BaseRestApi.php
3 years ago
Blocks.php
1 year ago
ClassicEditor.php
3 years ago
ConstantContact.php
1 year ago
Debug.php
1 year ago
EasyDigitalDownloads.php
1 year ago
Elementor.php
3 years ago
Inserter.php
3 years ago
InstallSkin.php
5 years ago
InstallSkinCompat.php
5 years ago
MailPoet.php
1 year ago
MemberPress.php
2 years ago
Menu.php
1 year ago
Notifications.php
1 year ago
OmuApi.php
4 years ago
Output.php
1 year ago
Pages.php
1 year ago
Partners.php
1 year ago
Plugins.php
2 years ago
Promos.php
3 years ago
Refresh.php
1 year ago
RestApi.php
1 year ago
RevenueAttribution.php
4 years ago
Review.php
4 years ago
Rules.php
1 year ago
Save.php
2 years ago
Shortcode.php
4 years ago
Sites.php
1 year ago
Support.php
1 year ago
Type.php
3 years ago
Urls.php
1 year ago
Utils.php
1 year ago
Validate.php
2 years ago
WPForms.php
2 years ago
Welcome.php
4 years ago
Widget.php
4 years ago
WooCommerce.php
1 year ago
Wordfence.php
3 years ago
WpErrorException.php
5 years ago
Actions.php
282 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Actions class. |
| 4 | * |
| 5 | * @since 1.0.0 |
| 6 | * |
| 7 | * @package OMAPI |
| 8 | * @author Thomas Griffin |
| 9 | */ |
| 10 | |
| 11 | // Exit if accessed directly. |
| 12 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | exit; |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * Actions class. |
| 18 | * |
| 19 | * @since 1.0.0 |
| 20 | */ |
| 21 | class OMAPI_Actions { |
| 22 | |
| 23 | /** |
| 24 | * Holds the class object. |
| 25 | * |
| 26 | * @since 1.0.0 |
| 27 | * |
| 28 | * @var object |
| 29 | */ |
| 30 | public static $instance; |
| 31 | |
| 32 | /** |
| 33 | * Path to the file. |
| 34 | * |
| 35 | * @since 1.0.0 |
| 36 | * |
| 37 | * @var string |
| 38 | */ |
| 39 | public $file = __FILE__; |
| 40 | |
| 41 | /** |
| 42 | * Holds the base class object. |
| 43 | * |
| 44 | * @since 1.0.0 |
| 45 | * |
| 46 | * @var object |
| 47 | */ |
| 48 | public $base; |
| 49 | |
| 50 | /** |
| 51 | * Primary class constructor. |
| 52 | * |
| 53 | * @since 1.0.0 |
| 54 | */ |
| 55 | public function __construct() { |
| 56 | |
| 57 | // Set our object. |
| 58 | $this->set(); |
| 59 | |
| 60 | // Add validation messages. |
| 61 | add_action( 'admin_init', array( $this, 'maybe_fetch_missing_data' ), 99 ); |
| 62 | |
| 63 | // We can run upgrade routines on cron runs and admin requests. |
| 64 | if ( defined( 'DOING_CRON' ) && DOING_CRON ) { |
| 65 | add_action( 'optin_monster_api_global_loaded', array( $this, 'check_upgrade_routines' ), 99 ); |
| 66 | } else { |
| 67 | add_action( 'admin_init', array( $this, 'check_upgrade_routines_admin' ), 100 ); |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Sets our object instance and base class instance. |
| 73 | * |
| 74 | * @since 1.0.0 |
| 75 | */ |
| 76 | public function set() { |
| 77 | self::$instance = $this; |
| 78 | $this->base = OMAPI::get_instance(); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * When the plugin is first installed |
| 83 | * Or Migrated from a pre-1.8.0 version |
| 84 | * We need to fetch some additional data |
| 85 | * |
| 86 | * @since 1.8.0 |
| 87 | * |
| 88 | * @return void |
| 89 | */ |
| 90 | public function maybe_fetch_missing_data() { |
| 91 | $creds = $this->base->get_api_credentials(); |
| 92 | $option = $this->base->get_option(); |
| 93 | $changed = false; |
| 94 | |
| 95 | // If we don't have an API Key yet, we can't fetch anything else. |
| 96 | if ( empty( $creds['apikey'] ) && empty( $creds['user'] ) && empty( $creds['key'] ) ) { |
| 97 | return; |
| 98 | } |
| 99 | |
| 100 | // Fetch the userId and accountId, if we don't have them. |
| 101 | if ( |
| 102 | empty( $option['userId'] ) |
| 103 | || empty( $option['accountId'] ) |
| 104 | || empty( $option['accountUserId'] ) |
| 105 | || empty( $option['currentLevel'] ) |
| 106 | || empty( $option['plan'] ) |
| 107 | || empty( $creds['apikey'] ) |
| 108 | ) { |
| 109 | $result = OMAPI_Api::fetch_me( $option, $creds ); |
| 110 | |
| 111 | if ( ! is_wp_error( $result ) ) { |
| 112 | $changed = true; |
| 113 | $option = $result; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | // Fetch the SiteIds for this site, if we don't have them. |
| 118 | if ( |
| 119 | empty( $option['siteIds'] ) |
| 120 | || empty( $option['siteId'] ) |
| 121 | || $this->site_ids_are_numeric( $option['siteIds'] ) |
| 122 | || ! isset( $option['apiCname'] ) |
| 123 | ) { |
| 124 | |
| 125 | $result = $this->base->sites->fetch(); |
| 126 | if ( ! is_wp_error( $result ) ) { |
| 127 | $option = array_merge( $option, $result ); |
| 128 | $changed = true; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | // Only update the option if we've changed something. |
| 133 | if ( $changed ) { |
| 134 | update_option( 'optin_monster_api', $option ); |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * In one version of the Plugin, we fetched the numeric SiteIds, |
| 140 | * But we actually needed the alphanumeric SiteIds. |
| 141 | * |
| 142 | * So we use this check to determine if we need to re-fetch Site Ids. |
| 143 | * |
| 144 | * @param array $site_ids Site ids to convert. |
| 145 | * @return bool True if the ids are numeric. |
| 146 | */ |
| 147 | protected function site_ids_are_numeric( $site_ids ) { |
| 148 | foreach ( $site_ids as $id ) { |
| 149 | if ( ! ctype_digit( (string) $id ) ) { |
| 150 | return false; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | return true; |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Runs upgrade routines in the admin, and refreshes the page if needed |
| 159 | * (if options changed, etc). |
| 160 | * |
| 161 | * @since 2.6.5 |
| 162 | * |
| 163 | * @return void |
| 164 | */ |
| 165 | public function check_upgrade_routines_admin() { |
| 166 | $refresh = $this->check_upgrade_routines(); |
| 167 | if ( $refresh ) { |
| 168 | wp_safe_redirect( esc_url_raw( add_query_arg( 'om', 1 ) ) ); |
| 169 | exit; |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Handles running the upgrade routines for each version. |
| 175 | * |
| 176 | * @since 2.6.5 |
| 177 | * |
| 178 | * @return bool Whether page should be refreshed. |
| 179 | */ |
| 180 | public function check_upgrade_routines() { |
| 181 | $in_progress = get_option( 'optinmonster_current_upgrade' ); |
| 182 | if ( ! empty( $in_progress ) ) { |
| 183 | return false; |
| 184 | } |
| 185 | |
| 186 | $refresh = false; |
| 187 | $plugin_version = $this->base->version; |
| 188 | $upgrade_completed = get_option( 'optinmonster_upgrade_completed', 0 ); |
| 189 | $upgrade_map = array( |
| 190 | '2.6.5' => 'v265_upgrades', |
| 191 | '2.9.0' => 'v290_upgrades', |
| 192 | ); |
| 193 | foreach ( $upgrade_map as $upgrade_version => $method ) { |
| 194 | if ( |
| 195 | version_compare( $plugin_version, $upgrade_version, '>=' ) |
| 196 | && version_compare( $upgrade_completed, $upgrade_version, '<' ) |
| 197 | ) { |
| 198 | update_option( 'optinmonster_current_upgrade', $upgrade_version ); |
| 199 | $refresh = $this->{$method}(); |
| 200 | delete_option( 'optinmonster_current_upgrade' ); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | if ( (string) $plugin_version !== (string) $upgrade_completed ) { |
| 205 | if ( empty( $this->base->notifications ) ) { |
| 206 | $this->base->notifications = new OMAPI_Notifications(); |
| 207 | } |
| 208 | $this->base->notifications->update(); |
| 209 | update_option( 'optinmonster_upgrade_completed', $plugin_version ); |
| 210 | } |
| 211 | |
| 212 | return $refresh; |
| 213 | } |
| 214 | |
| 215 | /** |
| 216 | * Upgrades for version 2.6.5. |
| 217 | * |
| 218 | * @since 2.6.5 |
| 219 | * |
| 220 | * @return bool Whether upgrade routine was completed successfully. |
| 221 | */ |
| 222 | public function v265_upgrades() { |
| 223 | $creds = $this->base->get_api_credentials(); |
| 224 | |
| 225 | // Missing previous api key to verify. |
| 226 | if ( empty( $creds['apikey'] ) ) { |
| 227 | return false; |
| 228 | } |
| 229 | |
| 230 | $api = OMAPI_Api::build( 'v1', 'verify/', 'POST', $creds ); |
| 231 | $results = $api->request(); |
| 232 | |
| 233 | // Current key is fine. |
| 234 | if ( ! is_wp_error( $results ) ) { |
| 235 | return false; |
| 236 | } |
| 237 | |
| 238 | $error_code = ! empty( $api->response_body->code ) |
| 239 | ? $api->response_body->code |
| 240 | : 0; |
| 241 | if ( |
| 242 | in_array( (string) $api->response_code, array( '410', '401', '424', '403' ), true ) |
| 243 | && '10051' === (string) $error_code |
| 244 | ) { |
| 245 | OMAPI_ApiKey::regenerate( $creds['apikey'] ); |
| 246 | |
| 247 | // Regenerated, so we want to refresh the page. |
| 248 | return true; |
| 249 | } |
| 250 | |
| 251 | // No luck. |
| 252 | return false; |
| 253 | } |
| 254 | |
| 255 | /** |
| 256 | * Upgrades for version 2.9.0. |
| 257 | * |
| 258 | * This adds an admin_url to the site. |
| 259 | * |
| 260 | * @since 2.9.0 |
| 261 | * |
| 262 | * @return bool Whether upgrade routine was completed successfully. |
| 263 | */ |
| 264 | public function v290_upgrades() { |
| 265 | $creds = $this->base->get_api_credentials(); |
| 266 | $site_id = $this->base->get_site_id(); |
| 267 | |
| 268 | if ( empty( $creds['apikey'] ) || empty( $site_id ) ) { |
| 269 | return false; |
| 270 | } |
| 271 | |
| 272 | $args = array( |
| 273 | 'admin_url' => esc_url_raw( get_admin_url() ), |
| 274 | ); |
| 275 | |
| 276 | $api = OMAPI_Api::build( 'v2', 'sites/' . $site_id, 'PUT', $creds ); |
| 277 | $results = $api->request( $args ); |
| 278 | |
| 279 | return ! is_wp_error( $results ); |
| 280 | } |
| 281 | } |
| 282 |