class-affiliate.php
3 years ago
class-polylang.php
3 years ago
class-review-box.php
3 years ago
class-upgrade-box.php
3 years ago
class-wpml.php
3 years ago
folders.class.php
3 years ago
form.class.php
3 years ago
media.replace.php
3 years ago
plugins.class.php
3 years ago
tree.class.php
3 years ago
media.replace.php
1197 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Class Folders Replace Media |
| 4 | * |
| 5 | * @author : Premio <contact@premio.io> |
| 6 | * @license : GPL2 |
| 7 | * */ |
| 8 | |
| 9 | if (! defined('ABSPATH')) { |
| 10 | exit; |
| 11 | } |
| 12 | |
| 13 | class folders_replace_media |
| 14 | { |
| 15 | |
| 16 | /** |
| 17 | * Button Color |
| 18 | * |
| 19 | * @var string $buttonColor Replacement Button Color |
| 20 | * @since 1.0.0 |
| 21 | * @access public |
| 22 | */ |
| 23 | public $buttonColor; |
| 24 | |
| 25 | /** |
| 26 | * Is Replacement functionality enabled or not |
| 27 | * |
| 28 | * @var string $isEnabled Replacement Functionality Status |
| 29 | * @since 1.0.0 |
| 30 | * @access public |
| 31 | */ |
| 32 | public $isEnabled = false; |
| 33 | |
| 34 | /** |
| 35 | * Folders Upgrade Link |
| 36 | * |
| 37 | * @var string $upgradeLink Upgrade Link |
| 38 | * @since 1.0.0 |
| 39 | * @access public |
| 40 | */ |
| 41 | public $upgradeLink; |
| 42 | |
| 43 | /** |
| 44 | * Old file path |
| 45 | * |
| 46 | * @var string $oldFilePath Old File Path |
| 47 | * @since 1.0.0 |
| 48 | * @access public |
| 49 | */ |
| 50 | public $oldFilePath; |
| 51 | |
| 52 | /** |
| 53 | * Old file URL |
| 54 | * |
| 55 | * @var string $oldFilePath Old File URL |
| 56 | * @since 1.0.0 |
| 57 | * @access public |
| 58 | */ |
| 59 | public $oldFileURL; |
| 60 | |
| 61 | /** |
| 62 | * New file Path |
| 63 | * |
| 64 | * @var string $newFilePath New File Path |
| 65 | * @since 1.0.0 |
| 66 | * @access public |
| 67 | */ |
| 68 | public $newFilePath; |
| 69 | |
| 70 | /** |
| 71 | * New file URL |
| 72 | * |
| 73 | * @var string $newFilePath New File URL |
| 74 | * @since 1.0.0 |
| 75 | * @access public |
| 76 | */ |
| 77 | public $newFileURL; |
| 78 | |
| 79 | /** |
| 80 | * Mode for file Replacement |
| 81 | * |
| 82 | * @var string $mode New File URL |
| 83 | * @since 1.0.0 |
| 84 | * @access public |
| 85 | */ |
| 86 | public $mode = "rename-file"; |
| 87 | |
| 88 | /** |
| 89 | * Old image Meta Array |
| 90 | * |
| 91 | * @var array $oldImageMeta Old image Meta Array |
| 92 | * @since 1.0.0 |
| 93 | * @access public |
| 94 | */ |
| 95 | public $oldImageMeta; |
| 96 | |
| 97 | /** |
| 98 | * New image Meta Array |
| 99 | * |
| 100 | * @var array $newImageMeta New image Meta Array |
| 101 | * @since 1.0.0 |
| 102 | * @access public |
| 103 | */ |
| 104 | public $newImageMeta; |
| 105 | |
| 106 | /** |
| 107 | * WordPress Upload Dir Path |
| 108 | * |
| 109 | * @var string $uploadDir WordPress Upload Dir Path |
| 110 | * @since 1.0.0 |
| 111 | * @access public |
| 112 | */ |
| 113 | public $uploadDir; |
| 114 | |
| 115 | /** |
| 116 | * Check if old file is image |
| 117 | * |
| 118 | * @var string $isOldImage Old file status |
| 119 | * @since 1.0.0 |
| 120 | * @access public |
| 121 | */ |
| 122 | public $isOldImage = 0; |
| 123 | |
| 124 | /** |
| 125 | * Check if uploaded file is image |
| 126 | * |
| 127 | * @var string $isNewImage New file status |
| 128 | * @since 1.0.0 |
| 129 | * @access public |
| 130 | */ |
| 131 | public $isNewImage = 0; |
| 132 | |
| 133 | /** |
| 134 | * Attachment ID for Replacement file |
| 135 | * |
| 136 | * @var string $attachment_id Attachment ID |
| 137 | * @since 1.0.0 |
| 138 | * @access public |
| 139 | */ |
| 140 | public $attachment_id; |
| 141 | |
| 142 | /** |
| 143 | * Collection of Replacement Items |
| 144 | * |
| 145 | * @var array $replaceItems Replacement Items |
| 146 | * @since 1.0.0 |
| 147 | * @access public |
| 148 | */ |
| 149 | public $replaceItems = []; |
| 150 | |
| 151 | |
| 152 | /** |
| 153 | * Define the core functionality of the replacement functionality. |
| 154 | * |
| 155 | * Set Button Color |
| 156 | * Check for Functionality is enabled or not |
| 157 | * Show Replacement form, Success message |
| 158 | * |
| 159 | * @since 1.0.0 |
| 160 | */ |
| 161 | function __construct() |
| 162 | { |
| 163 | |
| 164 | $customizeFolders = get_option('customize_folders'); |
| 165 | |
| 166 | $this->buttonColor = isset($customizeFolders['media_replace_button']) ? $customizeFolders['media_replace_button'] : "#FA166B"; |
| 167 | |
| 168 | $this->isEnabled = isset($customizeFolders['folders_enable_replace_media']) ? $customizeFolders['folders_enable_replace_media'] : "yes"; |
| 169 | |
| 170 | $this->isEnabled = ($this->isEnabled == "yes") ? true : false; |
| 171 | |
| 172 | if ($this->isEnabled) { |
| 173 | add_action('admin_menu', [$this, 'admin_menu']); |
| 174 | |
| 175 | add_filter('media_row_actions', [$this, 'add_media_action'], 10, 2); |
| 176 | |
| 177 | add_action( |
| 178 | 'add_meta_boxes', |
| 179 | function () { |
| 180 | add_meta_box('folders-replace-box', esc_html__('Replace Media', 'folders'), [$this, 'replace_meta_box'], 'attachment', 'side', 'low'); |
| 181 | } |
| 182 | ); |
| 183 | add_filter('attachment_fields_to_edit', [$this, 'attachment_editor'], 10, 2); |
| 184 | |
| 185 | add_action('admin_enqueue_scripts', [$this, 'folders_admin_css_and_js']); |
| 186 | |
| 187 | add_action('admin_init', [$this, 'handle_folders_file_upload']); |
| 188 | } |
| 189 | |
| 190 | $customizeFolders = get_option("customize_folders"); |
| 191 | if (isset($customizeFolders['show_folder_in_settings']) && $customizeFolders['show_folder_in_settings'] == "yes") { |
| 192 | $this->upgradeLink = admin_url("options-general.php?page=wcp_folders_settings&setting_page=upgrade-to-pro"); |
| 193 | } else { |
| 194 | $this->upgradeLink = admin_url("admin.php?page=folders-upgrade-to-pro"); |
| 195 | } |
| 196 | |
| 197 | // to replace file name |
| 198 | add_action( |
| 199 | 'add_meta_boxes', |
| 200 | function () { |
| 201 | add_meta_box('folders-replace-file-name', esc_html__('Change file name', 'folders'), [$this, 'change_file_name_box'], 'attachment', 'side', 'core'); |
| 202 | } |
| 203 | ); |
| 204 | |
| 205 | add_filter('attachment_fields_to_edit', [$this, 'attachment_replace_name_with_title'], 10, 2); |
| 206 | |
| 207 | add_action('admin_head', [$this, 'premio_replace_file_CSS']); |
| 208 | |
| 209 | add_action('wp_enqueue_media', [$this, 'replace_media_file_script']); |
| 210 | |
| 211 | add_action('admin_notices', [$this, 'admin_notices']); |
| 212 | |
| 213 | }//end __construct() |
| 214 | |
| 215 | |
| 216 | /** |
| 217 | * Add admin notice |
| 218 | * |
| 219 | * @since 2.6.3 |
| 220 | * @access public |
| 221 | */ |
| 222 | public function admin_notices() |
| 223 | { |
| 224 | if (isset($_REQUEST['premio_message']) && $_REQUEST['premio_message'] == "success") { ?> |
| 225 | <div class="notice notice-success is-dismissible"> |
| 226 | <p><b><?php esc_html_e('File successfully replaced', 'folders'); ?></b></p> |
| 227 | <p><?php esc_html_e('The file has been successfully replaced using the file replacement feature', 'folders'); ?></p> |
| 228 | </div> |
| 229 | |
| 230 | <style> |
| 231 | .folders-undo-notification { |
| 232 | position: fixed; |
| 233 | right: -500px; |
| 234 | bottom: 25px; |
| 235 | width: 280px; |
| 236 | background: #fff; |
| 237 | padding: 15px; |
| 238 | -webkit-box-shadow: 0 3px 6px -4px rgb(0 0 0 / 12%), 0 6px 16px 0 rgb(0 0 0 / 8%), 0 9px 28px 8px rgb(0 0 0 / 5%); |
| 239 | box-shadow: 0 3px 6px -4px rgb(0 0 0 / 12%), 0 6px 16px 0 rgb(0 0 0 / 8%), 0 9px 28px 8px rgb(0 0 0 / 5%); |
| 240 | transition: all .25s linear; |
| 241 | z-index: 250010; |
| 242 | } |
| 243 | .folders-undo-body { |
| 244 | position: relative; |
| 245 | font-size: 13px; |
| 246 | padding: 0 0 5px 0; |
| 247 | } |
| 248 | .close-undo-box { |
| 249 | position: absolute; |
| 250 | right: -10px; |
| 251 | top: 0; |
| 252 | width: 16px; |
| 253 | height: 16px; |
| 254 | transition: all .25s linear; |
| 255 | } |
| 256 | .close-undo-box span { |
| 257 | display: block; |
| 258 | position: relative; |
| 259 | width: 16px; |
| 260 | height: 16px; |
| 261 | transition: all .2s linear; |
| 262 | } |
| 263 | .close-undo-box span:after, .close-undo-box span:before { |
| 264 | content: ""; |
| 265 | position: absolute; |
| 266 | width: 12px; |
| 267 | height: 2px; |
| 268 | background-color: #333; |
| 269 | display: block; |
| 270 | border-radius: 2px; |
| 271 | transform: rotate(45deg); |
| 272 | top: 7px; |
| 273 | left: 2px; |
| 274 | } |
| 275 | .close-undo-box span:after { |
| 276 | transform: rotate(-45deg); |
| 277 | } |
| 278 | .folders-undo-header { |
| 279 | font-weight: 500; |
| 280 | font-size: 14px; |
| 281 | padding: 0 0 3px 0; |
| 282 | color: #014737; |
| 283 | } |
| 284 | .folders-undo-notification.success { |
| 285 | border-left: solid 3px #70C6A3; |
| 286 | } |
| 287 | html[dir="rtl"] .folders-undo-notification { |
| 288 | right: auto; |
| 289 | left: -500px |
| 290 | } |
| 291 | html[dir="rtl"] .folders-undo-notification.active { |
| 292 | left: 25px; |
| 293 | } |
| 294 | html[dir="rtl"] .folders-undo-notification.success { |
| 295 | border-left: none; |
| 296 | border-right: solid 3px #70C6A3; |
| 297 | } |
| 298 | html[dir="rtl"] .close-undo-box { |
| 299 | right: auto; |
| 300 | left: -10px; |
| 301 | } |
| 302 | </style> |
| 303 | <div class="folders-undo-notification success" id="media-success"> |
| 304 | <div class="folders-undo-body"> |
| 305 | <a href="javascript:;" class="close-undo-box"><span></span></a> |
| 306 | <div class="folders-undo-header"><?php esc_html_e('File successfully replaced', 'folders'); ?></div> |
| 307 | <div class="folders-undo-body" style="padding:0"><?php esc_html_e('The file has been successfully replaced using the file replacement feature', 'folders'); ?></div> |
| 308 | </div> |
| 309 | </div> |
| 310 | <script> |
| 311 | jQuery(document).ready(function(){ |
| 312 | jQuery("#media-success").addClass("active"); |
| 313 | setTimeout(function(){ |
| 314 | jQuery("#media-success").removeClass("active"); |
| 315 | }, 5000); |
| 316 | |
| 317 | jQuery(document).on("click", ".close-undo-box", function(){ |
| 318 | jQuery("#media-success").removeClass("active"); |
| 319 | }); |
| 320 | }); |
| 321 | </script> |
| 322 | <?php }//end if |
| 323 | |
| 324 | }//end admin_notices() |
| 325 | |
| 326 | |
| 327 | /** |
| 328 | * Add Metabox to replace file name with title |
| 329 | * |
| 330 | * @since 2.6.3 |
| 331 | * @access public |
| 332 | */ |
| 333 | public function change_file_name_box($post) |
| 334 | { |
| 335 | ?> |
| 336 | <p class="upgrade-bottom"> |
| 337 | <label for="change_file_name"><input disabled type="checkbox" id="change_file_name" name="premio_change_file_name" value="yes"> <?php esc_html_e("Change file name according to title", "folders") ?></label> |
| 338 | </p> |
| 339 | <div class="upgrade-box"> |
| 340 | <a href="<?php echo esc_url($this->upgradeLink) ?>" target="_blank"><?php esc_html_e("Upgrade to Pro", "folders"); ?></a> |
| 341 | </div> |
| 342 | <?php |
| 343 | |
| 344 | }//end change_file_name_box() |
| 345 | |
| 346 | |
| 347 | /** |
| 348 | * Add Js and CSS files for replace file name with title |
| 349 | * |
| 350 | * @since 2.6.3 |
| 351 | * @access public |
| 352 | */ |
| 353 | public function replace_media_file_script() |
| 354 | { |
| 355 | wp_enqueue_script('folders-replace-media', WCP_FOLDER_URL.'assets/js/replace-file-name.js', ['jquery'], WCP_FOLDER_VERSION, true); |
| 356 | wp_localize_script( |
| 357 | 'folders-replace-media', |
| 358 | 'replace_media_options', |
| 359 | [ |
| 360 | 'ajax_url' => admin_url("admin-ajax.php"), |
| 361 | ] |
| 362 | ); |
| 363 | |
| 364 | }//end replace_media_file_script() |
| 365 | |
| 366 | |
| 367 | /** |
| 368 | * Add inline CSS code for file replacement |
| 369 | * |
| 370 | * @since 2.6.3 |
| 371 | * @access public |
| 372 | */ |
| 373 | public function premio_replace_file_CSS() |
| 374 | { |
| 375 | ?> |
| 376 | <style> |
| 377 | .compat-field-replace_file_name th.label {display: none;} |
| 378 | .compat-field-replace_file_name td.field {width: 100%; border-top: solid 1px #c0c0c0; padding:10px 0 0 0;margin: 0;float: none;} |
| 379 | .compat-field-replace_file_name td.field label {width: 100%; display: block;padding:0 0 10px 0;} |
| 380 | .compat-field-replace_file_name td.field label input[type="checkbox"] {margin: 0 4px 0 2px;} |
| 381 | .compat-field-replace_file_name td.field a.update-name-with-title {display: none;} |
| 382 | .compat-field-replace_file_name td.field a.update-name-with-title.show {display: inline-block;} |
| 383 | |
| 384 | .compat-field-folders th.label {width: 100%; text-align: left; padding: 0 0 10px 0; margin: 0; border-top: solid 1px #c0c0c0;float: none;} |
| 385 | .compat-field-folders th.label .alignleft {float: none; text-align: left; font-weight: bold;} |
| 386 | .compat-field-folders th.label br {display: none;} |
| 387 | .compat-field-folders td.field {width: 100%; padding: 0; margin: 0;float: none;} |
| 388 | .folders-undo-notification{position:fixed;right:-500px;bottom:25px;width:280px;background:#fff;padding:15px;-webkit-box-shadow:0 3px 6px -4px rgb(0 0 0 / 12%),0 6px 16px 0 rgb(0 0 0 / 8%),0 9px 28px 8px rgb(0 0 0 / 5%);box-shadow:0 3px 6px -4px rgb(0 0 0 / 12%),0 6px 16px 0 rgb(0 0 0 / 8%),0 9px 28px 8px rgb(0 0 0 / 5%);transition:all .25s linear;z-index:250010}.folders-undo-notification.active{right:25px}.folders-undo-header{font-weight:500;font-size:14px;padding:0 0 3px 0}.folders-undo-body{font-size:13px;padding:0 0 5px 0}.folders-undo-footer{text-align:right;padding:5px 0 0 0}.folders-undo-footer .undo-button{background:#1da1f4;border:none;color:#fff;padding:3px 10px;font-size:12px;border-radius:2px;cursor:pointer}.folders-undo-body{position:relative}.close-undo-box{position:absolute;right:-10px;top:0;width:16px;height:16px;transition:all .25s linear}.close-undo-box:hover{transform:rotate(180deg)}.close-undo-box span{display:block;position:relative;width:16px;height:16px;transition:all .2s linear}.close-undo-box span:after,.close-undo-box span:before{content:"";position:absolute;width:12px;height:2px;background-color:#333;display:block;border-radius:2px;transform:rotate(45deg);top:7px;left:2px}.close-undo-box span:after{transform:rotate(-45deg)} |
| 389 | .folders-undo-notification.no .folders-undo-header { color: #dd0000; } |
| 390 | .folders-undo-notification.yes .folders-undo-header { color: #014737; } |
| 391 | .update-name-with-title .spinner {display: none; visibility: visible; margin-right: 0;} |
| 392 | .update-name-with-title.in-progress .spinner {display: inline-block;} |
| 393 | |
| 394 | #folders-replace-file-name .inside {position: relative;padding:0;margin:0} |
| 395 | #folders-replace-file-name .inside p {padding: 1em; margin: 0;} |
| 396 | #folders-replace-file-name .upgrade-box {position: absolute; top: 0; left: 0; width: 100%; height: 100%; background: rgba(0,0,0,0.3); z-index: 1;display: none;} |
| 397 | #folders-replace-file-name:hover .upgrade-box { display: block; } |
| 398 | #folders-replace-file-name:hover p {filter: blur(1.2px);} |
| 399 | #folders-replace-file-name:hover .upgrade-box a {display: inline-block; position: absolute; left: 0; right: 0; width: 100px; margin: 0 auto; top: 50%; padding: 5px 10px; text-decoration: none; background: #fa166b; color: #fff; border-radius: 4px; text-align: center; margin-top: -14px;} |
| 400 | </style> <?php |
| 401 | |
| 402 | }//end premio_replace_file_CSS() |
| 403 | |
| 404 | |
| 405 | /** |
| 406 | * Replace file name with title |
| 407 | * |
| 408 | * @since 2.6.3 |
| 409 | * @access public |
| 410 | */ |
| 411 | public function attachment_replace_name_with_title($formFields, $post) |
| 412 | { |
| 413 | $screen = null; |
| 414 | if (function_exists('get_current_screen')) { |
| 415 | $screen = get_current_screen(); |
| 416 | |
| 417 | if (! is_null($screen) && $screen->id == 'attachment') { |
| 418 | // hide on edit attachment screen. |
| 419 | return $formFields; |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | $formFields["replace_file_name"] = [ |
| 424 | "label" => esc_html__("Replace media", "folders"), |
| 425 | "input" => "html", |
| 426 | "html" => "<label for='attachment_title_".esc_attr($post->ID)."' data-post='".esc_attr($post->ID)."' data-nonce='".wp_create_nonce('change_attachment_title_'.$post->ID)."'><input id='attachment_title_".esc_attr($post->ID)."' type='checkbox' class='folder-replace-checkbox' value='".esc_attr($post->ID)."'>".esc_html__("Update file name with title")."</label><a href='".$this->upgradeLink."' target='_blank' style='background: ".esc_attr($this->buttonColor)."; border-color: ".esc_attr($this->buttonColor)."; color:#ffffff' type='button' class='button update-name-with-title' >".esc_html__("Upgrade to Pro", "folders")."</a>", |
| 427 | "helps" => "", |
| 428 | ]; |
| 429 | |
| 430 | return $formFields; |
| 431 | |
| 432 | }//end attachment_replace_name_with_title() |
| 433 | |
| 434 | |
| 435 | /** |
| 436 | * Add Js and CSS files for replace file screen |
| 437 | * |
| 438 | * @since 2.6.3 |
| 439 | * @access public |
| 440 | */ |
| 441 | public function folders_admin_css_and_js($page) |
| 442 | { |
| 443 | if ($page == "media_page_folders-replace-media" || $page == "admin_page_folders-replace-media") { |
| 444 | wp_enqueue_style('folders-media', plugin_dir_url(dirname(__FILE__)).'assets/css/replace-media.css', [], WCP_FOLDER_VERSION); |
| 445 | wp_enqueue_script('folders-media', plugin_dir_url(dirname(__FILE__)).'assets/js/replace-media.js', [], WCP_FOLDER_VERSION); |
| 446 | } |
| 447 | |
| 448 | }//end folders_admin_css_and_js() |
| 449 | |
| 450 | |
| 451 | /** |
| 452 | * Add file replace menu in admin |
| 453 | * |
| 454 | * @since 2.6.3 |
| 455 | * @access public |
| 456 | * @return $string |
| 457 | */ |
| 458 | public function admin_menu() |
| 459 | { |
| 460 | add_submenu_page( |
| 461 | null, |
| 462 | esc_html__("Replace media", "folders"), |
| 463 | esc_html__("Replace media", "folders"), |
| 464 | 'upload_files', |
| 465 | 'folders-replace-media', |
| 466 | [ |
| 467 | $this, |
| 468 | 'folders_replace_media', |
| 469 | ] |
| 470 | ); |
| 471 | |
| 472 | }//end admin_menu() |
| 473 | |
| 474 | |
| 475 | /** |
| 476 | * Add file replacement screen |
| 477 | * |
| 478 | * @since 2.6.3 |
| 479 | * @access public |
| 480 | * @return $string |
| 481 | */ |
| 482 | public function folders_replace_media() |
| 483 | { |
| 484 | global $plugin_page; |
| 485 | $action = isset($_GET['action']) ? sanitize_text_field($_GET['action']) : ''; |
| 486 | $attachment_id = isset($_GET['attachment_id']) ? sanitize_text_field($_GET['attachment_id']) : ''; |
| 487 | $nonce = isset($_GET['nonce']) ? sanitize_text_field($_GET['nonce']) : ''; |
| 488 | if (!wp_verify_nonce($nonce, "folders-replace-media-".$attachment_id)) { |
| 489 | echo 'Invalid Nonce'; |
| 490 | exit; |
| 491 | } |
| 492 | |
| 493 | $attachment = get_post($attachment_id); |
| 494 | if (empty($attachment) || !isset($attachment->guid)) { |
| 495 | echo 'Invalid URL'; |
| 496 | exit; |
| 497 | } |
| 498 | |
| 499 | $guid = $attachment->guid; |
| 500 | $guid = explode(".", $guid); |
| 501 | if ($guid == $attachment->guid) { |
| 502 | echo 'Invalid URL'; |
| 503 | exit; |
| 504 | } |
| 505 | |
| 506 | $form_action = $this->getMediaReplaceURL($attachment_id); |
| 507 | include_once dirname(dirname(__FILE__)).WCP_DS."/templates".WCP_DS."admin".WCP_DS."media-replace.php"; |
| 508 | |
| 509 | }//end folders_replace_media() |
| 510 | |
| 511 | |
| 512 | /** |
| 513 | * Add action for file replacement |
| 514 | * |
| 515 | * @since 2.6.3 |
| 516 | * @access public |
| 517 | * @return $actions |
| 518 | */ |
| 519 | public function add_media_action($actions, $post) |
| 520 | { |
| 521 | if (!$this->isEnabled) { |
| 522 | return array_merge($actions); |
| 523 | } |
| 524 | |
| 525 | if (wp_attachment_is('image', $post->ID)) { |
| 526 | $link = $this->getMediaReplaceURL($post->ID); |
| 527 | |
| 528 | $newaction['replace_media'] = '<a style="color: '.esc_attr($this->buttonColor).'" href="'.esc_url($link).'" rel="permalink">'.esc_html__("Replace media", "folders").'</a>'; |
| 529 | |
| 530 | return array_merge($actions, $newaction); |
| 531 | } else { |
| 532 | $link = $this->getMediaReplaceURL($post->ID); |
| 533 | |
| 534 | $newaction['replace_media'] = '<a style="color: '.esc_attr($this->buttonColor).'" target="_blank" href="'.esc_url($this->upgradeLink).'" rel="permalink">'.esc_html__("Replace Media 🔑", "folders").'</a>'; |
| 535 | |
| 536 | return array_merge($actions, $newaction); |
| 537 | } |
| 538 | |
| 539 | return $actions; |
| 540 | |
| 541 | }//end add_media_action() |
| 542 | |
| 543 | |
| 544 | /** |
| 545 | * Get URL for file Replacement |
| 546 | * |
| 547 | * @since 2.6.3 |
| 548 | * @access public |
| 549 | * @return $url |
| 550 | */ |
| 551 | public function getMediaReplaceURL($attach_id) |
| 552 | { |
| 553 | $url = admin_url("upload.php"); |
| 554 | $url = add_query_arg( |
| 555 | [ |
| 556 | 'page' => 'folders-replace-media', |
| 557 | 'action' => 'folders_replace_media', |
| 558 | 'attachment_id' => $attach_id, |
| 559 | 'nonce' => wp_create_nonce("folders-replace-media-".$attach_id), |
| 560 | ], |
| 561 | $url |
| 562 | ); |
| 563 | |
| 564 | return $url; |
| 565 | |
| 566 | }//end getMediaReplaceURL() |
| 567 | |
| 568 | |
| 569 | /** |
| 570 | * Get file size |
| 571 | * |
| 572 | * @since 2.6.3 |
| 573 | * @access public |
| 574 | * @return $size |
| 575 | */ |
| 576 | public function replace_meta_box($post) |
| 577 | { |
| 578 | if (wp_attachment_is('image', $post->ID)) { |
| 579 | $link = $this->getMediaReplaceURL($post->ID); ?> |
| 580 | <p><a style='background: <?php echo esc_attr($this->buttonColor) ?>; border-color: <?php echo esc_attr($this->buttonColor) ?>; color:#ffffff' href='<?php echo esc_url($link) ?>' class='button-secondary'><?php esc_html_e("Upload a new file", "folders") ?></a></p><p><?php esc_html_e("Click on the button to replace the file with another file", "folders") ?></p> |
| 581 | <?php } else { ?> |
| 582 | <p><a style='color: <?php echo esc_attr($this->buttonColor) ?>; font-weight: 500' target='_blank' href='<?php echo esc_url($this->upgradeLink) ?>' ><?php esc_html_e("Upgrade to Pro", "folders") ?></a><?php esc_html_e("to replace any kind of files while uploading including pdf/svg/docx/etc & more.", "folders") ?></p> |
| 583 | <?php } |
| 584 | |
| 585 | }//end replace_meta_box() |
| 586 | |
| 587 | |
| 588 | /** |
| 589 | * Get file size |
| 590 | * |
| 591 | * @since 2.6.3 |
| 592 | * @access public |
| 593 | * @return $size |
| 594 | */ |
| 595 | public function attachment_editor($formFields, $post) |
| 596 | { |
| 597 | $screen = null; |
| 598 | if (function_exists('get_current_screen')) { |
| 599 | $screen = get_current_screen(); |
| 600 | |
| 601 | if (! is_null($screen) && $screen->id == 'attachment') { |
| 602 | // hide on edit attachment screen. |
| 603 | return $formFields; |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | if (wp_attachment_is('image', $post->ID)) { |
| 608 | $link = $this->getMediaReplaceURL($post->ID); |
| 609 | $formFields["folders"] = [ |
| 610 | "label" => esc_html__("Replace media", "folders"), |
| 611 | "input" => "html", |
| 612 | "html" => "<a style='background: ".esc_attr($this->buttonColor)."; border-color: ".esc_attr($this->buttonColor)."; color:#ffffff' href='".esc_url($link)."' class='button-secondary'>".esc_html__("Upload a new file", "folders")."</a>", |
| 613 | "helps" => esc_html__("Click on the button to replace the file with another file", "folders"), |
| 614 | ]; |
| 615 | } else { |
| 616 | $formFields["folders"] = [ |
| 617 | "label" => esc_html__("Replace media", "folders"), |
| 618 | "input" => "html", |
| 619 | "html" => "<div style='border: solid 1px #c0c0c0; padding: 10px; border-radius: 2px; background: #ececec;'><a style='color: ".esc_attr($this->buttonColor)."; font-weight: 500' target='_blank' href='".esc_url($this->upgradeLink)."' >".esc_html__("Upgrade to Pro", "folders")."</a> ".esc_html__("to replace media files other than images", "folders")."</div>", |
| 620 | "helps" => esc_html__("Click on the button to replace the file with another file", "folders"), |
| 621 | ]; |
| 622 | } |
| 623 | |
| 624 | return $formFields; |
| 625 | |
| 626 | }//end attachment_editor() |
| 627 | |
| 628 | |
| 629 | /** |
| 630 | * Get file size |
| 631 | * |
| 632 | * @since 2.6.3 |
| 633 | * @access public |
| 634 | * @return $size |
| 635 | */ |
| 636 | public function getFileSize($attachment_id) |
| 637 | { |
| 638 | $size = filesize(get_attached_file($attachment_id)); |
| 639 | if ($size > 1000000) { |
| 640 | $size = ($size / 1000000); |
| 641 | return number_format((float) $size, 2, ".", ",")." MB"; |
| 642 | } else if ($size > 1000) { |
| 643 | $size = ($size / 1000); |
| 644 | return number_format((float) $size, 2, ".", ",")." KB"; |
| 645 | } |
| 646 | |
| 647 | return $size." B"; |
| 648 | |
| 649 | }//end getFileSize() |
| 650 | |
| 651 | |
| 652 | /** |
| 653 | * Upload file and Replace it |
| 654 | * |
| 655 | * @since 2.6.3 |
| 656 | * @access public |
| 657 | */ |
| 658 | public function handle_folders_file_upload() |
| 659 | { |
| 660 | global $wpdb; |
| 661 | if (isset($_FILES['new_media_file'])) { |
| 662 | if ($_FILES['new_media_file']['error'] == 0) { |
| 663 | $attachment_id = sanitize_text_field(filter_input(INPUT_GET, "attachment_id")); |
| 664 | $nonce = sanitize_text_field(filter_input(INPUT_GET, "nonce")); |
| 665 | if (!wp_verify_nonce($nonce, "folders-replace-media-".$attachment_id)) { |
| 666 | return; |
| 667 | } |
| 668 | |
| 669 | $attachment = get_post($attachment_id); |
| 670 | if (empty($attachment) || !isset($attachment->guid)) { |
| 671 | return; |
| 672 | } |
| 673 | |
| 674 | $attachment_url = $attachment->guid; |
| 675 | $url = wp_get_attachment_url($attachment_id); |
| 676 | if (!empty($url)) { |
| 677 | $attachment_url = $url; |
| 678 | } |
| 679 | |
| 680 | $guid = explode(".", $attachment_url); |
| 681 | $guid = array_pop($guid); |
| 682 | if ($guid == $attachment->guid) { |
| 683 | return; |
| 684 | } |
| 685 | |
| 686 | $replacement_option = isset($_REQUEST['replacement_option']) ? sanitize_text_field($_REQUEST['replacement_option']) : "replace_only_file"; |
| 687 | |
| 688 | $this->attachment_id = $attachment_id; |
| 689 | |
| 690 | $file = $_FILES['new_media_file']; |
| 691 | $file_name = $file['name']; |
| 692 | $file_ext = explode(".", $file_name); |
| 693 | $file_ext = array_pop($file_ext); |
| 694 | |
| 695 | if (wp_attachment_is('image', $attachment_id)) { |
| 696 | $this->isOldImage = 1; |
| 697 | } |
| 698 | |
| 699 | $this->oldFileURL = $attachment_url; |
| 700 | $this->oldImageMeta = wp_get_attachment_metadata($attachment_id); |
| 701 | |
| 702 | $new_file = $file['tmp_name']; |
| 703 | |
| 704 | $file_parts = pathinfo($attachment_url); |
| 705 | |
| 706 | $db_file_name = $file_parts['basename']; |
| 707 | $db_file_array = explode(".", $db_file_name); |
| 708 | array_pop($db_file_array); |
| 709 | $db_file_name = implode(".", $db_file_array)."."; |
| 710 | $db_file_name .= $file_ext; |
| 711 | |
| 712 | $wp_upload_path = wp_get_upload_dir(); |
| 713 | |
| 714 | $base_path = $old_path = $wp_upload_path['basedir'].DIRECTORY_SEPARATOR; |
| 715 | $baseurl = $old_url = $wp_upload_path['baseurl']."/"; |
| 716 | |
| 717 | $post_upload = ""; |
| 718 | |
| 719 | $wp_attached_file = get_post_meta($attachment_id, "_wp_attached_file", true); |
| 720 | if ($wp_attached_file !== false) { |
| 721 | $old_file_name = explode("/", $wp_attached_file); |
| 722 | array_pop($old_file_name); |
| 723 | |
| 724 | if (count($old_file_name) > 0) { |
| 725 | $old_path .= implode(DIRECTORY_SEPARATOR, $old_file_name); |
| 726 | $old_url .= implode("/", $old_file_name); |
| 727 | } |
| 728 | |
| 729 | if ($replacement_option == "replace_file_with_name" && isset($_REQUEST['new_folder_option']) && $_REQUEST['new_folder_option'] && isset($_REQUEST['new_folder_path']) && !empty($_REQUEST['new_folder_path'])) { |
| 730 | $baseurl .= sanitize_text_field($_REQUEST['new_folder_path']); |
| 731 | $base_path .= str_replace("/", DIRECTORY_SEPARATOR, sanitize_text_field($_REQUEST['new_folder_path'])); |
| 732 | $post_upload = sanitize_text_field($_REQUEST['new_folder_path']); |
| 733 | } else if (count($old_file_name) > 0) { |
| 734 | $baseurl .= implode(DIRECTORY_SEPARATOR, $old_file_name); |
| 735 | $base_path .= implode("/", $old_file_name); |
| 736 | $post_upload = implode("/", $old_file_name); |
| 737 | } |
| 738 | } |
| 739 | |
| 740 | $uploadDir = []; |
| 741 | $uploadDir['path'] = $base_path; |
| 742 | $uploadDir['old_path'] = $old_path; |
| 743 | $uploadDir['url'] = $baseurl; |
| 744 | $uploadDir['old_url'] = $old_url; |
| 745 | |
| 746 | $this->uploadDir = $uploadDir; |
| 747 | |
| 748 | $this->oldFilePath = $old_path."/".$file_parts['basename']; |
| 749 | |
| 750 | if (!is_dir($base_path)) { |
| 751 | mkdir($base_path, 755, true); |
| 752 | } |
| 753 | |
| 754 | if (is_dir($base_path)) { |
| 755 | $file_array = explode(".", $file['name']); |
| 756 | $file_ext = array_pop($file_array); |
| 757 | $new_file_name = sanitize_title(implode(".", $file_array)).".".$file_ext; |
| 758 | if ($replacement_option == "replace_only_file") { |
| 759 | $new_file_name = $db_file_name; |
| 760 | } |
| 761 | |
| 762 | if (strtolower($new_file_name) != strtolower($file_parts['basename'])) { |
| 763 | $new_file_name = $this->checkForFileName($new_file_name, $base_path.DIRECTORY_SEPARATOR); |
| 764 | } |
| 765 | |
| 766 | $this->newFilePath = $base_path.DIRECTORY_SEPARATOR.$new_file_name; |
| 767 | |
| 768 | $status = move_uploaded_file($new_file, $this->newFilePath); |
| 769 | |
| 770 | $this->newFileURL = trim($baseurl, "/")."/".$new_file_name; |
| 771 | |
| 772 | if ($status) { |
| 773 | $oldFilePath = str_replace(["/", DIRECTORY_SEPARATOR], ["", ""], $this->oldFilePath); |
| 774 | $newFilePath = str_replace(["/", DIRECTORY_SEPARATOR], ["", ""], $this->newFilePath); |
| 775 | if ($oldFilePath != $newFilePath) { |
| 776 | if (file_exists($this->oldFilePath)) { |
| 777 | @unlink($this->oldFilePath); |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | update_attached_file($attachment->ID, $this->newFileURL); |
| 782 | |
| 783 | $update_array = []; |
| 784 | $update_array['ID'] = $attachment->ID; |
| 785 | $update_array['guid'] = $this->newFileURL; |
| 786 | // wp_get_attachment_url($this->post_id); |
| 787 | $update_array['post_mime_type'] = $file['type']; |
| 788 | |
| 789 | $current_date = date("Y-m-d H:i:s"); |
| 790 | $current_date_gmt = date_i18n("Y-m-d H:i:s", strtotime($current_date)); |
| 791 | if (isset($_REQUEST['date_options']) && !empty($_REQUEST['date_options'])) { |
| 792 | if ($_REQUEST['date_options'] == "replace_date") { |
| 793 | $update_array['post_date'] = $current_date; |
| 794 | $update_array['post_date_gmt'] = $current_date_gmt; |
| 795 | } else if ($_REQUEST['date_options'] == "custom_date") { |
| 796 | $custom_date = filter_input(INPUT_POST, "custom_date"); |
| 797 | $custom_date_hour = filter_input(INPUT_POST, "custom_date_hour"); |
| 798 | $custom_hour = str_pad($custom_date_hour, 2, 0, STR_PAD_LEFT); |
| 799 | $custom_date_min = filter_input(INPUT_POST, "custom_date_min"); |
| 800 | $custom_minute = str_pad($custom_date_min, 2, 0, STR_PAD_LEFT); |
| 801 | $custom_date = date("Y-m-d H:i:s", strtotime($custom_date." ".$custom_hour.":".$custom_minute)); |
| 802 | if ($custom_date !== false) { |
| 803 | $datetime = date("Y-m-d H:i:s", strtotime($custom_date)); |
| 804 | $datetime_gmt = date_i18n("Y-m-d H:i:s", strtotime($datetime)); |
| 805 | $update_array['post_date'] = $datetime; |
| 806 | $update_array['post_date_gmt'] = $datetime_gmt; |
| 807 | } |
| 808 | } |
| 809 | } |
| 810 | |
| 811 | $update_array['post_modified'] = $current_date; |
| 812 | $update_array['post_modified_gmt'] = $current_date_gmt; |
| 813 | $post_id = \wp_update_post($update_array, true); |
| 814 | |
| 815 | update_post_meta($attachment_id, '_wp_attached_file', trim(trim($post_upload, "/")."/".$new_file_name, "/")); |
| 816 | |
| 817 | // update post doesn't update GUID on updates. |
| 818 | $wpdb->update($wpdb->posts, ['guid' => $this->newFileURL], ['ID' => $attachment->ID]); |
| 819 | |
| 820 | $this->removeThumbImages(); |
| 821 | |
| 822 | $metadata = wp_generate_attachment_metadata($attachment->ID, $this->newFilePath); |
| 823 | wp_update_attachment_metadata($attachment->ID, $metadata); |
| 824 | |
| 825 | $this->newImageMeta = wp_get_attachment_metadata($attachment_id); |
| 826 | |
| 827 | // update_post_meta( $attachment_id, '_wp_attached_file', trim(trim($post_upload, "/")."/".$new_file_name ), "/"); |
| 828 | $this->searchAndReplace(); |
| 829 | wp_redirect(admin_url("post.php?post=".esc_attr($attachment_id)."&action=edit&premio_message=success&image_update=1")); |
| 830 | exit; |
| 831 | } else { |
| 832 | wp_die("Error during uploading file"); |
| 833 | }//end if |
| 834 | } else { |
| 835 | wp_die("Permission issue, Unable to create directory"); |
| 836 | }//end if |
| 837 | }//end if |
| 838 | }//end if |
| 839 | |
| 840 | }//end handle_folders_file_upload() |
| 841 | |
| 842 | |
| 843 | /** |
| 844 | * Check for filename |
| 845 | * |
| 846 | * @since 2.6.3 |
| 847 | * @access public |
| 848 | */ |
| 849 | public function checkForFileName($fileName, $filePath, $postFix=0) |
| 850 | { |
| 851 | $new_file_name = $fileName; |
| 852 | if (!empty($postFix)) { |
| 853 | $file_array = explode(".", $fileName); |
| 854 | $file_ext = array_pop($file_array); |
| 855 | $new_file_name = implode(".", $file_array)."-".$postFix.".".$file_ext; |
| 856 | } |
| 857 | |
| 858 | if (!file_exists($filePath.$new_file_name)) { |
| 859 | return $new_file_name; |
| 860 | } |
| 861 | |
| 862 | return $this->checkForFileName($fileName, $filePath, ($postFix + 1)); |
| 863 | |
| 864 | }//end checkForFileName() |
| 865 | |
| 866 | |
| 867 | /** |
| 868 | * Check and Remove Thumb image in wp-content |
| 869 | * |
| 870 | * @since 2.6.3 |
| 871 | * @access public |
| 872 | */ |
| 873 | public function removeThumbImages() |
| 874 | { |
| 875 | if (!empty($this->oldImageMeta) && isset($this->oldImageMeta['sizes']) && !empty($this->uploadDir) && isset($this->uploadDir['path'])) { |
| 876 | $path = $this->uploadDir['path'].DIRECTORY_SEPARATOR; |
| 877 | foreach ($this->oldImageMeta['sizes'] as $image) { |
| 878 | if (file_exists($path.$image['file'])) { |
| 879 | @unlink($path.$image['file']); |
| 880 | } |
| 881 | } |
| 882 | } |
| 883 | |
| 884 | }//end removeThumbImages() |
| 885 | |
| 886 | |
| 887 | /** |
| 888 | * Search and Replace files in Database |
| 889 | * |
| 890 | * @since 2.6.3 |
| 891 | * @access public |
| 892 | * @return $string |
| 893 | */ |
| 894 | public function searchAndReplace() |
| 895 | { |
| 896 | if (wp_attachment_is('image', $this->attachment_id)) { |
| 897 | $this->isNewImage = 1; |
| 898 | } |
| 899 | |
| 900 | if ($this->oldFileURL != $this->newFileURL) { |
| 901 | $replace = [ |
| 902 | 'search' => $this->oldFileURL, |
| 903 | 'replace' => $this->newFileURL, |
| 904 | ]; |
| 905 | $this->replaceItems[] = $replace; |
| 906 | } |
| 907 | |
| 908 | $base_url = $this->uploadDir['url']; |
| 909 | $base_url = trim($base_url, "/")."/"; |
| 910 | $new_url = $this->newFileURL; |
| 911 | |
| 912 | if (isset($this->oldImageMeta['sizes']) && !empty($this->oldImageMeta['sizes'])) { |
| 913 | if (!isset($this->newImageMeta['sizes']) || empty($this->newImageMeta['sizes'])) { |
| 914 | foreach ($this->oldImageMeta['sizes'] as $key => $image) { |
| 915 | $replace = [ |
| 916 | 'search' => $base_url.$image['file'], |
| 917 | 'replace' => $new_url, |
| 918 | ]; |
| 919 | $this->replaceItems[] = $replace; |
| 920 | } |
| 921 | } else if (isset($this->newImageMeta['sizes']) && !empty($this->newImageMeta['sizes'])) { |
| 922 | $new_size = $this->newImageMeta['sizes']; |
| 923 | foreach ($this->oldImageMeta['sizes'] as $key => $image) { |
| 924 | $new_replace_url = $new_url; |
| 925 | if (isset($new_size[$key])) { |
| 926 | $new_replace_url = $base_url.$new_size[$key]['file']; |
| 927 | } |
| 928 | |
| 929 | $replace = [ |
| 930 | 'search' => $base_url.$image['file'], |
| 931 | 'replace' => $new_replace_url, |
| 932 | ]; |
| 933 | $this->replaceItems[] = $replace; |
| 934 | } |
| 935 | }//end if |
| 936 | }//end if |
| 937 | |
| 938 | if (!empty($this->replaceItems)) { |
| 939 | $replaceItems = []; |
| 940 | foreach ($this->replaceItems as $args) { |
| 941 | if ($args['search'] != $args['replace']) { |
| 942 | $replaceItems[] = $args; |
| 943 | } |
| 944 | } |
| 945 | |
| 946 | $this->replaceItems = $replaceItems; |
| 947 | $this->replaceURL(); |
| 948 | } |
| 949 | |
| 950 | }//end searchAndReplace() |
| 951 | |
| 952 | |
| 953 | /** |
| 954 | * Replace URL in Database tables |
| 955 | * |
| 956 | * @since 2.6.3 |
| 957 | * @access public |
| 958 | * @return $string |
| 959 | */ |
| 960 | function replaceURL() |
| 961 | { |
| 962 | // check in post content |
| 963 | $this->checkInPostContent(); |
| 964 | |
| 965 | // check in options |
| 966 | $this->checkInOptions(); |
| 967 | |
| 968 | // check in meta |
| 969 | $this->checkInMetaData(); |
| 970 | |
| 971 | if (function_exists('folders_clear_all_caches')) { |
| 972 | folders_clear_all_caches(); |
| 973 | } |
| 974 | |
| 975 | }//end replaceURL() |
| 976 | |
| 977 | |
| 978 | /** |
| 979 | * Checking image URLs in Post Content |
| 980 | * |
| 981 | * @since 2.6.3 |
| 982 | * @access public |
| 983 | * @return $string |
| 984 | */ |
| 985 | function checkInPostContent() |
| 986 | { |
| 987 | global $wpdb; |
| 988 | $post_table = $wpdb->prefix."posts"; |
| 989 | if (!empty($this->replaceItems)) { |
| 990 | $query = "SELECT ID, post_content FROM ".$post_table." WHERE post_content LIKE %s"; |
| 991 | $update_query = "UPDATE ".$post_table." SET post_content = %s WHERE ID = %d"; |
| 992 | foreach ($this->replaceItems as $args) { |
| 993 | if ($args['search'] != $args['replace']) { |
| 994 | $sql_query = $wpdb->prepare($query, "%".$args['search']."%"); |
| 995 | $results = $wpdb->get_results($sql_query, ARRAY_A); |
| 996 | if (!empty($results)) { |
| 997 | foreach ($results as $row) { |
| 998 | $content = $this->findAndReplaceContent($row['post_content'], $args['search'], $args['replace']); |
| 999 | $update_post_query = $wpdb->prepare($update_query, $content, $row['ID']); |
| 1000 | $result = $wpdb->query($update_post_query); |
| 1001 | } |
| 1002 | } |
| 1003 | } |
| 1004 | } |
| 1005 | } |
| 1006 | |
| 1007 | }//end checkInPostContent() |
| 1008 | |
| 1009 | |
| 1010 | /** |
| 1011 | * Checking image URLs in MetaData |
| 1012 | * |
| 1013 | * @since 2.6.3 |
| 1014 | * @access public |
| 1015 | * @return $string |
| 1016 | */ |
| 1017 | function checkInOptions() |
| 1018 | { |
| 1019 | global $wpdb; |
| 1020 | $post_table = $wpdb->prefix."options"; |
| 1021 | if (!empty($this->replaceItems)) { |
| 1022 | $query = "SELECT option_id, option_value FROM ".$post_table." WHERE option_value LIKE %s"; |
| 1023 | $update_query = "UPDATE ".$post_table." SET option_value = %s WHERE option_id = %d"; |
| 1024 | foreach ($this->replaceItems as $args) { |
| 1025 | if ($args['search'] != $args['replace']) { |
| 1026 | $sql_query = $wpdb->prepare($query, "%".$args['search']."%"); |
| 1027 | $results = $wpdb->get_results($sql_query, ARRAY_A); |
| 1028 | if (!empty($results)) { |
| 1029 | foreach ($results as $row) { |
| 1030 | $content = $this->findAndReplaceContent($row['post_content'], $args['search'], $args['replace']); |
| 1031 | $update_post_query = $wpdb->prepare($update_query, $content, $row['ID']); |
| 1032 | $result = $wpdb->query($update_post_query); |
| 1033 | } |
| 1034 | } |
| 1035 | } |
| 1036 | } |
| 1037 | } |
| 1038 | |
| 1039 | }//end checkInOptions() |
| 1040 | |
| 1041 | |
| 1042 | /** |
| 1043 | * Checking image URLs in MetaData |
| 1044 | * |
| 1045 | * @since 2.6.3 |
| 1046 | * @access public |
| 1047 | * @return $string |
| 1048 | */ |
| 1049 | function checkInMetaData() |
| 1050 | { |
| 1051 | $tables = [ |
| 1052 | [ |
| 1053 | 'table_name' => 'usermeta', |
| 1054 | 'primary_key' => 'umeta_id', |
| 1055 | 'search_key' => 'meta_value', |
| 1056 | ], |
| 1057 | [ |
| 1058 | 'table_name' => 'termmeta', |
| 1059 | 'primary_key' => 'meta_id', |
| 1060 | 'search_key' => 'meta_value', |
| 1061 | ], |
| 1062 | [ |
| 1063 | 'table_name' => 'postmeta', |
| 1064 | 'primary_key' => 'meta_id', |
| 1065 | 'search_key' => 'meta_value', |
| 1066 | ], |
| 1067 | [ |
| 1068 | 'table_name' => 'commentmeta', |
| 1069 | 'primary_key' => 'meta_id', |
| 1070 | 'search_key' => 'meta_value', |
| 1071 | ], |
| 1072 | ]; |
| 1073 | global $wpdb; |
| 1074 | foreach ($tables as $table) { |
| 1075 | $post_table = $wpdb->prefix.$table['table_name']; |
| 1076 | if (!empty($this->replaceItems)) { |
| 1077 | $query = "SELECT ".esc_attr($table['primary_key']).", ".esc_attr($table['search_key'])." FROM ".esc_attr($post_table)." WHERE ".esc_attr($table['search_key'])." LIKE %s"; |
| 1078 | $update_query = "UPDATE ".$post_table." SET ".esc_attr($table['search_key'])." = %s WHERE ".esc_attr($table['primary_key'])." = %d"; |
| 1079 | foreach ($this->replaceItems as $args) { |
| 1080 | if ($args['search'] != $args['replace']) { |
| 1081 | $sql_query = $wpdb->prepare($query, "%".$args['search']."%"); |
| 1082 | $results = $wpdb->get_results($sql_query, ARRAY_A); |
| 1083 | if (!empty($results)) { |
| 1084 | foreach ($results as $row) { |
| 1085 | $content = $this->findAndReplaceContent($row[$table['search_key']], $args['search'], $args['replace']); |
| 1086 | $update_post_query = $wpdb->prepare($update_query, $content, $row[$table['primary_key']]); |
| 1087 | $result = $wpdb->query($update_post_query); |
| 1088 | } |
| 1089 | } |
| 1090 | } |
| 1091 | } |
| 1092 | } |
| 1093 | } |
| 1094 | |
| 1095 | }//end checkInMetaData() |
| 1096 | |
| 1097 | |
| 1098 | /** |
| 1099 | * Checking for Array Key |
| 1100 | * |
| 1101 | * @since 2.6.3 |
| 1102 | * @access public |
| 1103 | * @return $json |
| 1104 | * Forked from Enable Media Replace |
| 1105 | */ |
| 1106 | function findAndReplaceContent($content, $search, $replace, $depth=false) |
| 1107 | { |
| 1108 | $content = maybe_unserialize($content); |
| 1109 | |
| 1110 | // Checking for JSON Data |
| 1111 | $isJson = $this->isJSON($content); |
| 1112 | if ($isJson) { |
| 1113 | $content = json_decode($content); |
| 1114 | } |
| 1115 | |
| 1116 | // Replace content if content is String |
| 1117 | if (is_string($content)) { |
| 1118 | $content = str_replace($search, $replace, $content); |
| 1119 | } else if (is_wp_error($content)) { |
| 1120 | // Return if error in data |
| 1121 | } else if (is_array($content)) { |
| 1122 | // Replace content if content is Array |
| 1123 | foreach ($content as $index => $value) { |
| 1124 | $content[$index] = $this->findAndReplaceContent($value, $search, $replace, true); |
| 1125 | if (is_string($index)) { |
| 1126 | $index_replaced = $this->findAndReplaceContent($index, $search, $replace, true); |
| 1127 | if ($index_replaced !== $index) { |
| 1128 | $content = $this->changeArrayKey($content, [$index => $index_replaced]); |
| 1129 | } |
| 1130 | } |
| 1131 | } |
| 1132 | } else if (is_object($content)) { |
| 1133 | // Replace content if content is Object |
| 1134 | foreach ($content as $key => $value) { |
| 1135 | $content->{$key} = $this->findAndReplaceContent($value, $search, $replace, true); |
| 1136 | } |
| 1137 | }//end if |
| 1138 | |
| 1139 | if ($isJson && $depth === false) { |
| 1140 | $content = json_encode($content, JSON_UNESCAPED_SLASHES); |
| 1141 | } else if ($depth === false && (is_array($content) || is_object($content))) { |
| 1142 | $content = maybe_serialize($content); |
| 1143 | } |
| 1144 | |
| 1145 | return $content; |
| 1146 | |
| 1147 | }//end findAndReplaceContent() |
| 1148 | |
| 1149 | |
| 1150 | /** |
| 1151 | * Checking for Array Key |
| 1152 | * |
| 1153 | * @since 2.6.3 |
| 1154 | * @access public |
| 1155 | * @return $json |
| 1156 | */ |
| 1157 | function changeArrayKey($array, $set) |
| 1158 | { |
| 1159 | if (is_array($array) && is_array($set)) { |
| 1160 | $newArray = []; |
| 1161 | foreach ($array as $k => $v) { |
| 1162 | $key = array_key_exists($k, $set) ? $set[$k] : $k; |
| 1163 | $newArray[$key] = is_array($v) ? $this->changeArrayKey($v, $set) : $v; |
| 1164 | } |
| 1165 | |
| 1166 | return $newArray; |
| 1167 | } |
| 1168 | |
| 1169 | return $array; |
| 1170 | |
| 1171 | }//end changeArrayKey() |
| 1172 | |
| 1173 | |
| 1174 | /** |
| 1175 | * Check if it is JSON or not |
| 1176 | * |
| 1177 | * @since 2.6.3 |
| 1178 | * @access public |
| 1179 | * Forked from Enable Media Replace |
| 1180 | * @return $json |
| 1181 | */ |
| 1182 | function isJSON($content) |
| 1183 | { |
| 1184 | if (is_array($content) || is_object($content)) { |
| 1185 | return false; |
| 1186 | } |
| 1187 | |
| 1188 | $json = json_decode($content); |
| 1189 | return $json && $json != $content; |
| 1190 | |
| 1191 | }//end isJSON() |
| 1192 | |
| 1193 | |
| 1194 | }//end class |
| 1195 | |
| 1196 | $folders_replace_media = new folders_replace_media(); |
| 1197 |