admin
10 months ago
core
10 months ago
class-activator.php
10 months ago
class-assets.php
11 months ago
class-builder-page.php
1 year ago
class-deactivator.php
3 years ago
class-debug-info.php
2 years ago
class-feed-ajax.php
1 year ago
class-feed-block.php
1 year ago
class-feed-deserializer.php
11 months ago
class-feed-old.php
4 years ago
class-feed-page.php
11 months ago
class-feed-serializer.php
2 years ago
class-feed-shortcode.php
2 years ago
class-feed-widget.php
4 years ago
class-plugin-overview-ajax.php
1 year ago
class-plugin-overview.php
11 months ago
class-plugin-settings.php
10 months ago
class-plugin-support.php
1 year ago
class-plugin.php
11 months ago
class-post-types.php
3 years ago
class-reviews-cron.php
1 year ago
class-settings-save.php
1 year ago
class-view.php
10 months ago
index.php
4 years ago
page-setting-advance.php
1 year ago
page-setting-fig.php
1 year ago
page-setting-support.php
4 years ago
class-settings-save.php
186 lines
| 1 | <?php |
| 2 | |
| 3 | namespace WP_Rplg_Google_Reviews\Includes; |
| 4 | |
| 5 | class Settings_Save { |
| 6 | |
| 7 | private $activator; |
| 8 | private $reviews_cron; |
| 9 | |
| 10 | public function __construct(Activator $activator, Reviews_Cron $reviews_cron) { |
| 11 | $this->activator = $activator; |
| 12 | $this->reviews_cron = $reviews_cron; |
| 13 | } |
| 14 | |
| 15 | public function register() { |
| 16 | add_action('admin_post_grw_settings_save', array($this, 'save_from_post_array')); |
| 17 | } |
| 18 | |
| 19 | public function save_from_post_array() { |
| 20 | global $wpdb; |
| 21 | |
| 22 | if (!function_exists('wp_nonce_field')) { |
| 23 | function wp_nonce_field() {} |
| 24 | } |
| 25 | |
| 26 | if (!current_user_can('manage_options')) { |
| 27 | die('The account you\'re logged in to doesn\'t have permission to access this page.'); |
| 28 | } |
| 29 | |
| 30 | if (!empty($_POST)) { |
| 31 | $nonce_result_check = $this->check_nonce(); |
| 32 | if ($nonce_result_check === false) { |
| 33 | die('Unable to save changes. Make sure you are accessing this page from the Wordpress dashboard.'); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | $notice_code = null; |
| 38 | update_option('grw_notice_type', 'success'); |
| 39 | |
| 40 | if (isset($_POST['active']) && isset($_GET['active'])) { |
| 41 | $active = $_GET['active'] == '1' ? '1' : '0'; |
| 42 | update_option('grw_active', $active); |
| 43 | $notice_code = 'settings_active_' . $active; |
| 44 | } |
| 45 | |
| 46 | if (isset($_POST['save'])) { |
| 47 | $notice_code = 'settings_save'; |
| 48 | $fields = array('grw_async_css', 'grw_demand_assets', 'grw_freq_revs_upd', 'grw_gpa_old', 'grw_google_api_key'); |
| 49 | foreach ($fields as $field) { |
| 50 | |
| 51 | if (isset($_POST[$field])) { |
| 52 | $value = trim(sanitize_text_field(wp_unslash($_POST[$field]))); |
| 53 | update_option($field, $value); |
| 54 | |
| 55 | // If save Google API key automatically enable a reviews update cron |
| 56 | if ($field == 'grw_google_api_key' && strlen($value) > 0) { |
| 57 | update_option('grw_revupd_cron', '1'); |
| 58 | |
| 59 | // Test Google API key |
| 60 | $gpa_old = get_option('grw_gpa_old'); |
| 61 | if ($gpa_old === 'true') { |
| 62 | $res = wp_remote_get(GRW_GOOGLE_PLACE_API . 'details/json?placeid=ChIJ3TH9CwFZwokRIvNO1SP0WLg&key=' . $value); |
| 63 | } else { |
| 64 | $res = wp_remote_get(GRW_GOOGLE_PLACE_API_NEW . 'ChIJ3TH9CwFZwokRIvNO1SP0WLg?fields=rating&key=' . $value); |
| 65 | } |
| 66 | $body = wp_remote_retrieve_body($res); |
| 67 | $body_json = json_decode($body); |
| 68 | if ($body_json) { |
| 69 | $error = isset($body_json->error) ? $body_json->error->message : (isset($body_json->error_message) ? $body_json->error_message : ''); |
| 70 | if (!empty($error)) { |
| 71 | update_option('grw_notice_msg', $error); |
| 72 | update_option('grw_notice_type', 'error'); |
| 73 | $notice_code = 'custom_msg'; |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | // Change reviews update cron frequency |
| 79 | if ($field == 'grw_freq_revs_upd' && strlen($value) > 0) { |
| 80 | $this->reviews_cron->deactivate(); |
| 81 | $this->reviews_cron->activate(); |
| 82 | } |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | if (isset($_POST['create_db'])) { |
| 88 | $this->activator->create_db(); |
| 89 | $notice_code = 'settings_create_db'; |
| 90 | } |
| 91 | |
| 92 | if (isset($_POST['install'])) { |
| 93 | $install_multisite = isset($_POST['install_multisite']) ? sanitize_text_field(wp_unslash($_POST['install_multisite'])) : null; |
| 94 | $this->activator->drop_db($install_multisite); |
| 95 | $this->activator->delete_all_options($install_multisite); |
| 96 | $this->activator->delete_all_feeds($install_multisite); |
| 97 | $this->activator->activate(); |
| 98 | $this->reviews_cron->deactivate(); |
| 99 | $notice_code = 'settings_install'; |
| 100 | } |
| 101 | |
| 102 | if (isset($_POST['reset_all'])) { |
| 103 | $reset_all_multisite = isset($_POST['reset_all_multisite']) ? sanitize_text_field(wp_unslash($_POST['reset_all_multisite'])) : false; |
| 104 | $this->activator->drop_db($reset_all_multisite); |
| 105 | $this->activator->delete_all_options($reset_all_multisite); |
| 106 | $this->activator->delete_all_feeds($reset_all_multisite); |
| 107 | $this->reviews_cron->deactivate(); |
| 108 | $notice_code = 'settings_reset_all'; |
| 109 | } |
| 110 | |
| 111 | if (isset($_POST['debug_mode'])) { |
| 112 | $debug_mode = $_POST['debug_mode'] == 'Enable' ? '1' : '0'; |
| 113 | update_option('grw_debug_mode', $debug_mode); |
| 114 | $notice_code = 'settings_debug_mode_' . $debug_mode; |
| 115 | } |
| 116 | |
| 117 | if (isset($_POST['update_db_ver']) && isset($_POST['update_db'])) { |
| 118 | $update_db_ver = sanitize_text_field(wp_unslash($_POST['update_db_ver'])); |
| 119 | if (strlen($update_db_ver) > 0) { |
| 120 | $this->activator->update_db($update_db_ver); |
| 121 | $notice_code = 'settings_update_db'; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | if (isset($_POST['del_dup_revs'])) { |
| 126 | $this->activator->delete_duplicates(); |
| 127 | $notice_code = 'settings_del_dup_revs'; |
| 128 | } |
| 129 | |
| 130 | if (isset($_POST['revupd_cron'])) { |
| 131 | $revupd_cron = $_POST['revupd_cron'] == 'Enable' ? '1' : '0'; |
| 132 | if ($revupd_cron == '0') { |
| 133 | $this->reviews_cron->deactivate(); |
| 134 | } |
| 135 | update_option('grw_revupd_cron', $revupd_cron); |
| 136 | $notice_code = 'settings_revupd_cron_' . $revupd_cron; |
| 137 | |
| 138 | /*$api_key = get_option('grw_google_api_key'); |
| 139 | if ($api_key) { |
| 140 | update_option('grw_revupd_cron', $revupd_cron); |
| 141 | $notice_code = 'settings_revupd_cron_' . $revupd_cron; |
| 142 | } else { |
| 143 | update_option('grw_notice_type', 'error'); |
| 144 | update_option('grw_notice_msg', 'To make the reviews automatically updated, please create your own Google API key. The extrimly detailed instruction how to do it, you can <a href="' . admin_url('admin.php?page=grw-support&grw_tab=fig#fig_api_key') . '" target="_blank">find here</a>.'); |
| 145 | $notice_code = 'custom_msg'; |
| 146 | }*/ |
| 147 | } |
| 148 | |
| 149 | $this->redirect_to_tab($notice_code); |
| 150 | } |
| 151 | |
| 152 | public function redirect_to_tab($notice_code = '') { |
| 153 | if (empty($_GET['grw_tab'])) { |
| 154 | wp_safe_redirect(wp_get_referer()); |
| 155 | exit; |
| 156 | } |
| 157 | |
| 158 | $tab = sanitize_text_field(wp_unslash($_GET['grw_tab'])); |
| 159 | |
| 160 | $query_args = array( |
| 161 | 'grw_tab' => $tab, |
| 162 | ); |
| 163 | |
| 164 | if (!empty($notice_code)) { |
| 165 | $query_args['grw_notice'] = $notice_code; |
| 166 | } |
| 167 | |
| 168 | wp_safe_redirect(add_query_arg($query_args, wp_get_referer())); |
| 169 | exit; |
| 170 | } |
| 171 | |
| 172 | private function check_nonce() { |
| 173 | $nonce_actions = array('active', 'save', 'create_db', 'reset', 'reset_all', 'debug_mode', 'update_db', 'del_dup_revs'); |
| 174 | $nonce_form_prefix = 'grw-form_nonce_'; |
| 175 | $nonce_action_prefix = 'grw-wpnonce_'; |
| 176 | foreach ($nonce_actions as $key => $value) { |
| 177 | if (isset($_POST[$nonce_form_prefix.$value])) { |
| 178 | check_admin_referer($nonce_action_prefix.$value, $nonce_form_prefix.$value); |
| 179 | return true; |
| 180 | } |
| 181 | } |
| 182 | return false; |
| 183 | } |
| 184 | |
| 185 | } |
| 186 |