| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentSnippets\App\Http\Controllers; |
| 4 |
|
| 5 |
use FluentSnippets\App\Helpers\Arr; |
| 6 |
use FluentSnippets\App\Helpers\Helper; |
| 7 |
use FluentSnippets\App\Model\Snippet; |
| 8 |
|
| 9 |
class SnippetsController |
| 10 |
{ |
| 11 |
public static function getSnippets(\WP_REST_Request $request) |
| 12 |
{ |
| 13 |
$snippetModel = new Snippet([ |
| 14 |
'search' => sanitize_text_field($request->get_param('search')), |
| 15 |
'type' => sanitize_text_field($request->get_param('type')), |
| 16 |
'tag' => sanitize_text_field($request->get_param('tag')), |
| 17 |
'sort_by' => sanitize_text_field($request->get_param('sort_by')), |
| 18 |
'sort_order' => strtolower(sanitize_text_field($request->get_param('sort_order'))), |
| 19 |
]); |
| 20 |
|
| 21 |
$perPage = $request->get_param('per_page'); |
| 22 |
$page = $request->get_param('page'); |
| 23 |
|
| 24 |
if (!$perPage) { |
| 25 |
$perPage = 10; |
| 26 |
} |
| 27 |
|
| 28 |
if (!$page) { |
| 29 |
$page = 1; |
| 30 |
} |
| 31 |
|
| 32 |
return [ |
| 33 |
'snippets' => $snippetModel->getIndexedSnippets($perPage, $page) |
| 34 |
]; |
| 35 |
} |
| 36 |
|
| 37 |
public static function findSnippet(\WP_REST_Request $request) |
| 38 |
{ |
| 39 |
$snippetName = sanitize_file_name($request->get_param('snippet_name')); |
| 40 |
|
| 41 |
$snippetModel = new Snippet(); |
| 42 |
$snippet = $snippetModel->findByFileName($snippetName); |
| 43 |
|
| 44 |
if (is_wp_error($snippet)) { |
| 45 |
return $snippet; |
| 46 |
} |
| 47 |
|
| 48 |
$snippet['file_name'] = basename($snippet['file']); |
| 49 |
|
| 50 |
if ($snippet['meta']['type'] == 'PHP') { |
| 51 |
// Remove Beginning php tag |
| 52 |
$snippet['code'] = preg_replace('/^<\?php/', '', $snippet['code']); |
| 53 |
// remove new line at the very first |
| 54 |
$snippet['code'] = ltrim($snippet['code'], PHP_EOL); |
| 55 |
} |
| 56 |
|
| 57 |
$config = Helper::getIndexedConfig(); |
| 58 |
|
| 59 |
if (!empty($config['error_files']) && !empty($config['error_files'][$snippet['file_name']])) { |
| 60 |
$snippet['error'] = $config['error_files'][$snippet['file_name']]; |
| 61 |
} |
| 62 |
|
| 63 |
return [ |
| 64 |
'snippet' => $snippet |
| 65 |
]; |
| 66 |
} |
| 67 |
|
| 68 |
public static function createSnippet(\WP_REST_Request $request) |
| 69 |
{ |
| 70 |
if ($restricted = self::isBlockedRequest()) { |
| 71 |
return $restricted; |
| 72 |
} |
| 73 |
|
| 74 |
$meta = json_decode($request->get_param('meta'), true); |
| 75 |
$code = $meta['code']; |
| 76 |
|
| 77 |
unset($meta['code']); |
| 78 |
|
| 79 |
$metaValidated = self::validateMeta($meta); |
| 80 |
|
| 81 |
if (is_wp_error($metaValidated)) { |
| 82 |
return $metaValidated; |
| 83 |
} |
| 84 |
|
| 85 |
$meta['status'] = 'draft'; |
| 86 |
|
| 87 |
// Check if the php snippet $code is valid or not by validating it |
| 88 |
|
| 89 |
if ($meta['type'] == 'PHP') { |
| 90 |
// Check if the code starts with <?php |
| 91 |
if (preg_match('/^<\?php/', $code)) { |
| 92 |
return new \WP_Error('invalid_code', 'Please remove <?php from the beginning of the code', [ |
| 93 |
'code' => 'Please remove <?php from the beginning of the code' |
| 94 |
]); |
| 95 |
} |
| 96 |
$code = rtrim($code, '?>'); |
| 97 |
$code = '<?php' . PHP_EOL . $code; |
| 98 |
} else if ($meta['type'] == 'php_content') { |
| 99 |
$code = apply_filters('fluent_snippets/sanitize_mixed_content', $code, $meta); |
| 100 |
if (is_wp_error($code)) { |
| 101 |
return $code; |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
// Validate the code |
| 106 |
$validated = Helper::validateCode($meta['type'], $code); |
| 107 |
|
| 108 |
if (is_wp_error($validated)) { |
| 109 |
|
| 110 |
$message = $validated->get_error_message(); |
| 111 |
$additionalData = $validated->get_error_data(); |
| 112 |
|
| 113 |
if ($lineNumber = Arr::get($additionalData, 'line')) { |
| 114 |
if (is_numeric($lineNumber) && $lineNumber > 1) { |
| 115 |
$lineNumber = $lineNumber - 1; |
| 116 |
$additionalData['line'] = $lineNumber; |
| 117 |
} |
| 118 |
|
| 119 |
$message .= ' on line ' . $lineNumber; |
| 120 |
} |
| 121 |
|
| 122 |
return new \WP_Error('code', $message, [ |
| 123 |
'code' => $message, |
| 124 |
'code_explanation' => $additionalData |
| 125 |
]); |
| 126 |
} |
| 127 |
|
| 128 |
$settings = Helper::getConfigSettings(); |
| 129 |
|
| 130 |
if ($settings['auto_publish'] == 'yes') { |
| 131 |
$meta['status'] = 'published'; |
| 132 |
} |
| 133 |
|
| 134 |
// check if the $code which is a php snippet is valid or not |
| 135 |
$snippetModel = new Snippet(); |
| 136 |
$snippet = $snippetModel->createSnippet($code, $meta); |
| 137 |
do_action('fluent_snippets/snippet_created', $snippet); |
| 138 |
|
| 139 |
return [ |
| 140 |
'snippet' => $snippet, |
| 141 |
'message' => __('Snippet created successfully', 'easy-code-manager') |
| 142 |
]; |
| 143 |
} |
| 144 |
|
| 145 |
public static function updateSnippet(\WP_REST_Request $request) |
| 146 |
{ |
| 147 |
if ($restricted = self::isBlockedRequest()) { |
| 148 |
return $restricted; |
| 149 |
} |
| 150 |
|
| 151 |
$fileName = sanitize_file_name($request->get_param('fluent_saving_snippet_name')); |
| 152 |
$meta = json_decode($request->get_param('meta'), true); |
| 153 |
$code = $meta['code']; |
| 154 |
unset($meta['code']); |
| 155 |
|
| 156 |
$metaValidated = self::validateMeta($meta); |
| 157 |
|
| 158 |
if (is_wp_error($metaValidated)) { |
| 159 |
return $metaValidated; |
| 160 |
} |
| 161 |
|
| 162 |
if ($meta['type'] == 'PHP') { |
| 163 |
// Check if the code starts with <?php |
| 164 |
if (preg_match('/^<\?php/', $code)) { |
| 165 |
return new \WP_Error('invalid_code', 'Please remove <?php from the beginning of the code', [ |
| 166 |
'code' => 'Please remove <?php from the beginning of the code' |
| 167 |
]); |
| 168 |
} |
| 169 |
|
| 170 |
$code = rtrim($code, '?>'); |
| 171 |
$code = '<?php' . PHP_EOL . $code; |
| 172 |
} else if ($meta['type'] == 'php_content') { |
| 173 |
$code = apply_filters('fluent_snippets/sanitize_mixed_content', $code, $meta); |
| 174 |
if (is_wp_error($code)) { |
| 175 |
return $code; |
| 176 |
} |
| 177 |
} else { |
| 178 |
$sanitizedCode = Helper::escCssJs($code); |
| 179 |
if ($sanitizedCode !== $code) { |
| 180 |
return new \WP_Error('invalid_code', 'Please remove any any style or script tag from the code'); |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
// Validate the code |
| 185 |
$validated = Helper::validateCode($meta['type'], $code); |
| 186 |
|
| 187 |
if (is_wp_error($validated)) { |
| 188 |
|
| 189 |
$message = $validated->get_error_message(); |
| 190 |
$additionalData = $validated->get_error_data(); |
| 191 |
|
| 192 |
if ($lineNumber = Arr::get($additionalData, 'line')) { |
| 193 |
if (is_numeric($lineNumber) && $lineNumber > 1) { |
| 194 |
$lineNumber = $lineNumber - 1; |
| 195 |
$additionalData['line'] = $lineNumber; |
| 196 |
} |
| 197 |
|
| 198 |
$message .= ' on line ' . $lineNumber; |
| 199 |
} |
| 200 |
|
| 201 |
return new \WP_Error('code', $message, [ |
| 202 |
'code' => $message, |
| 203 |
'code_explanation' => $additionalData |
| 204 |
]); |
| 205 |
} |
| 206 |
|
| 207 |
// check if the $code which is a php snippet is valid or not |
| 208 |
$snippetModel = new Snippet(); |
| 209 |
$snippet = $snippetModel->updateSnippet($fileName, $code, $meta); |
| 210 |
|
| 211 |
if ($request->get_param('reactivate')) { |
| 212 |
$config = Helper::getIndexedConfig(); |
| 213 |
if (isset($config['error_files'][$fileName])) { |
| 214 |
unset($config['error_files'][$fileName]); |
| 215 |
} |
| 216 |
Helper::saveIndexedConfig($config); |
| 217 |
} |
| 218 |
|
| 219 |
do_action('fluent_snippets/snippet_updated', $snippet); |
| 220 |
|
| 221 |
return [ |
| 222 |
'snippet' => $snippet, |
| 223 |
'message' => 'Snippet updated successfully' |
| 224 |
]; |
| 225 |
} |
| 226 |
|
| 227 |
public static function updateSnippetStatus(\WP_REST_Request $request) |
| 228 |
{ |
| 229 |
if ($restricted = self::isBlockedRequest()) { |
| 230 |
return $restricted; |
| 231 |
} |
| 232 |
|
| 233 |
$fileName = sanitize_file_name($request->get_param('fluent_saving_snippet_name')); |
| 234 |
$status = sanitize_text_field($request->get_param('status')); |
| 235 |
|
| 236 |
$snippetModel = new Snippet(); |
| 237 |
$snippet = $snippetModel->findByFileName($fileName); |
| 238 |
|
| 239 |
if (is_wp_error($snippet)) { |
| 240 |
return $snippet; |
| 241 |
} |
| 242 |
|
| 243 |
if ($status != 'published') { |
| 244 |
$status = 'draft'; |
| 245 |
} |
| 246 |
|
| 247 |
$snippet['meta']['status'] = $status; |
| 248 |
|
| 249 |
$snippetName = $snippetModel->updateSnippet($fileName, $snippet['code'], $snippet['meta']); |
| 250 |
|
| 251 |
do_action('fluent_snippets/snippet_status_updated', $snippetName); |
| 252 |
do_action('fluent_snippets/snippet_updated', $snippetName); |
| 253 |
|
| 254 |
return [ |
| 255 |
'snippet' => $snippet, |
| 256 |
'message' => 'Snippet status updated successfully' |
| 257 |
]; |
| 258 |
} |
| 259 |
|
| 260 |
public static function deleteSnippet(\WP_REST_Request $request) |
| 261 |
{ |
| 262 |
if ($restricted = self::isBlockedRequest()) { |
| 263 |
return $restricted; |
| 264 |
} |
| 265 |
|
| 266 |
$fileName = sanitize_file_name($request->get_param('fluent_saving_snippet_name')); |
| 267 |
|
| 268 |
$snippetModel = new Snippet(); |
| 269 |
$snippet = $snippetModel->findByFileName($fileName); |
| 270 |
|
| 271 |
if (is_wp_error($snippet)) { |
| 272 |
return $snippet; |
| 273 |
} |
| 274 |
|
| 275 |
$snippetModel->deleteSnippet($fileName); |
| 276 |
|
| 277 |
do_action('fluent_snippets/snippet_deleted', $fileName); |
| 278 |
|
| 279 |
return [ |
| 280 |
'message' => __('Snippet has been deleted successfully', 'easy-code-manager') |
| 281 |
]; |
| 282 |
} |
| 283 |
|
| 284 |
private static function validateMeta($meta) |
| 285 |
{ |
| 286 |
$required = ['name', 'status', 'type', 'run_at']; |
| 287 |
|
| 288 |
foreach ($required as $key) { |
| 289 |
if (empty($meta[$key])) { |
| 290 |
return new \WP_Error($key, sprintf(__('%s is required', 'easy-code-manager'), $key), [ |
| 291 |
$key => sprintf(__('%s is required', 'easy-code-manager'), $key) |
| 292 |
]); |
| 293 |
} |
| 294 |
} |
| 295 |
|
| 296 |
return true; |
| 297 |
} |
| 298 |
|
| 299 |
private static function isBlockedRequest() |
| 300 |
{ |
| 301 |
if (current_user_can('unfiltered_html')) { |
| 302 |
return false; |
| 303 |
} |
| 304 |
|
| 305 |
return new \WP_Error('invalid_request', 'You do not have permission to perform this action. Required Permission: unfiltered_html'); |
| 306 |
} |
| 307 |
} |
| 308 |
|