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