PluginProbe
FluentSnippets – High-Performance Code Snippets, Header & Footer Code, Custom CSS & PHP Code Manager / trunk
FluentSnippets – High-Performance Code Snippets, Header & Footer Code, Custom CSS & PHP Code Manager vtrunk
10.56 1.2.1 10 10.1 10.2 10.3 10.31 10.32 10.33 10.34 10.50 10.51 10.52 10.53 10.55 9.0 9.0.1 9.4 trunk 1.0.0 1.1 1.2
easy-code-manager / app / Http / Controllers / SnippetsController.php

SnippetsController.php in FluentSnippets – High-Performance Code Snippets, Header & Footer Code, Custom CSS & PHP Code Manager trunk, at app/Http/Controllers/SnippetsController.php

180 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 /**
11 * Heal an index that has drifted from the files on disk.
12 *
13 * The admin app calls this once when it boots, instead of getSnippets() doing it
14 * on every list load, search keystroke and pagination click. Returns whether a
15 * rebuild happened so the app only re-fetches the list when it would differ.
16 */
17 public static function syncIndex(\WP_REST_Request $request)
18 {
19 return [
20 'changed' => Helper::syncSnippetIndex()
21 ];
22 }
23
24 public static function getSnippets(\WP_REST_Request $request)
25 {
26 $snippetModel = new Snippet([
27 'search' => sanitize_text_field($request->get_param('search')),
28 'type' => sanitize_text_field($request->get_param('type')),
29 'tag' => sanitize_text_field($request->get_param('tag')),
30 'sort_by' => sanitize_text_field($request->get_param('sort_by')),
31 'sort_order' => strtolower(sanitize_text_field($request->get_param('sort_order'))),
32 ]);
33
34 $perPage = $request->get_param('per_page');
35 $page = $request->get_param('page');
36
37 if (!$perPage) {
38 $perPage = 10;
39 }
40
41 if (!$page) {
42 $page = 1;
43 }
44
45 $data = [
46 'snippets' => $snippetModel->getIndexedSnippets($perPage, $page),
47 'time' => current_time('mysql')
48 ];
49
50 if ($page == 1) {
51 [$tags, $groups] = (new Snippet())->getAllSnippetTagsGroups();
52 $data['tags'] = $tags;
53 $data['groups'] = $groups;
54 }
55
56 return $data;
57 }
58
59 public static function findSnippet(\WP_REST_Request $request)
60 {
61 $snippetName = sanitize_file_name($request->get_param('snippet_name'));
62
63 $snippetModel = new Snippet();
64 $snippet = $snippetModel->findByFileName($snippetName);
65
66 if (is_wp_error($snippet)) {
67 return $snippet;
68 }
69
70 $snippet['file_name'] = basename($snippet['file']);
71
72 if ($snippet['meta']['type'] == 'PHP') {
73 // Remove Beginning php tag
74 $snippet['code'] = preg_replace('/^<\?php/', '', $snippet['code']);
75 // remove new line at the very first
76 $snippet['code'] = ltrim($snippet['code'], PHP_EOL);
77 }
78
79 $config = Helper::getIndexedConfig();
80
81 if (!empty($config['error_files']) && !empty($config['error_files'][$snippet['file_name']])) {
82 $snippet['error'] = $config['error_files'][$snippet['file_name']];
83 }
84
85 return [
86 'snippet' => $snippet
87 ];
88 }
89
90 public static function updateSnippetStatus(\WP_REST_Request $request)
91 {
92 if ($restricted = self::denyUnlessCanAuthorSnippets()) {
93 return $restricted;
94 }
95
96 $fileName = sanitize_file_name($request->get_param('fluent_saving_snippet_name'));
97 $status = sanitize_text_field($request->get_param('status'));
98
99 $snippetModel = new Snippet();
100 $snippet = $snippetModel->findByFileName($fileName);
101
102 if (is_wp_error($snippet)) {
103 return $snippet;
104 }
105
106 if ($status != 'published') {
107 $status = 'draft';
108 }
109
110 $snippet['meta']['status'] = $status;
111
112 $snippetName = $snippetModel->updateSnippet($fileName, $snippet['code'], $snippet['meta']);
113
114 do_action('fluent_snippets/snippet_status_updated', $snippetName);
115 do_action('fluent_snippets/snippet_updated', $snippetName);
116
117 return [
118 'snippet' => $snippet,
119 'message' => 'Snippet status updated successfully'
120 ];
121 }
122
123 public static function deleteSnippet(\WP_REST_Request $request)
124 {
125 if ($restricted = self::denyUnlessCanAuthorSnippets()) {
126 return $restricted;
127 }
128
129 $fileName = sanitize_file_name($request->get_param('fluent_saving_snippet_name'));
130
131 $snippetModel = new Snippet();
132 $snippet = $snippetModel->findByFileName($fileName);
133
134 if (is_wp_error($snippet)) {
135 return $snippet;
136 }
137
138 $snippetModel->deleteSnippet($fileName);
139
140 do_action('fluent_snippets/snippet_deleted', $fileName);
141
142 return [
143 'message' => __('Snippet has been deleted successfully', 'easy-code-manager')
144 ];
145 }
146
147 /**
148 * Kept as an entry point because AdminMenuHandler's import calls it, but the rules
149 * live in Helper::validateMeta(). This was a byte-for-byte duplicate of that method,
150 * which is exactly how the two drift apart (L7).
151 */
152 public static function validateMeta($meta)
153 {
154 return Helper::validateMeta($meta);
155 }
156
157 /**
158 * Guard for anything that writes, publishes or removes a snippet — all of which
159 * amount to putting code on the site.
160 *
161 * Deliberately NOT the same test as SettingsController::denyUnlessCanManageSettings(),
162 * which also wants manage_options. The two used to share the name isBlockedRequest(),
163 * which made the difference look accidental.
164 *
165 * install_plugins is checked here rather than left to route registration. It used to
166 * be safe to assume, because every route in this plugin required it; now that reading
167 * and writing have separate gates (see Http/routes.php), assuming it is how a write
168 * ends up reachable from a read-only screen the first time somebody moves a route
169 * between the two lists.
170 */
171 private static function denyUnlessCanAuthorSnippets()
172 {
173 if (current_user_can('install_plugins') && current_user_can('unfiltered_html')) {
174 return false;
175 }
176
177 return new \WP_Error('invalid_request', 'You do not have permission to perform this action. Required Permission: install_plugins & unfiltered_html');
178 }
179 }
180