PluginProbe ʕ •ᴥ•ʔ
Permalink Manager Lite / 2.4.1.4
Permalink Manager Lite v2.4.1.4
2.6.0 2.5.5 2.5.4 2.5.3.4 2.2.18 2.2.19.2 2.2.19.3 2.2.19.3.1 2.2.2 2.2.20 2.2.20.1 2.2.20.3 2.2.4 2.2.5 2.2.6 2.2.7.2 2.2.7.3 2.2.7.5 2.2.7.6 2.2.8.4 2.2.8.5 2.2.8.6 2.2.8.7 2.2.8.9 2.2.9.1 2.2.9.2 2.2.9.2.1 2.2.9.3 2.2.9.4 2.2.9.6 2.2.9.7 2.2.9.9 2.3.0 2.3.1.1 2.4.0 2.4.1 2.4.1.2 2.4.1.3 2.4.1.4 2.4.1.5 2.4.1.6 2.4.2 2.4.2.1 2.4.3 2.4.3.1 2.4.3.2 2.4.3.3 2.4.3.4 2.4.4 2.4.4.1 2.4.4.2 2.4.4.3 2.5.0 2.5.1 2.5.1.1 2.5.1.2 2.5.1.3 2.5.1.4 2.5.2 2.5.2.1 2.5.2.2 2.5.2.3 2.5.2.4 2.5.3 2.5.3.1 2.5.3.2 2.5.3.3 trunk 0.2 0.3 0.3.1 0.3.2 0.3.3 0.3.4 0.4 0.4.1 0.4.2 0.4.3 0.4.4 0.4.6 0.4.7 0.4.8 0.4.9 0.5.3 0.5.4 1.0.0 1.0.1 1.0.4 1.1.0 1.1.1 1.1.2 1.11.6.3 2.0.0 2.0.3 2.0.4 2.0.4.3 2.0.5.1 2.0.5.2 2.0.5.3 2.0.5.3.1 2.0.5.4 2.0.5.4a 2.0.5.5 2.0.5.6 2.0.5.6.1 2.0.5.7 2.0.5.9a 2.0.6.2.1 2.0.6.2a 2.0.6.3 2.1.0 2.1.1 2.1.2.4 2.2.0 2.2.1.1 2.2.1.2 2.2.11 2.2.12 2.2.13.1 2.2.14 2.2.15.1 2.2.16 2.2.17
permalink-manager / includes / core / permalink-manager-admin-functions.php
permalink-manager / includes / core Last commit date
permalink-manager-actions.php 2 years ago permalink-manager-admin-functions.php 2 years ago permalink-manager-core-functions.php 2 years ago permalink-manager-debug.php 2 years ago permalink-manager-gutenberg.php 2 years ago permalink-manager-helper-functions.php 2 years ago permalink-manager-uri-functions-post.php 2 years ago permalink-manager-uri-functions.php 2 years ago
permalink-manager-admin-functions.php
400 lines
1 <?php
2
3 /**
4 * Additional functions related to WordPress Admin Dashboard UI
5 */
6 class Permalink_Manager_Admin_Functions {
7
8 public $menu_name, $sections, $active_section, $active_subsection;
9
10 public function __construct() {
11 add_action( 'admin_menu', array( $this, 'add_menu_page' ) );
12 add_action( 'admin_init', array( $this, 'init' ) );
13 add_action( 'admin_bar_menu', array( $this, 'fix_customize_url' ), 41 );
14
15 add_action( 'admin_notices', array( $this, 'display_plugin_notices' ) );
16 add_action( 'admin_notices', array( $this, 'display_global_notices' ) );
17 }
18
19 /**
20 * Hooks that should be triggered with "admin_init"
21 */
22 public function init() {
23 // Additional links in "Plugins" page
24 add_filter( "plugin_action_links_" . PERMALINK_MANAGER_BASENAME, array( $this, "plugins_page_links" ) );
25 add_filter( "plugin_row_meta", array( $this, "plugins_page_meta" ), 10, 2 );
26
27 // Detect current section
28 $this->sections = apply_filters( 'permalink_manager_sections', array() );
29 $this->get_current_section();
30 }
31
32 /**
33 * Use the native URL for "Customize" button in the admin bar
34 *
35 * @param WP_Admin_Bar $wp_admin_bar
36 */
37 public function fix_customize_url( $wp_admin_bar ) {
38 global $permalink_manager_ignore_permalink_filters;
39
40 $object = get_queried_object();
41 $customize = $wp_admin_bar->get_node( 'customize' );
42
43 if ( empty( $customize->href ) ) {
44 return;
45 }
46
47 $permalink_manager_ignore_permalink_filters = true;
48 if ( ! empty( $object->ID ) ) {
49 $new_url = get_permalink( $object->ID );
50 }
51 $permalink_manager_ignore_permalink_filters = false;
52
53 if ( ! empty( $new_url ) ) {
54 $new_url = urlencode_deep( $new_url );
55 $customize_url = preg_replace( '/url=([^&]+)/', "url={$new_url}", $customize->href );
56
57 $wp_admin_bar->add_node( array(
58 'id' => 'customize',
59 'href' => $customize_url,
60 ) );
61 }
62 }
63
64 /**
65 * Get current section of Permalink Manager admin panel
66 */
67 public function get_current_section() {
68 global $active_section, $active_subsection, $current_admin_tax;
69
70 // 1. Get current section
71 if ( isset( $_GET['page'] ) && $_GET['page'] == PERMALINK_MANAGER_PLUGIN_SLUG ) {
72 if ( isset( $_POST['section'] ) ) {
73 $this->active_section = sanitize_title_with_dashes( $_POST['section'] );
74 } else if ( isset( $_GET['section'] ) ) {
75 $this->active_section = sanitize_title_with_dashes( $_GET['section'] );
76 } else {
77 $sections_names = array_keys( $this->sections );
78 $this->active_section = $sections_names[0];
79 }
80 }
81
82 // 2. Get current subsection
83 if ( $this->active_section && isset( $this->sections[ $this->active_section ]['subsections'] ) ) {
84 if ( isset( $_POST['subsection'] ) ) {
85 $this->active_subsection = sanitize_title_with_dashes( $_POST['subsection'] );
86 } else if ( isset( $_GET['subsection'] ) ) {
87 $this->active_subsection = sanitize_title_with_dashes( $_GET['subsection'] );
88 } else {
89 $subsections_names = array_keys( $this->sections[ $this->active_section ]['subsections'] );
90 $this->active_subsection = $subsections_names[0];
91 }
92 }
93
94 // 3. Check if current admin page is related to taxonomies
95 if ( ! empty( $this->active_subsection ) && substr( $this->active_subsection, 0, 4 ) == 'tax_' ) {
96 $current_admin_tax = substr( $this->active_subsection, 4, strlen( $this->active_subsection ) );
97 } else {
98 $current_admin_tax = false;
99 }
100
101 // Set globals
102 $active_section = $this->active_section;
103 $active_subsection = $this->active_subsection;
104 }
105
106 /**
107 * Add "Tools -> Permalink Manager" to the admin sidebar menu
108 */
109 public function add_menu_page() {
110 $this->menu_name = add_management_page( __( 'Permalink Manager', 'permalink-manager' ), __( 'Permalink Manager', 'permalink-manager' ), 'manage_options', PERMALINK_MANAGER_PLUGIN_SLUG, array( $this, 'display_section' ) );
111
112 add_action( 'admin_init', array( $this, 'enqueue_styles' ) );
113 add_action( 'admin_init', array( $this, 'enqueue_scripts' ) );
114 }
115
116 /**
117 * Display the plugin sections
118 */
119 public function display_section() {
120 echo Permalink_Manager_UI_Elements::get_plugin_sections_html( $this->sections, $this->active_section, $this->active_subsection );
121 }
122
123 /**
124 * Register the CSS files for the plugin's dashboard
125 */
126 public function enqueue_styles() {
127 wp_enqueue_style( 'permalink-manager-plugins', PERMALINK_MANAGER_URL . '/out/permalink-manager-plugins.css', array(), PERMALINK_MANAGER_VERSION );
128 wp_enqueue_style( 'permalink-manager', PERMALINK_MANAGER_URL . '/out/permalink-manager-admin.css', array( 'permalink-manager-plugins' ), PERMALINK_MANAGER_VERSION );
129 }
130
131 /**
132 * Register the JavaScript files for the plugin's dashboard.
133 */
134 public function enqueue_scripts() {
135 wp_enqueue_script( 'permalink-manager-plugins', PERMALINK_MANAGER_URL . '/out/permalink-manager-plugins.js', array( 'jquery', ), PERMALINK_MANAGER_VERSION );
136 wp_enqueue_script( 'permalink-manager', PERMALINK_MANAGER_URL . '/out/permalink-manager-admin.js', array( 'jquery', 'permalink-manager-plugins' ), PERMALINK_MANAGER_VERSION );
137
138 wp_localize_script( 'permalink-manager', 'permalink_manager', array(
139 'ajax_url' => admin_url( 'admin-ajax.php' ),
140 'url' => PERMALINK_MANAGER_URL,
141 'confirm' => __( 'Are you sure? This action cannot be undone!', 'permalink-manager' ),
142 'spinners' => admin_url( 'images' )
143 ) );
144
145 }
146
147 /**
148 * Get the URL of the plugin's dashboard
149 *
150 * @param string $append
151 *
152 * @return string
153 */
154 public static function get_admin_url( $append = '' ) {
155 //return menu_page_url(PERMALINK_MANAGER_PLUGIN_SLUG, false) . $append;
156 $admin_page = sprintf( "tools.php?page=%s", PERMALINK_MANAGER_PLUGIN_SLUG . $append );
157
158 return admin_url( $admin_page );
159 }
160
161 /**
162 * Add shortcut links for Permalink Manager on "Plugins" page
163 *
164 * @param array $links
165 *
166 * @return array
167 */
168 public function plugins_page_links( $links ) {
169 $new_links = array(
170 sprintf( '<a href="%s">%s</a>', $this->get_admin_url(), __( 'URI Editor', 'permalink-manager' ) ),
171 sprintf( '<a href="%s">%s</a>', $this->get_admin_url( '&section=settings' ), __( 'Settings', 'permalink-manager' ) ),
172 );
173
174 return array_merge( $links, $new_links );
175 }
176
177 /**
178 * Add shortcut meta links for Permalink Manager on "Plugins" page
179 *
180 * @param array $links
181 * @param string $file
182 *
183 * @return array
184 */
185 public function plugins_page_meta( $links, $file ) {
186 if ( $file == PERMALINK_MANAGER_BASENAME ) {
187 $new_links = array(
188 'doc' => sprintf( '<a href="%s?utm_source=plugin_admin_page" target="_blank">%s</a>', 'https://permalinkmanager.pro/docs/', __( 'Documentation', 'permalink-manager' ) )
189 );
190
191 if ( ! defined( 'PERMALINK_MANAGER_PRO' ) ) {
192 $new_links['upgrade'] = sprintf( '<a href="%s?utm_source=plugin_admin_page" target="_blank"><strong>%s</strong></a>', PERMALINK_MANAGER_WEBSITE, __( 'Buy Permalink Manager Pro', 'permalink-manager' ) );
193 }
194
195 $links = array_merge( $links, $new_links );
196 }
197
198 return $links;
199 }
200
201 /**
202 * Check if URI Editor should be displayed for current user
203 *
204 * @return bool
205 */
206 public static function current_user_can_edit_uris() {
207 global $permalink_manager_options;
208
209 $edit_uris_cap = ( ! empty( $permalink_manager_options['general']['edit_uris_cap'] ) ) ? $permalink_manager_options['general']['edit_uris_cap'] : 'publish_posts';
210
211 return current_user_can( $edit_uris_cap );
212 }
213
214 /**
215 * Display global notices (throughout wp-admin dashboard)
216 */
217 function display_global_notices() {
218 global $permalink_manager_alerts, $active_section;
219
220 $html = "";
221 if ( ! empty( $permalink_manager_alerts ) && is_array( $permalink_manager_alerts ) ) {
222 foreach ( $permalink_manager_alerts as $alert_id => $alert ) {
223 $dismissed_transient_name = sprintf( 'permalink-manager-notice_%s', sanitize_title( $alert_id ) );
224 $dismissed = get_transient( $dismissed_transient_name );
225
226 // Check if alert was dismissed
227 if ( empty( $dismissed ) ) {
228 // Display the notice only on the plugin pages
229 if ( empty( $active_section ) && ! empty( $alert['plugin_only'] ) ) {
230 continue;
231 }
232
233 // Check if the notice did not expire
234 if ( isset( $alert['until'] ) && ( time() > strtotime( $alert['until'] ) ) ) {
235 continue;
236 }
237
238 $html .= Permalink_Manager_UI_Elements::get_alert_message( $alert['txt'], $alert['type'], true, $alert_id );
239 }
240 }
241 }
242
243 echo $html;
244 }
245
246 /**
247 * Display notices generated by Permalink Manager tools
248 */
249 function display_plugin_notices() {
250 global $permalink_manager_before_sections_html;
251
252 echo $permalink_manager_before_sections_html;
253 }
254
255 /**
256 * Get the list of all duplicated redirects and custom permalinks
257 *
258 * @param bool $include_custom_uris
259 *
260 * @return array
261 */
262 public static function get_all_duplicates( $include_custom_uris = true ) {
263 global $permalink_manager_uris, $permalink_manager_redirects;
264
265 // Make sure that both variables are arrays
266 $all_uris = ( $include_custom_uris && is_array( $permalink_manager_uris ) ) ? $permalink_manager_uris : array();
267 $permalink_manager_redirects = ( is_array( $permalink_manager_redirects ) ) ? $permalink_manager_redirects : array();
268
269 // Convert redirects list, so it can be merged with $permalink_manager_uris
270 foreach ( $permalink_manager_redirects as $element_id => $redirects ) {
271 if ( is_array( $redirects ) ) {
272 foreach ( $redirects as $index => $uri ) {
273 $all_uris["redirect-{$index}_{$element_id}"] = $uri;
274 }
275 }
276 }
277
278 // Count duplicates
279 $duplicates_groups = array();
280 $duplicates_list = array_count_values( $all_uris );
281 $duplicates_list = array_filter( $duplicates_list, function ( $x ) {
282 return $x >= 2;
283 } );
284
285 // Assign keys to duplicates (group them)
286 if ( count( $duplicates_list ) > 0 ) {
287 foreach ( $duplicates_list as $duplicated_uri => $count ) {
288 $duplicated_ids = array_keys( $all_uris, $duplicated_uri );
289
290 // Ignore duplicates in different langauges
291 if ( self::is_uri_duplicated( $duplicated_uri, $duplicated_ids[0], $duplicated_ids ) ) {
292 $duplicates_groups[ $duplicated_uri ] = $duplicated_ids;
293 }
294 }
295 }
296
297 return $duplicates_groups;
298 }
299
300 /**
301 * Check if a single URI is duplicated
302 *
303 * @param string $uri
304 * @param int $element_id
305 * @param array $duplicated_ids
306 *
307 * @return bool
308 */
309 public static function is_uri_duplicated( $uri, $element_id, $duplicated_ids = array() ) {
310 global $permalink_manager_uris;
311
312 if ( empty( $uri ) || empty( $element_id ) || empty( $permalink_manager_uris ) ) {
313 return false;
314 }
315
316 $uri = trim( trim( sanitize_text_field( $uri ) ), "/" );
317 $element_id = sanitize_text_field( $element_id );
318
319 // Keep the URIs in a separate array just here
320 if ( ! empty( $duplicated_ids ) ) {
321 $all_duplicates = $duplicated_ids;
322 } else if ( in_array( $uri, $permalink_manager_uris ) ) {
323 $all_duplicates = array_keys( $permalink_manager_uris, $uri );
324 }
325
326 if ( ! empty( $all_duplicates ) ) {
327 // Get the language code of current element
328 $this_uri_lang = apply_filters( 'permalink_manager_get_language_code', '', $element_id );
329
330 foreach ( $all_duplicates as $key => $duplicated_id ) {
331 // Ignore custom redirects
332 if ( strpos( $key, 'redirect-' ) !== false ) {
333 unset( $all_duplicates[ $key ] );
334 continue;
335 }
336
337 if ( $this_uri_lang ) {
338 $duplicated_uri_lang = apply_filters( 'permalink_manager_get_language_code', '', $duplicated_id );
339 }
340
341 // Ignore the URI for requested element and other elements in other languages to prevent the false alert
342 if ( ( ! empty( $duplicated_uri_lang ) && $duplicated_uri_lang !== $this_uri_lang ) || $element_id == $duplicated_id ) {
343 unset( $all_duplicates[ $key ] );
344 }
345 }
346
347 return ( count( $all_duplicates ) > 0 ) ? true : false;
348 } else {
349 return false;
350 }
351 }
352
353 /**
354 * Allow to use custom permalinks in search queries in Bulk URI Editor
355 *
356 * @param string $search_query
357 * @param string $content_type
358 *
359 * @return array
360 */
361 public static function search_uri( $search_query, $content_type = null ) {
362 global $permalink_manager_uris;
363
364 $found = array();
365 $search_query = preg_quote( $search_query, '/' );
366
367 foreach ( $permalink_manager_uris as $id => $uri ) {
368 if ( preg_match( "/\b$search_query\b/i", $uri ) ) {
369 if ( $content_type && $content_type == 'taxonomies' && ( strpos( $id, 'tax-' ) !== false ) ) {
370 $found[] = (int) abs( filter_var( $id, FILTER_SANITIZE_NUMBER_INT ) );
371 } else if ( $content_type && $content_type == 'posts' && is_numeric( $id ) ) {
372 $found[] = (int) filter_var( $id, FILTER_SANITIZE_NUMBER_INT );
373 } else {
374 $found[] = $id;
375 }
376 }
377 }
378
379 return $found;
380 }
381
382 /**
383 * Check if Permalink Manager Pro is active
384 *
385 * @return bool
386 */
387 public static function is_pro_active() {
388 if ( defined( 'PERMALINK_MANAGER_PRO' ) && class_exists( 'Permalink_Manager_Pro_Functions' ) ) {
389 // Check if license is active
390 $exp_date = Permalink_Manager_Pro_Functions::get_expiration_date( true );
391
392 $is_pro = ( $exp_date > 2 ) ? false : true;
393 } else {
394 $is_pro = false;
395 }
396
397 return $is_pro;
398 }
399 }
400