| 1 | <?php |
|---|
| 2 | namespace EnableMediaReplace; |
|---|
| 3 | |
|---|
| 4 | if (! defined('ABSPATH')) { |
|---|
| 5 | exit; // Exit if accessed directly. |
|---|
| 6 | } |
|---|
| 7 | |
|---|
| 8 | use EnableMediaReplace as emr; |
|---|
| 9 | use EnableMediaReplace\ShortPixelLogger\ShortPixelLogger as Log; |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | abstract class ViewController |
|---|
| 13 | { |
|---|
| 14 | |
|---|
| 15 | abstract function load(); |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | const ERROR_UPLOAD_PERMISSION = 1; |
|---|
| 19 | const ERROR_IMAGE_PERMISSION = 2; |
|---|
| 20 | const ERROR_FORM = 3; |
|---|
| 21 | const ERROR_TIME = 4; |
|---|
| 22 | const ERROR_UPDATE_FAILED = 5; |
|---|
| 23 | const ERROR_SECURITY = 6; |
|---|
| 24 | const ERROR_UPLOAD_FAILED = 7; |
|---|
| 25 | const ERROR_NONCE = 8; |
|---|
| 26 | const ERROR_KEY = 9; // Missing key when replacing backgrounds. |
|---|
| 27 | |
|---|
| 28 | // These synced with ReplaceController |
|---|
| 29 | const ERROR_TARGET_EXISTS = 20; |
|---|
| 30 | const ERROR_DESTINATION_FAIL = 21; |
|---|
| 31 | const ERROR_COPY_FAILED = 22; |
|---|
| 32 | const ERROR_UPDATE_POST = 23; |
|---|
| 33 | const ERROR_DIRECTORY_SECURITY = 24; |
|---|
| 34 | const ERROR_DIRECTORY_NOTEXIST = 25; |
|---|
| 35 | |
|---|
| 36 | // Remove Background |
|---|
| 37 | const ERROR_DOWNLOAD_FAILED = 31; |
|---|
| 38 | |
|---|
| 39 | protected static $viewsLoaded = array(); |
|---|
| 40 | |
|---|
| 41 | protected $view; // object to use in the view. |
|---|
| 42 | protected $url; // if controller is home to a page, sets the URL here. For redirects and what not. |
|---|
| 43 | |
|---|
| 44 | |
|---|
| 45 | public function __construct() |
|---|
| 46 | { |
|---|
| 47 | $this->view = new \stdClass; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | protected function loadView($template = null, $unique = true) |
|---|
| 51 | { |
|---|
| 52 | if (is_null($template) ) |
|---|
| 53 | { |
|---|
| 54 | return false; |
|---|
| 55 | } |
|---|
| 56 | elseif (strlen(trim($template)) == 0) |
|---|
| 57 | { |
|---|
| 58 | return false; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | $view = $this->view; |
|---|
| 62 | $controller = $this; |
|---|
| 63 | $template_path = emr()->plugin_path('views/' . $template . '.php'); |
|---|
| 64 | |
|---|
| 65 | if (file_exists($template_path) === false) |
|---|
| 66 | { |
|---|
| 67 | Log::addError("View $template could not be found in " . $template_path, |
|---|
| 68 | array('class' => get_class($this))); |
|---|
| 69 | } |
|---|
| 70 | elseif ($unique === false || ! in_array($template, self::$viewsLoaded)) |
|---|
| 71 | { |
|---|
| 72 | include($template_path); |
|---|
| 73 | self::$viewsLoaded[] = $template; |
|---|
| 74 | } |
|---|
| 75 | } |
|---|
| 76 | |
|---|
| 77 | protected function viewError($errorCode, $errorData = array()) |
|---|
| 78 | { |
|---|
| 79 | $message = $description = false; |
|---|
| 80 | switch($errorCode) |
|---|
| 81 | { |
|---|
| 82 | case self::ERROR_UPLOAD_PERMISSION: |
|---|
| 83 | $message = __('You don\'t have permission to upload images. Please refer to your administrator', 'enable-media-replace'); |
|---|
| 84 | break; |
|---|
| 85 | case self::ERROR_IMAGE_PERMISSION: |
|---|
| 86 | $message = __('You don\'t have permission to edit this image', 'enable-media-replace'); |
|---|
| 87 | break; |
|---|
| 88 | case self::ERROR_FORM: |
|---|
| 89 | $message = __('The form submitted is missing various fields', 'enable-media-replace'); |
|---|
| 90 | break; |
|---|
| 91 | case self::ERROR_TIME: |
|---|
| 92 | $message = __('The custom time format submitted is invalid', 'enable-media-replace'); |
|---|
| 93 | break; |
|---|
| 94 | case self::ERROR_UPDATE_FAILED: |
|---|
| 95 | $message = __('Updating the WordPress attachment failed', 'enable-media-replace'); |
|---|
| 96 | break; |
|---|
| 97 | case self::ERROR_SECURITY: |
|---|
| 98 | $message = __('The file upload has been rejected for security reason. WordPress might not allow uploading this extension or filetype', 'enable-media-replace'); |
|---|
| 99 | break; |
|---|
| 100 | case self::ERROR_UPLOAD_FAILED: |
|---|
| 101 | $message = __('The upload from your browser seem to have failed', 'enable-media-replace'); |
|---|
| 102 | break; |
|---|
| 103 | case self::ERROR_TARGET_EXISTS: |
|---|
| 104 | $message = __('The target file already exists in this directory. Please try another name / directory', 'enable-media-replace'); |
|---|
| 105 | $description = __('This error is shown because you try to move the image to another folder, which already has this file', 'enable-media-replace'); |
|---|
| 106 | break; |
|---|
| 107 | case self::ERROR_DESTINATION_FAIL: |
|---|
| 108 | $message = __('Something went wrong while writing the file or directory', 'enable-media-replace'); |
|---|
| 109 | break; |
|---|
| 110 | case self::ERROR_COPY_FAILED: |
|---|
| 111 | $message = __('Copying replacement file to destination failed', 'enable-media-replace'); |
|---|
| 112 | break; |
|---|
| 113 | case self::ERROR_UPDATE_POST: |
|---|
| 114 | $message = __('Error updating WordPress post in the database', 'enable-media-replace'); |
|---|
| 115 | break; |
|---|
| 116 | case self::ERROR_DIRECTORY_SECURITY: |
|---|
| 117 | $message = __('Specified directory is outside the upload directory. This is not allowed for security reasons', 'enable-media-replace'); |
|---|
| 118 | $path = isset($errorData['path']) ? $errorData['path'] : false; |
|---|
| 119 | $basedir = isset($errorData['basedir']) ? $errorData['basedir'] : false; |
|---|
| 120 | |
|---|
| 121 | if ($path !== false && $basedir !== false) |
|---|
| 122 | { |
|---|
| 123 | $description = sprintf(__('Path: %s is not within basedir reported as: %s', 'shortpixel-image-optimiser'), $path, $basedir); |
|---|
| 124 | } |
|---|
| 125 | break; |
|---|
| 126 | case self::ERROR_DIRECTORY_NOTEXIST: |
|---|
| 127 | $message = __('Specified new directory does not exist. Path must be a relative path from the upload directory and exist', 'enable-media-replace'); |
|---|
| 128 | break; |
|---|
| 129 | |
|---|
| 130 | case self::ERROR_NONCE: |
|---|
| 131 | $message = __('Fail to validate form nonce. Please try again', 'enable-media-replace'); |
|---|
| 132 | $description = __('This can happen when the window is open for a long time and/or there has been a timeout. You can go back to previous screen and try again. If this happens each time when replacing, contact us', 'enable-media-replace'); |
|---|
| 133 | break; |
|---|
| 134 | |
|---|
| 135 | // Remove Background |
|---|
| 136 | case self::ERROR_DOWNLOAD_FAILED: |
|---|
| 137 | $message = __('Replacement Image could not be downloaded or does not exist', 'enable-media-replace'); |
|---|
| 138 | break; |
|---|
| 139 | |
|---|
| 140 | default: |
|---|
| 141 | $message = __('An unknown error has occured', 'enable-media-replace'); |
|---|
| 142 | break; |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | if( false !== $message) |
|---|
| 146 | $this->view->errorMessage = $message; |
|---|
| 147 | |
|---|
| 148 | |
|---|
| 149 | if (false !== $description) |
|---|
| 150 | { |
|---|
| 151 | $this->view->errorDescription = $description; |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | |
|---|
| 155 | $this->loadView('error'); |
|---|
| 156 | exit(); |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | |
|---|
| 160 | protected function viewSuccess() |
|---|
| 161 | { |
|---|
| 162 | wp_enqueue_script('emr_success'); |
|---|
| 163 | $this->loadView('success'); |
|---|
| 164 | exit(); |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | } |
|---|