PluginProbe
Waymark / trunk
Waymark vtrunk
1.6.5 1.6.4 1.6.2 1.6.3 1.6.0 1.5.16 trunk 1.4.3 1.5.0 1.5.1 1.5.10 1.5.11 1.5.14 1.5.15 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9
waymark / inc / Waymark_Admin.php

Waymark_Admin.php in Waymark trunk, at inc/Waymark_Admin.php

349 lines 12.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 class Waymark_Admin {
4
5 private $current_screen;
6
7 public function __construct() {
8 //Don't do anything if we're not logged in to the back-end
9 if (! is_admin()) {
10 return;
11 }
12
13 //Actions
14 add_action('admin_init', [$this, 'admin_init']);
15 add_action('admin_menu', [$this, 'menu_init']);
16 add_action('current_screen', [$this, 'current_screen']);
17 add_action('admin_notices', [$this, 'admin_notices']);
18 //add_action('widgets_init', array($this, 'widgets_init'));
19 add_action('admin_action_waymark_duplicate_post', [$this, 'duplicate_post']);
20 add_action('manage_waymark_map_posts_custom_column', [$this, 'map_posts_custom_column'], 10, 2);
21 add_action('template_redirect', [$this, 'redirect_view_to_edit']);
22
23 //Filters
24 add_filter('post_row_actions', [$this, 'edit_post_links'], 10, 2);
25 add_filter('manage_edit-waymark_collection_columns', [$this, 'collection_taxonomy_columns'], 10, 3);
26 add_filter('manage_waymark_collection_custom_column', [$this, 'collection_taxonomy_custom_column'], 10, 3);
27 add_filter('manage_waymark_map_posts_columns', [$this, 'map_posts_columns']);
28 add_filter('wp_read_image_metadata', [$this, 'add_gps_exif'], 10, 3);
29 add_filter('upload_mimes', [$this, 'upload_mimes'], 1);
30 // add_filter('wp_check_filetype_and_ext', array($this, 'wp_check_filetype_and_ext'), 10, 4);
31 }
32
33 public function admin_init() {
34 require_once 'Admin/Waymark_JS.php';
35 require_once 'Admin/Waymark_AJAX.php';
36 require_once 'Admin/Waymark_Meta.php';
37 require_once 'Admin/Waymark_Revisions.php';
38
39 //Add nonce
40 Waymark_JS::add_chunk('//Admin');
41 Waymark_JS::add_chunk('var waymark_multi_value_seperator = "' . Waymark_Config::get_item('multi_value_seperator') . '"');
42
43 //Debug Mode
44 if (Waymark_Config::get_setting('misc', 'advanced', 'debug_mode')) {
45 Waymark_JS::add_call('jQuery("body").addClass("waymark-debug");');
46 }
47 }
48
49 public function menu_init() {
50 require_once 'Admin/Waymark_Settings.php';
51 require_once 'Admin/Waymark_CSS.php';
52 require_once 'Admin/Waymark_Menu.php';
53 }
54
55 public function current_screen() {
56 if (function_exists('get_current_screen')) {
57 $this->current_screen = get_current_screen();
58
59 //Actions for specific admin pages
60 switch ($this->current_screen->base) {
61 //Add / Edit Single
62 case 'post-new':
63 case 'post':
64
65 break;
66
67 //Collections
68 case 'term':
69 case 'edit-tags':
70 if ($this->current_screen->taxonomy == 'waymark_collection') {
71 Waymark_CSS::add_chunk('
72 .inline-edit-col label:last-child,
73 .form-field p {
74 display: none !important;
75 }
76 ');
77 }
78
79 break;
80 }
81 }
82 }
83
84 public function widgets_init() {
85 require_once 'Admin/Waymark_Widgets.php';
86 register_widget('Waymark_Meta_Widget');
87 }
88
89 public function duplicate_post() {
90 //Check for post ID
91 if (! isset($_GET['post_id']) || ! is_numeric($_GET['post_id'])) {
92 wp_die(esc_html__('Can not duplicate, no post to supplied!', 'waymark'));
93 }
94
95 //Nonce verification
96 $get_data = wp_unslash($_GET);
97 if (! isset($get_data['duplicate_nonce']) || ! wp_verify_nonce($get_data['duplicate_nonce'], basename(__FILE__))) {
98 wp_die(esc_html__('Security verification error!', 'waymark'));
99 }
100
101 $Object = new Waymark_Object(esc_attr($get_data['post_id']));
102 $new_post_id = $Object->duplicate_post();
103
104 wp_redirect(admin_url('post.php?action=edit&post=' . $new_post_id));
105 }
106
107 public function edit_post_links($actions, $post) {
108 //Queries & Layout
109 if (in_array($post->post_type, ['waymark_map'])) {
110 //Add Duplicate Link
111 $actions['duplicate'] = '<a href="' . wp_nonce_url('admin.php?action=waymark_duplicate_post&post_id=' . $post->ID, basename(__FILE__), 'duplicate_nonce') . '" title="' . esc_attr__('Duplicate this post', 'waymark') . '" rel="permalink">' . esc_html__('Duplicate', 'waymark') . '</a>';
112
113 //Remove Quick Edit
114 unset($actions['inline hide-if-no-js']);
115 }
116
117 return $actions;
118 }
119
120 public function collection_taxonomy_columns($columns) {
121 unset($columns['slug']);
122 unset($columns['description']);
123
124 $columns['shortcode'] = esc_html__('Shortcode', 'waymark');
125
126 return $columns;
127 }
128
129 public function collection_taxonomy_custom_column($content, $column_name, $term_id) {
130 switch ($column_name) {
131 case 'shortcode';
132
133 //Shortcode output
134 echo '<input type="text" value="[' . esc_html(Waymark_Config::get_item('shortcode')) . ' collection_id=&quot;' . esc_attr($term_id) . '&quot;]" />';
135
136 break;
137 }
138 }
139
140 public function map_posts_columns($columns) {
141 $columns['shortcode'] = 'Shortcode';
142
143 unset($columns['date']);
144 $columns['date'] = esc_html__('Date', 'waymark');
145
146 return $columns;
147 }
148
149 public function map_posts_custom_column($column, $post_id) {
150 switch ($column) {
151 case 'shortcode';
152
153 //Shortcode output
154 echo '<input type="text" value="[' . esc_html(Waymark_Config::get_item('shortcode')) . ' map_id=&quot;' . esc_attr($post_id) . '&quot;]" />';
155
156 break;
157 }
158 }
159
160 public function redirect_view_to_edit() {
161 global $post;
162
163 if (is_single() && $post->post_type == 'waymark_map') {
164 wp_redirect(admin_url('post.php?post=' . $post->ID . '&action=edit'), 301);
165 exit;
166 }
167 }
168
169 public function admin_notices() {
170 if (function_exists('get_current_screen')) {
171 $title = '';
172 $description = '';
173
174 //Collections List
175 if ($this->current_screen->base == 'edit-tags' && $this->current_screen->taxonomy == 'waymark_collection') {
176 // translators: Collection is a group of maps
177 $title = esc_html__('Collections', 'waymark');
178 // translators: %s: URL to the Waymark documentation
179 $description = sprintf(__('Collections allow you to organise your Maps. Use the <a href="%1$s">Shortcode</a> to display all Maps in a Collection at once. <a href="%2$s" class="button waymark-right">Read the Docs &raquo;</a>', 'waymark'), Waymark_Helper::site_url('docs/shortcodes'), Waymark_Helper::site_url('docs/collections'));
180 }
181
182 //Map Posts List
183 if ($this->current_screen->base == 'edit' && $this->current_screen->post_type == 'waymark_map') {
184 // translators: %s: URL to the Waymark documentation
185 $title = esc_html__('Maps', 'waymark');
186 // translators: %s: URL to the Waymark documentation
187 $description = sprintf(__('Create Maps here, then add them to your content using the <a href="%1$s">Shortcode</a>.', 'waymark'), Waymark_Helper::site_url('docs/shortcodes'));
188 }
189
190 $map_posts = get_posts([
191 'post_type' => 'waymark_map',
192 ]);
193
194 //Map New/Edit Page (not Collections... not pages)
195 if ($this->current_screen->post_type == 'waymark_map' && ! $this->current_screen->taxonomy && strpos($this->current_screen->id, 'waymark_page') === false) {
196 global $post;
197
198 //Waymark_Helper::debug($current_screen, false);
199
200 //Add
201 if ($this->current_screen->action == 'add') {
202 //No Maps created yet
203 //if(true || sizeof($map_posts) == 0) {
204 if (sizeof($map_posts) == 0) {
205 // translators: Creating a new map
206 $title = esc_html__('Creating a Map', 'waymark');
207 // translators: %s: URL to the Waymark documentation
208 $description = sprintf(__('<span class="waymark-lead">Use the Map <a href="%1$s">Editor</a> to place Markers, draw Lines/Shapes and display Photos. Each can be given a <a target="_blank" href="%2$s">Type</a>, title, description and an image.</span><br /><br />You can add the Map to your content using the %3$s <a href="%4$s">Shortcode</a>.', 'waymark'), Waymark_Helper::site_url('docs/editor'), Waymark_Helper::site_url('docs/types'), '<code style="font-size:10px">[' . Waymark_Config::get_item('shortcode') . ' map_id=&quot;' . $post->ID . '&quot;]</code>', Waymark_Helper::site_url('docs/shortcodes'));
209 } else {
210 // translators: Creating a new map
211 $title = esc_html__('New Map', 'waymark');
212 // translators: %s: URL to the Waymark documentation
213 $description = sprintf(__('You can add the Map to your content using the %1$s <a href="%2$s">Shortcode</a>.', 'waymark'), '<code style="font-size:10px">[' . Waymark_Config::get_item('shortcode') . ' map_id=&quot;' . $post->ID . '&quot;]</code>', Waymark_Helper::site_url('docs/shortcodes'));
214 }
215 } elseif ($this->current_screen->base == 'post') {
216 // translators: Editing a map
217 $title = esc_html__('Edit Map', 'waymark');
218 // translators: %s: URL to the Waymark documentation
219 $description = sprintf(__('You can add the Map to your content using the %1$s <a href="%2$s">Shortcode</a>.', 'waymark'), '<code style="font-size:10px">[' . Waymark_Config::get_item('shortcode') . ' map_id=&quot;' . $post->ID . '&quot;]</code>', Waymark_Helper::site_url('docs/shortcodes'));
220 }
221 }
222
223 }
224
225 // START Plugin Callout
226 if ($title || $this->current_screen->base == 'waymark_page_waymark-settings') {
227 echo '<div id="waymark-notice-container" class="plugin-callout">' . "\n";
228 echo ' <div class="logo">' . Waymark_Helper::logo('primary', 32, 32) . ' </div>' . "\n";
229 echo ' <p class="lead">Waymark currently has <strong>TWO</strong> sponsors. Software takes time and effort to maintain. If you like this plugin:</p>' . "\n";
230 echo ' <p style="font-size:150%">Help <strong>keep it alive <a href="https://github.com/sponsors/OpenGIS">Through Sponsorship</a>.</strong></p>' . "\n";
231 echo ' <p>Thank you <a href="https://github.com/infester86">infester86</a> & <a href="https://github.com/jhmnieuwenhuis">jhmnieuwenhuis</a> ! ❤️</p>' . "\n";
232 echo '</div>' . "\n";
233
234 return;
235 }
236 // END Plugin Callout
237
238 if ($title || $description) {
239 echo '<div id="waymark-notice-container" class="wrap">' . "\n";
240 echo ' <div class="card">' . "\n";
241 echo ' <h1>' . esc_html($title) . '</h1>' . "\n";
242 echo ' <p>' . wp_kses($description, [
243 'a' => ['href' => [], 'class' => []],
244 'code' => [],
245 ]) . '</p>' . "\n";
246 echo ' </div>' . "\n";
247 echo '</div>' . "\n";
248 }
249 }
250
251 //Thanks to https://kristarella.blog/2009/04/add-image-exif-metadata-to-wordpress/
252 public function add_gps_exif($meta, $file, $sourceImageType) {
253 //Required the PHP EXIF extension
254 if (! function_exists('exif_read_data')) {
255 return $meta;
256 }
257
258 $exif_data = exif_read_data($file);
259
260 if (is_array($exif_data)) {
261 //Latitude
262 if (array_key_exists('GPSLatitude', $exif_data) && array_key_exists('GPSLatitudeRef', $exif_data)) {
263 $meta['GPSLatitude'] = $exif_data['GPSLatitude'];
264 $meta['GPSLatitudeRef'] = $exif_data['GPSLatitudeRef'];
265
266 //Make decimal
267 $meta['GPSLatitudeNum'] = Waymark_Helper::exif_gps_to_gps_float($exif_data['GPSLatitude'], $exif_data['GPSLatitudeRef']);
268 }
269
270 //Longitude
271 if (array_key_exists('GPSLongitude', $exif_data) && array_key_exists('GPSLongitudeRef', $exif_data)) {
272 $meta['GPSLongitude'] = $exif_data['GPSLongitude'];
273 $meta['GPSLongitudeRef'] = $exif_data['GPSLongitudeRef'];
274
275 //Make decimal
276 $meta['GPSLongitudeNum'] = Waymark_Helper::exif_gps_to_gps_float($exif_data['GPSLongitude'], $exif_data['GPSLongitudeRef']);
277 }
278 }
279
280 return $meta;
281 }
282
283 public function upload_mimes($existing_mimes) {
284 //Don't filter if current_screen is not set (e.g., AJAX uploads)
285 if (!$this->current_screen) {
286 return $existing_mimes;
287 }
288
289 //Don't do this for admin pages not related to Waymark
290 if ($this->current_screen->post_type != 'waymark_map') {
291 return $existing_mimes;
292 }
293
294 //Returning limited subset
295 $upload_mimes = [];
296
297 //Images only
298 foreach ($existing_mimes as $mime_key => $mime_string) {
299 if (strpos($mime_string, 'image/') === 0) {
300 $upload_mimes[$mime_key] = $mime_string;
301 }
302 }
303
304 //Files
305 foreach (Waymark_Config::get_item('mimes', 'file') as $ext => $mime) {
306 if (is_array($mime)) {
307 $upload_mimes[$ext] = $mime[0];
308 } elseif (is_string($mime)) {
309 $upload_mimes[$ext] = $mime;
310 }
311 }
312
313 return $upload_mimes;
314 }
315
316 public function wp_check_filetype_and_ext($check, $file, $filename, $mimes) {
317 //Check file type
318 $filetype = wp_check_filetype($filename, Waymark_Config::get_item('mimes', 'file'));
319
320 //File type we are interested in
321 if (array_key_exists('type', $filetype) && $filetype['type']) {
322 //Allow
323 return [
324 'ext' => $filetype['ext'],
325 'type' => $filetype['type'],
326 'proper_filename' => $filename,
327 ];
328 }
329
330 return $check;
331 }
332 // function wp_check_filetype_and_ext( $check, $file, $filename, $mimes ) {
333 // //Check file type
334 // $filetype = wp_check_filetype($filename, Waymark_Config::get_item('mimes', 'file'));
335 //
336 // //File type we are interested in
337 // if(array_key_exists('type', $filetype) && $filetype['type']) {
338 // //Allow
339 // return array(
340 // 'ext' => $filetype['ext'],
341 // 'type' => $filetype['type'],
342 // 'proper_filename' => $filename
343 // );
344 // }
345 //
346 // return $check;
347 // }
348 }
349