/** * Refresh the optins. * * @since 1.0.0 */ public function refresh() { $creds = $this->base->get_api_credentials(); $api = new OMAPI_Api('optins', array('user' => $creds['user'], 'key' => $creds['key']), 'GET'); $ret = $api->request(); if (is_wp_error($ret)) { // If no optins available, make sure they get deleted. if ('optins' == $ret->get_error_code()) { $this->base->save->store_optins(array()); } // Set an error message. $this->error = $ret->get_error_message(); add_action('optin_monster_api_messages_' . $this->view, array($this, 'error')); } else { // Store the optin data. $this->base->save->store_optins($ret); // Update the option to remove stale error messages. $option = $this->base->get_option(); $option['is_invalid'] = false; $option['is_expired'] = false; $option['is_disabled'] = false; update_option('optin_monster_api', $option); // Set a message. add_action('optin_monster_api_messages_' . $this->view, array($this, 'message')); } }
/** * Validate API credentials. * * @since 1.0.0 */ public function validate() { $creds = $this->base->get_api_credentials(); $api = new OMAPI_Api('validate', array('user' => $creds['user'], 'key' => $creds['key'])); $ret = $api->request(); if (is_wp_error($ret)) { $option = $this->base->get_option(); $type = $ret->get_error_code(); switch ($type) { case 'missing': case 'auth': // Set option values. $option['is_invalid'] = true; $option['is_expired'] = false; $option['is_disabled'] = false; break; case 'disabled': // Set option values. $option['is_invalid'] = false; $option['is_expired'] = false; $option['is_disabled'] = true; break; case 'expired': // Set option values. $option['is_invalid'] = false; $option['is_expired'] = true; $option['is_disabled'] = false; break; } // Update option. update_option('optin_monster_api', $option); // Set our transient to run again in an hour. set_transient('_omapi_validate', true, HOUR_IN_SECONDS); } else { set_transient('_omapi_validate', true, DAY_IN_SECONDS); } }
/** * Save the plugin options. * * @since 1.0.0 */ public function save() { // Prepare variables. $data = stripslashes_deep($_POST['omapi'][$this->view]); // Save the data. switch ($this->view) { case 'api': // Create a new API instance to verify API credentials. $option = $this->base->get_option(); $user = isset($data['user']) ? $data['user'] : false; $key = isset($data['key']) ? $data['key'] : false; $old_user = isset($option['api']['user']) ? $option['api']['user'] : false; $old_key = isset($option['api']['key']) ? $option['api']['key'] : false; // If one or both items are missing, fail. if (!$user || !$key) { // If it had been stored and it is now empty, reset the keys altogether. if (!$user && $old_user || !$key && $old_key) { $option['api']['user'] = ''; $option['api']['key'] = ''; // Allow option to be filtered before saving. $option = apply_filters('optin_monster_api_save', $option, $data, $this->view); // Save the option. update_option('optin_monster_api', $option); } else { $this->errors['error'] = __('You must provide a valid API Username and API Key to authenticate to OptinMonster.', 'optin-monster-api'); } } else { if ($user != $old_user || $key != $old_key) { $api = new OMAPI_Api('verify', array('user' => $user, 'key' => $key)); $ret = $api->request(); if (is_wp_error($ret)) { $this->errors['error'] = $ret->get_error_message(); } else { // This user and key are good to go! $option['api']['user'] = $user; $option['api']['key'] = $key; // Remove any error messages. $option['is_invalid'] = false; $option['is_expired'] = false; $option['is_disabled'] = false; // Store the optin data. $this->store_optins($ret); // Allow option to be filtered before saving. $option = apply_filters('optin_monster_api_save', $option, $data, $this->view); // Save the option. update_option('optin_monster_api', $option); } } } break; case 'optins': // Prepare variables. $data['categories'] = isset($_POST['post_category']) ? stripslashes_deep($_POST['post_category']) : array(); $data['taxonomies'] = isset($_POST['tax_input']) ? stripslashes_deep($_POST['tax_input']) : array(); $optin_id = absint($_GET['optin_monster_api_id']); $fields = array(); $fields['enabled'] = isset($data['enabled']) ? 1 : 0; $fields['global'] = isset($data['global']) ? 1 : 0; $fields['automatic'] = isset($data['automatic']) ? 1 : 0; $fields['users'] = isset($data['users']) ? esc_attr($data['users']) : 'all'; $fields['never'] = isset($data['never']) ? explode(',', $data['never']) : array(); $fields['only'] = isset($data['only']) ? explode(',', $data['only']) : array(); $fields['categories'] = isset($data['categories']) ? $data['categories'] : array(); $fields['taxonomies'] = isset($data['taxonomies']) ? $data['taxonomies'] : array(); $fields['show'] = isset($data['show']) ? $data['show'] : array(); $fields['shortcode'] = isset($data['shortcode']) ? 1 : 0; if (class_exists('WYSIJA')) { $fields['mailpoet'] = isset($data['mailpoet']) ? 1 : 0; $fields['mailpoet_list'] = isset($data['mailpoet_list']) ? esc_attr($data['mailpoet_list']) : 'none'; } // Allow fields to be filtered. $fields = apply_filters('optin_monster_save_fields', $fields, $optin_id); // Loop through each field and save the data. foreach ($fields as $key => $val) { update_post_meta($optin_id, '_omapi_' . $key, $val); } break; case 'settings': $option = $this->base->get_option(); $option['settings']['cookies'] = isset($data['cookies']) ? 1 : 0; // Allow option to be filtered before saving. $option = apply_filters('optin_monster_api_save', $option, $data, $this->view); // Save the option. update_option('optin_monster_api', $option); break; } // If selected, clear out all local cookies. if ($this->base->get_option('settings', 'cookies')) { $this->base->actions->cookies(); } // Add message to show error or success messages. if (!empty($this->errors)) { add_action('optin_monster_api_messages_' . $this->view, array($this, 'errors')); } else { // Add a success message. add_action('optin_monster_api_messages_' . $this->view, array($this, 'message')); } }
/** * Runs the migration through OMAPI_Api * * @since 1.0.0 * * @return bool */ public function run() { $this->api->set_additional_data($this->data); $response = $this->api->request(); if (is_wp_error($response)) { return false; } $migration_data = get_option('_om_migration_data', array('migrated_optins' => array())); $migration_data['errors'] = false; if (property_exists($response, 'imported_optins')) { $migration_data['migrated_optins'] = array_merge($migration_data['migrated_optins'], $response->imported_optins); } if (property_exists($response, 'integrations')) { $migration_data['integrations'] = $response->integrations; } if (property_exists($response, 'site')) { $migration_data['site'] = $response->site; } if (property_exists($response, 'errors')) { foreach ($response->errors as $error) { $migration_data['errors'][] = $error; } } if (property_exists($response, 'new_optins') && $response->new_optins) { foreach ($response->new_optins as $slug => $optin) { // Maybe update an optin rather than add a new one. $local = $this->base->get_optin_by_slug($slug); $data = array(); if ($local) { $data['ID'] = $local->ID; $data['post_title'] = $optin->title; $data['post_content'] = $optin->output; $data['post_status'] = 'publish'; wp_update_post($data); update_post_meta($local->ID, '_omapi_type', $optin->type); update_post_meta($local->ID, '_omapi_ids', $optin->ids); $post_id = $local->ID; } else { $data['post_name'] = $slug; $data['post_title'] = $optin->title; $data['post_excerpt'] = $optin->id; $data['post_content'] = $optin->output; $data['post_status'] = 'publish'; $data['post_type'] = 'omapi'; $post_id = wp_insert_post($data); update_post_meta($post_id, '_omapi_type', $optin->type); update_post_meta($post_id, '_omapi_ids', $optin->ids); } // Now that the data has been saved, let's now try to grab meta from the previous optin for output settings. $prev_optin = get_posts(array('post_type' => 'optin', 'posts_per_page' => 1, 'no_found_rows' => true, 'cache_results' => false, 'name' => $slug)); if (empty($prev_optin)) { continue; } // Now grab all the meta for the optin. $prev_optin = $prev_optin[0]; $meta = get_post_meta($prev_optin->ID, '_om_meta', true); $test = get_post_meta($prev_optin->ID, '_om_test_mode', true); $fields = $this->base->output->fields; // Get all the new fields available and store the data. foreach ($fields as $field) { $value = false; switch ($field) { case 'enabled': $value = false; // Make sure the optins are disabled by default when being added back. break; case 'global': $value = isset($meta['display']['global']) && $meta['display']['global'] ? true : false; break; case 'automatic': $value = isset($meta['display']['automatic']) && $meta['display']['automatic'] ? true : false; break; case 'users': $value = isset($meta['logged_in']) && $meta['logged_in'] ? 'out' : 'all'; break; case 'never': $value = !empty($meta['display']['never']) ? $meta['display']['never'] : array(); break; case 'only': $value = !empty($meta['display']['exclusive']) ? $meta['display']['exclusive'] : array(); break; case 'categories': $value = !empty($meta['display']['categories']) ? $meta['display']['categories'] : array(); break; case 'show': $value = !empty($meta['display']['show']) ? $meta['display']['show'] : array(); break; case 'test': $value = $test ? true : false; break; case 'shortcode': $value = false; break; case 'mailpoet': $provider = isset($meta['email']['provider']) ? $meta['email']['provider'] : 'none'; $value = 'mailpoet' == $provider ? true : false; // If true, we need to set the MailPoet list as well. if ($value) { $list = isset($meta['email']['list_id']) ? $meta['email']['list_id'] : false; if ($list) { update_post_meta($post_id, '_omapi_mailpoet_list', $list); } } break; case 'type': case 'ids': $value = get_post_meta($post_id, '_omapi_' . $field, true); break; } update_post_meta($post_id, '_omapi_' . $field, $value); } } } update_option('_om_migration_data', $migration_data); return true; }