PluginProbe ʕ •ᴥ•ʔ
Permalink Manager Lite / 2.6.0
Permalink Manager Lite v2.6.0
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 weeks ago permalink-manager-admin-functions.php 2 weeks ago permalink-manager-core-functions.php 2 weeks ago permalink-manager-debug.php 2 weeks ago permalink-manager-gutenberg.php 2 weeks ago permalink-manager-helper-functions.php 2 weeks ago permalink-manager-permastructures-functions.php 2 weeks ago permalink-manager-uri-functions-post.php 2 weeks ago permalink-manager-uri-functions.php 2 weeks ago
permalink-manager-admin-functions.php
370 lines
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit;
5 }
6
7 /**
8 * Additional functions related to WordPress Admin Dashboard UI
9 */
10 class Permalink_Manager_Admin_Functions {
11
12 public $sections, $active_section, $active_subsection;
13
14 public function __construct() {
15 add_action( 'admin_menu', array( $this, 'add_menu_page' ) );
16 add_action( 'admin_init', array( $this, 'init' ) );
17 add_action( 'admin_bar_menu', array( $this, 'fix_customize_url' ), 41 );
18
19 add_action( 'admin_notices', array( $this, 'display_plugin_notices' ) );
20 add_action( 'admin_notices', array( $this, 'display_global_notices' ) );
21 }
22
23 /**
24 * Hooks that should be triggered with "admin_init"
25 */
26 public function init() {
27 // Additional links in "Plugins" page
28 add_filter( "plugin_action_links_" . PERMALINK_MANAGER_BASENAME, array( $this, "plugins_page_links" ) );
29 add_filter( "plugin_row_meta", array( $this, "plugins_page_meta" ), 10, 2 );
30
31 // Detect current section
32 $this->sections = apply_filters( 'permalink_manager_sections', array() );
33 $this->get_current_section();
34 }
35
36 /**
37 * Use the native URL for "Customize" button in the admin bar
38 *
39 * @param WP_Admin_Bar $wp_admin_bar
40 */
41 public function fix_customize_url( $wp_admin_bar ) {
42 global $permalink_manager_ignore_permalink_filters;
43
44 $object = get_queried_object();
45 $customize = $wp_admin_bar->get_node( 'customize' );
46
47 if ( empty( $customize->href ) ) {
48 return;
49 }
50
51 $permalink_manager_ignore_permalink_filters = true;
52 if ( ! empty( $object->ID ) && is_a( $object, 'WP_Post' ) ) {
53 $new_url = get_permalink( $object->ID );
54 } else if ( ! empty( $object->taxonomy ) && is_a( $object, 'WP_Term' ) ) {
55 $new_url = get_term_link( $object, $object->taxonomy );
56 }
57 $permalink_manager_ignore_permalink_filters = false;
58
59 if ( ! empty( $new_url ) ) {
60 // The original permalink should be already encoded via "utf8_uri_encode()" in "sanitize_title_with_dashes()" function, so there is no need to encode them once again
61 $new_url = filter_var( $new_url, FILTER_SANITIZE_URL );
62 $customize_url = preg_replace( '/url=([^&]+)/', "url={$new_url}", $customize->href );
63
64 $wp_admin_bar->add_node( array(
65 'id' => 'customize',
66 'href' => $customize_url,
67 ) );
68 }
69 }
70
71 /**
72 * Get current section of Permalink Manager admin panel
73 */
74 public function get_current_section() {
75 global $permalink_manager_active_section, $permalink_manager_active_subsection, $permalink_manager_admin_tax;
76
77 // 1. Get current section
78 // phpcs:disable WordPress.Security.NonceVerification -- Read-only admin section routing; no data mutated.
79 if ( isset( $_GET['page'] ) && $_GET['page'] == PERMALINK_MANAGER_PLUGIN_SLUG ) {
80 if ( isset( $_POST['section'] ) ) {
81 $this->active_section = sanitize_title_with_dashes( wp_unslash( $_POST['section'] ) );
82 } else if ( isset( $_GET['section'] ) ) {
83 $this->active_section = sanitize_title_with_dashes( wp_unslash( $_GET['section'] ) );
84 } else {
85 $sections_names = array_keys( $this->sections );
86 $this->active_section = $sections_names[0];
87 }
88 }
89
90 // 2. Get current subsection
91 if ( $this->active_section && isset( $this->sections[ $this->active_section ]['subsections'] ) ) {
92 if ( isset( $_POST['subsection'] ) ) {
93 $this->active_subsection = sanitize_title_with_dashes( wp_unslash( $_POST['subsection'] ) );
94 } else if ( isset( $_GET['subsection'] ) ) {
95 $this->active_subsection = sanitize_title_with_dashes( wp_unslash( $_GET['subsection'] ) );
96 } else {
97 $subsections_names = array_keys( $this->sections[ $this->active_section ]['subsections'] );
98 $this->active_subsection = $subsections_names[0];
99 }
100 }
101 // phpcs:enable WordPress.Security.NonceVerification
102
103 // 3. Check if current admin page is related to taxonomies
104 if ( ! empty( $this->active_subsection ) && substr( $this->active_subsection, 0, 4 ) == 'tax_' ) {
105 $permalink_manager_admin_tax = substr( $this->active_subsection, 4, strlen( $this->active_subsection ) );
106 } else {
107 $permalink_manager_admin_tax = false;
108 }
109
110 // Set globals
111 $permalink_manager_active_section = $this->active_section;
112 $permalink_manager_active_subsection = $this->active_subsection;
113 }
114
115 /**
116 * Add "Tools -> Permalink Manager" to the admin sidebar menu
117 */
118 public function add_menu_page() {
119 add_management_page( __( 'Permalink Manager', 'permalink-manager' ), __( 'Permalink Manager', 'permalink-manager' ), 'manage_options', PERMALINK_MANAGER_PLUGIN_SLUG, array( $this, 'display_section' ) );
120
121 add_action( 'admin_init', array( $this, 'enqueue_cssjs' ) );
122 }
123
124 /**
125 * Display the plugin sections
126 */
127 public function display_section() {
128 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
129 echo Permalink_Manager_UI_Elements::get_plugin_sections_html( $this->sections, $this->active_section, $this->active_subsection );
130 }
131
132 /**
133 * Register the CSS & JS files for the plugin's dashboard
134 */
135 public function enqueue_cssjs() {
136 wp_enqueue_style( 'permalink-manager-plugins', PERMALINK_MANAGER_URL . '/out/permalink-manager-plugins.css', array(), PERMALINK_MANAGER_VERSION );
137 wp_enqueue_style( 'permalink-manager', PERMALINK_MANAGER_URL . '/out/permalink-manager-admin.css', array( 'permalink-manager-plugins' ), PERMALINK_MANAGER_VERSION );
138
139 wp_enqueue_script( 'permalink-manager-plugins', PERMALINK_MANAGER_URL . '/out/permalink-manager-plugins.js', array( 'jquery', ), PERMALINK_MANAGER_VERSION, array( 'in_footer' => false ) );
140 wp_enqueue_script( 'permalink-manager', PERMALINK_MANAGER_URL . '/out/permalink-manager-admin.js', array( 'jquery', 'permalink-manager-plugins' ), PERMALINK_MANAGER_VERSION, array( 'in_footer' => false ) );
141
142 // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only check to decide whether to enqueue Thickbox assets; no data mutated.
143 if ( isset( $_GET['section'] ) && $_GET['section'] === 'permastructs' ) {
144 wp_enqueue_script( 'thickbox' );
145 wp_enqueue_style( 'thickbox' );
146 }
147
148 wp_localize_script( 'permalink-manager', 'permalink_manager', array(
149 'ajax_url' => admin_url( 'admin-ajax.php' ),
150 'url' => PERMALINK_MANAGER_URL,
151 'confirm' => __( 'Are you sure? This action cannot be undone!', 'permalink-manager' ),
152 'spinners' => admin_url( 'images' )
153 ) );
154
155 }
156
157 /**
158 * Get the URL of the plugin's dashboard
159 *
160 * @param string $append
161 *
162 * @return string
163 */
164 public static function get_admin_url( $append = '' ) {
165 //return menu_page_url(PERMALINK_MANAGER_PLUGIN_SLUG, false) . $append;
166 $admin_page = sprintf( "tools.php?page=%s", PERMALINK_MANAGER_PLUGIN_SLUG . $append );
167
168 return admin_url( $admin_page );
169 }
170
171 /**
172 * Add shortcut links for Permalink Manager on "Plugins" page
173 *
174 * @param array $links
175 *
176 * @return array
177 */
178 public function plugins_page_links( $links ) {
179 $new_links = array(
180 sprintf( '<a href="%s">%s</a>', $this->get_admin_url(), __( 'URI Editor', 'permalink-manager' ) ),
181 sprintf( '<a href="%s">%s</a>', $this->get_admin_url( '&section=settings' ), __( 'Settings', 'permalink-manager' ) ),
182 );
183
184 return array_merge( $links, $new_links );
185 }
186
187 /**
188 * Add shortcut meta links for Permalink Manager on "Plugins" page
189 *
190 * @param array $links
191 * @param string $file
192 *
193 * @return array
194 */
195 public function plugins_page_meta( $links, $file ) {
196 if ( $file == PERMALINK_MANAGER_BASENAME ) {
197 $new_links = array(
198 'doc' => sprintf( '<a href="%s?utm_source=plugin_admin_page" target="_blank">%s</a>', 'https://permalinkmanager.pro/docs/', __( 'Documentation', 'permalink-manager' ) )
199 );
200
201 if ( ! defined( 'PERMALINK_MANAGER_PRO' ) ) {
202 $website_url = self::get_plugin_website_url( 'features' );
203 $new_links['upgrade'] = sprintf( '<a href="%s" target="_blank"><strong>%s</strong></a>', $website_url, __( 'Buy Permalink Manager Pro', 'permalink-manager' ) );
204 }
205
206 $links = array_merge( $links, $new_links );
207 }
208
209 return $links;
210 }
211
212 /**
213 * Check if URI Editor should be displayed for current user
214 *
215 * @return bool
216 */
217 public static function current_user_can_edit_uris() {
218 global $permalink_manager_options;
219
220 $edit_uris_cap = ( ! empty( $permalink_manager_options['general']['edit_uris_cap'] ) ) ? $permalink_manager_options['general']['edit_uris_cap'] : 'publish_posts';
221
222 return current_user_can( $edit_uris_cap );
223 }
224
225 /**
226 * Display global notices (throughout wp-admin dashboard)
227 */
228 function display_global_notices() {
229 global $permalink_manager_alerts, $permalink_manager_active_section;
230
231 $html = "";
232 if ( ! empty( $permalink_manager_alerts ) && is_array( $permalink_manager_alerts ) ) {
233 foreach ( $permalink_manager_alerts as $alert_id => $alert ) {
234 $dismissed_transient_name = sprintf( 'permalink-manager-notice_%s', sanitize_title( $alert_id ) );
235 $dismissed = get_transient( $dismissed_transient_name );
236
237 // Check if alert was dismissed
238 if ( empty( $dismissed ) ) {
239 // Hide notice in Permalink Manager Pro
240 if ( defined( 'PERMALINK_MANAGER_PRO' ) && ( ! empty( $alert['show'] ) ) && $alert['show'] == 'pro_hide' ) {
241 continue;
242 }
243
244 // Display the notice only on the plugin pages
245 if ( empty( $permalink_manager_active_section ) && ! empty( $alert['plugin_only'] ) ) {
246 continue;
247 }
248
249 // Check if the notice did not expire
250 if ( isset( $alert['until'] ) && ( time() > strtotime( $alert['until'] ) ) ) {
251 continue;
252 }
253
254 $html .= Permalink_Manager_UI_Elements::get_alert_message( $alert['txt'], $alert['type'], true, $alert_id );
255 }
256 }
257 }
258
259 echo wp_kses_post( $html );
260 }
261
262 /**
263 * Display notices generated by Permalink Manager tools
264 */
265 function display_plugin_notices() {
266 global $permalink_manager_before_sections_html;
267
268 echo wp_kses_post( $permalink_manager_before_sections_html );
269 }
270
271 /**
272 * Helper function to generate plugin website URLs.
273 *
274 * @param string $path Optional. The path to append to the base URL (e.g., 'features/').
275 * @param array $query_args Optional. Additional query arguments to append.
276 *
277 * @return string The fully constructed URL.
278 */
279 public static function get_plugin_website_url( $path = '', $query_args = array() ) {
280 // Ensure the base URL doesn't have a trailing slash
281 $url = rtrim( PERMALINK_MANAGER_WEBSITE, '/' );
282
283 // Append the path if provided, ensuring exactly one slash separates them
284 if ( ! empty( $path ) ) {
285 $url .= '/' . trim( $path, '/' ) . '/';
286 }
287
288 // Set default query arguments that should appear on all links
289 $defaults = array(
290 'utm_source' => 'plugin',
291 );
292
293 // Merge defaults with any custom arguments passed to the function
294 $final_args = array_merge( $defaults, $query_args );
295
296 // Build the query string securely
297 $query_string = http_build_query( $final_args );
298
299 // Append the query string to the URL
300 if ( ! empty( $query_string ) ) {
301 // Check if the base URL or path already contains a '?' to prevent malformed URLs
302 $url .= ( strpos( $url, '?' ) !== false ? '&' : '?' ) . $query_string;
303 }
304
305 return $url;
306 }
307
308 /**
309 * Get the list of all duplicated redirects and custom permalinks
310 *
311 * @param bool $include_custom_uris
312 *
313 * @return array
314 */
315 public static function get_all_duplicates( $include_custom_uris = true ) {
316 global $permalink_manager_redirects;
317
318 // Make sure that both variables are arrays
319 $all_uris = ( $include_custom_uris ) ? Permalink_Manager_URI_Functions::get_all_uris() : array();
320 $permalink_manager_redirects = ( is_array( $permalink_manager_redirects ) ) ? $permalink_manager_redirects : array();
321
322 // Convert redirects list, so it can be merged with custom permalinks array
323 foreach ( $permalink_manager_redirects as $element_id => $redirects ) {
324 if ( is_array( $redirects ) ) {
325 foreach ( $redirects as $index => $uri ) {
326 $all_uris["redirect-{$index}_{$element_id}"] = $uri;
327 }
328 }
329 }
330
331 // Count duplicates
332 $duplicates_groups = array();
333 $duplicates_list = array_count_values( $all_uris );
334 $duplicates_list = array_filter( $duplicates_list, function ( $x ) {
335 return $x >= 2;
336 } );
337
338 // Assign keys to duplicates (group them)
339 if ( count( $duplicates_list ) > 0 ) {
340 foreach ( $duplicates_list as $duplicated_uri => $count ) {
341 $duplicated_ids = array_keys( $all_uris, $duplicated_uri );
342
343 // Ignore duplicates in different langauges
344 if ( Permalink_Manager_URI_Functions::is_uri_duplicated( $duplicated_uri, $duplicated_ids[0], $duplicated_ids ) ) {
345 $duplicates_groups[ $duplicated_uri ] = $duplicated_ids;
346 }
347 }
348 }
349
350 return $duplicates_groups;
351 }
352
353 /**
354 * Check if Permalink Manager Pro is active
355 *
356 * @return bool
357 */
358 public static function is_pro_active() {
359 if ( defined( 'PERMALINK_MANAGER_PRO' ) && class_exists( 'Permalink_Manager_Pro_License' ) ) {
360 // Check if license is active
361 $exp_date = Permalink_Manager_Pro_License::get_expiration_date( true );
362
363 $is_pro = ( $exp_date > 2 ) ? false : true;
364 } else {
365 $is_pro = false;
366 }
367
368 return $is_pro;
369 }
370 }