| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; |
| 4 |
} |
| 5 |
|
| 6 |
/** |
| 7 |
* GmediaProcessor_Settings |
| 8 |
*/ |
| 9 |
class GmediaProcessor_Settings extends GmediaProcessor { |
| 10 |
private static $me = null; |
| 11 |
|
| 12 |
public static function getMe() { |
| 13 |
if ( null === self::$me ) { |
| 14 |
self::$me = new GmediaProcessor_Settings(); |
| 15 |
} |
| 16 |
|
| 17 |
return self::$me; |
| 18 |
} |
| 19 |
|
| 20 |
protected function processor() { |
| 21 |
global $gmCore, $gmGallery, $gmDB; |
| 22 |
|
| 23 |
if ( ! $gmCore->caps['gmedia_settings'] ) { |
| 24 |
wp_die( esc_html__( 'You are not allowed to change gmedia settings', 'grand-media' ) ); |
| 25 |
} |
| 26 |
$lk_check = isset( $_POST['license-key-activate'] ); |
| 27 |
if ( isset( $_POST['gmedia_settings_save'] ) ) { |
| 28 |
check_admin_referer( 'gmedia_settings', '_wpnonce_settings' ); |
| 29 |
|
| 30 |
$set = $gmCore->_post( 'set', array() ); |
| 31 |
|
| 32 |
if ( ! empty( $set['purchase_key'] ) && ( empty( $set['license_key'] ) || empty( $set['license_key2'] ) ) ) { |
| 33 |
$lk_check = true; |
| 34 |
} |
| 35 |
if ( empty( $set['purchase_key'] ) && ( ! empty( $set['license_key'] ) || ! empty( $set['license_key2'] ) ) ) { |
| 36 |
$set['license_name'] = ''; |
| 37 |
$set['purchase_key'] = ''; |
| 38 |
$set['license_key'] = ''; |
| 39 |
$set['license_key2'] = ''; |
| 40 |
$this->error[] = esc_html__( 'License Key empty...', 'grand-media' ); |
| 41 |
} |
| 42 |
|
| 43 |
$set['google_api_key'] = trim( $set['google_api_key'] ); |
| 44 |
|
| 45 |
$flush_rewrite_rules = false; |
| 46 |
if ( empty( $set['endpoint'] ) ) { |
| 47 |
$set['endpoint'] = 'gmedia'; |
| 48 |
} |
| 49 |
if ( |
| 50 |
$set['endpoint'] !== $gmGallery->options['endpoint'] |
| 51 |
|| $set['gmedia_post_slug'] !== $gmGallery->options['gmedia_post_slug'] |
| 52 |
|| $set['gmedia_album_post_slug'] !== $gmGallery->options['gmedia_album_post_slug'] |
| 53 |
|| $set['gmedia_gallery_post_slug'] !== $gmGallery->options['gmedia_gallery_post_slug'] |
| 54 |
|| $set['gmedia_has_archive'] !== $gmGallery->options['gmedia_has_archive'] |
| 55 |
|| $set['gmedia_album_has_archive'] !== $gmGallery->options['gmedia_album_has_archive'] |
| 56 |
|| $set['gmedia_gallery_has_archive'] !== $gmGallery->options['gmedia_gallery_has_archive'] |
| 57 |
) { |
| 58 |
$flush_rewrite_rules = true; |
| 59 |
$set['flush_rewrite_rules'] = true; |
| 60 |
} |
| 61 |
|
| 62 |
foreach ( $set as $key => $val ) { |
| 63 |
$gmGallery->options[ $key ] = $val; |
| 64 |
} |
| 65 |
|
| 66 |
$capabilities = $gmCore->_post( 'capability', array() ); |
| 67 |
if ( ! empty( $capabilities ) && current_user_can( 'manage_options' ) ) { |
| 68 |
global $wp_roles; |
| 69 |
$_roles = $wp_roles->roles; |
| 70 |
$_roles = array_keys( apply_filters( 'editable_roles', $_roles ) ); |
| 71 |
$roles = array_flip( $_roles ); |
| 72 |
|
| 73 |
// upload cap. |
| 74 |
if ( $roles[ $capabilities['gmedia_upload'] ] < $roles[ $capabilities['gmedia_import'] ] ) { |
| 75 |
$capabilities['gmedia_import'] = $capabilities['gmedia_upload']; |
| 76 |
} |
| 77 |
// edit/delete cap. |
| 78 |
if ( $roles[ $capabilities['gmedia_edit_media'] ] < $roles[ $capabilities['gmedia_edit_others_media'] ] ) { |
| 79 |
$capabilities['gmedia_edit_others_media'] = $capabilities['gmedia_edit_media']; |
| 80 |
} |
| 81 |
if ( $roles[ $capabilities['gmedia_edit_media'] ] < $roles[ $capabilities['gmedia_delete_media'] ] ) { |
| 82 |
$capabilities['gmedia_delete_media'] = $capabilities['gmedia_edit_media']; |
| 83 |
} |
| 84 |
if ( $roles[ $capabilities['gmedia_delete_media'] ] < $roles[ $capabilities['gmedia_delete_others_media'] ] ) { |
| 85 |
$capabilities['gmedia_delete_others_media'] = $capabilities['gmedia_delete_media']; |
| 86 |
} |
| 87 |
if ( $roles[ $capabilities['gmedia_edit_others_media'] ] < $roles[ $capabilities['gmedia_delete_others_media'] ] ) { |
| 88 |
$capabilities['gmedia_delete_others_media'] = $capabilities['gmedia_edit_others_media']; |
| 89 |
} |
| 90 |
if ( $roles[ $capabilities['gmedia_show_others_media'] ] < $roles[ $capabilities['gmedia_edit_others_media'] ] ) { |
| 91 |
$capabilities['gmedia_edit_others_media'] = $capabilities['gmedia_show_others_media']; |
| 92 |
} |
| 93 |
if ( $roles[ $capabilities['gmedia_show_others_media'] ] < $roles[ $capabilities['gmedia_delete_others_media'] ] ) { |
| 94 |
$capabilities['gmedia_delete_others_media'] = $capabilities['gmedia_show_others_media']; |
| 95 |
} |
| 96 |
// terms cap. |
| 97 |
if ( $roles[ $capabilities['gmedia_terms'] ] < $roles[ $capabilities['gmedia_album_manage'] ] ) { |
| 98 |
$capabilities['gmedia_album_manage'] = $capabilities['gmedia_terms']; |
| 99 |
} |
| 100 |
if ( $roles[ $capabilities['gmedia_terms'] ] < $roles[ $capabilities['gmedia_category_manage'] ] ) { |
| 101 |
$capabilities['gmedia_category_manage'] = $capabilities['gmedia_terms']; |
| 102 |
} |
| 103 |
if ( $roles[ $capabilities['gmedia_terms'] ] < $roles[ $capabilities['gmedia_tag_manage'] ] ) { |
| 104 |
$capabilities['gmedia_tag_manage'] = $capabilities['gmedia_terms']; |
| 105 |
} |
| 106 |
if ( $roles[ $capabilities['gmedia_terms'] ] < $roles[ $capabilities['gmedia_terms_delete'] ] ) { |
| 107 |
$capabilities['gmedia_terms_delete'] = $capabilities['gmedia_terms']; |
| 108 |
} else { |
| 109 |
$rolekey = max( $roles[ $capabilities['gmedia_album_manage'] ], $roles[ $capabilities['gmedia_tag_manage'] ] ); |
| 110 |
$role = $_roles[ $rolekey ]; |
| 111 |
if ( $role < $roles[ $capabilities['gmedia_terms_delete'] ] ) { |
| 112 |
$capabilities['gmedia_terms_delete'] = $role; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
foreach ( $capabilities as $key => $val ) { |
| 117 |
$gmDB->set_capability( $val, $key ); |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
if ( isset( $set['delete_originals'] ) && (int) $set['delete_originals'] ) { |
| 122 |
//if (($handle = opendir($gmCore->upload['path'] . '/' . $gmGallery->options['folder']['image_original']))) { |
| 123 |
// while (false !== ($file = readdir($handle))) { |
| 124 |
// // do something with the file |
| 125 |
// // note that '.' and '..' is returned even |
| 126 |
// wp_delete_file($file); |
| 127 |
// } |
| 128 |
// closedir($handle); |
| 129 |
//} |
| 130 |
$files = glob( $gmCore->upload['path'] . '/' . $gmGallery->options['folder']['image_original'] . '/*', GLOB_NOSORT ); |
| 131 |
if ( ! empty( $files ) ) { |
| 132 |
foreach ( $files as $file ) { |
| 133 |
wp_delete_file( $file ); |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
update_option( 'gmediaOptions', $gmGallery->options ); |
| 139 |
if ( isset( $_POST['GmediaHashID_salt'] ) ) { |
| 140 |
update_option( 'GmediaHashID_salt', (string) $gmCore->_post( 'GmediaHashID_salt', '' ) ); |
| 141 |
} |
| 142 |
gmedia_delete_transients( 'gm_cache' ); |
| 143 |
if ( $flush_rewrite_rules ) { |
| 144 |
flush_rewrite_rules( false ); |
| 145 |
} |
| 146 |
$this->msg[] .= esc_html__( 'Settings saved', 'grand-media' ); |
| 147 |
} |
| 148 |
|
| 149 |
if ( $lk_check ) { |
| 150 |
check_admin_referer( 'gmedia_settings', '_wpnonce_settings' ); |
| 151 |
$license_key = $gmCore->_post( 'set' ); |
| 152 |
if ( ! empty( $license_key['purchase_key'] ) ) { |
| 153 |
global $wp_version; |
| 154 |
$gmedia_ua = "WordPress/{$wp_version} | "; |
| 155 |
$gmedia_ua .= 'Gmedia/' . constant( 'GMEDIA_VERSION' ); |
| 156 |
|
| 157 |
$response = wp_remote_post( |
| 158 |
'https://codeasily.com/rest/gmedia-key.php', |
| 159 |
array( |
| 160 |
'body' => array( 'key' => $license_key['purchase_key'], 'site' => site_url() ), |
| 161 |
'headers' => array( |
| 162 |
'Content-Type' => 'application/x-www-form-urlencoded; ' . 'charset=' . get_option( 'blog_charset' ), |
| 163 |
'Host' => 'codeasily.com', |
| 164 |
'User-Agent' => $gmedia_ua, |
| 165 |
), |
| 166 |
'httpversion' => '1.0', |
| 167 |
'timeout' => 45, |
| 168 |
) |
| 169 |
); |
| 170 |
|
| 171 |
if ( is_wp_error( $response ) ) { |
| 172 |
$this->error[] = $response->get_error_message(); |
| 173 |
$this->error[] = esc_html__( 'Use Help Screen (top right button) for more info', 'grand-media' ); |
| 174 |
} else { |
| 175 |
$result = json_decode( $response['body'] ); |
| 176 |
if ( isset( $result->error ) ) { |
| 177 |
if ( $result && 200 === $result->error->code ) { |
| 178 |
$gmGallery->options['license_name'] = $result->content; |
| 179 |
$gmGallery->options['purchase_key'] = $result->dkey; |
| 180 |
$gmGallery->options['license_key'] = $result->key; |
| 181 |
$gmGallery->options['license_key2'] = $result->key2; |
| 182 |
// translators: link tag. |
| 183 |
$this->msg[] = sprintf( esc_html__( 'License Key activated successfully. You can see all activated websites with this key on your account page %s', 'grand-media' ), '<a href="https://codeasily.com/my-account/" target="_blank">https://codeasily.com/my-account/</a>' ); |
| 184 |
} else { |
| 185 |
$gmGallery->options['license_name'] = ''; |
| 186 |
$gmGallery->options['purchase_key'] = ''; |
| 187 |
$gmGallery->options['license_key'] = ''; |
| 188 |
$gmGallery->options['license_key2'] = ''; |
| 189 |
$this->error[] = esc_html( __( 'Error', 'grand-media' ) . ': ' . $result->error->message ); |
| 190 |
} |
| 191 |
update_option( 'gmediaOptions', $gmGallery->options ); |
| 192 |
} else { |
| 193 |
$this->error[] = esc_html__( 'Something went wrong.. Try again later or use Help Screen (top right button) for more info', 'grand-media' ); |
| 194 |
} |
| 195 |
} |
| 196 |
} else { |
| 197 |
$this->error[] = esc_html__( 'Empty License Key', 'grand-media' ); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
if ( isset( $_POST['gmedia_settings_reset'] ) ) { |
| 202 |
check_admin_referer( 'gmedia_settings', '_wpnonce_settings' ); |
| 203 |
include_once GMEDIA_ABSPATH . 'config/setup.php'; |
| 204 |
$_temp_options = $gmGallery->options; |
| 205 |
$gmGallery->options = gmedia_default_options(); |
| 206 |
// don't reset license. |
| 207 |
$gmGallery->options['license_name'] = $_temp_options['license_name']; |
| 208 |
$gmGallery->options['purchase_key'] = $_temp_options['purchase_key']; |
| 209 |
$gmGallery->options['license_key'] = $_temp_options['license_key']; |
| 210 |
$gmGallery->options['license_key2'] = $_temp_options['license_key2']; |
| 211 |
// don't reset mobile app. |
| 212 |
$gmGallery->options['site_ID'] = $_temp_options['site_ID']; |
| 213 |
$gmGallery->options['mobile_app'] = (int) $_temp_options['mobile_app']; |
| 214 |
if ( $gmGallery->options['mobile_app'] && isset( $_temp_options['gmedia_service'] ) ) { |
| 215 |
$gmGallery->options['gmedia_service'] = $_temp_options['gmedia_service']; |
| 216 |
} |
| 217 |
delete_metadata( 'user', 0, 'gm_screen_options', '', true ); |
| 218 |
update_option( 'gmediaOptions', $gmGallery->options ); |
| 219 |
|
| 220 |
if ( $gmCore->_post( 'capability' ) && current_user_can( 'manage_options' ) ) { |
| 221 |
$capabilities = $gmCore->plugin_capabilities(); |
| 222 |
$capabilities = apply_filters( 'gmedia_capabilities', $capabilities ); |
| 223 |
//$role = get_role('administrator'); |
| 224 |
foreach ( $capabilities as $cap ) { |
| 225 |
$gmDB->set_capability( 'administrator', $cap ); |
| 226 |
} |
| 227 |
} |
| 228 |
gmedia_delete_transients( 'gm_cache' ); |
| 229 |
$this->msg[] .= esc_html__( 'All settings set to default', 'grand-media' ); |
| 230 |
} |
| 231 |
|
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
global $gmProcessorSettings; |
| 236 |
$gmProcessorSettings = GmediaProcessor_Settings::getMe(); |
| 237 |
|