PluginProbe ʕ •ᴥ•ʔ
Permalink Manager Lite / 2.4.4.3
Permalink Manager Lite v2.4.4.3
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 1 year ago permalink-manager-admin-functions.php 1 year ago permalink-manager-core-functions.php 1 year ago permalink-manager-debug.php 1 year ago permalink-manager-gutenberg.php 1 year ago permalink-manager-helper-functions.php 1 year ago permalink-manager-uri-functions-post.php 1 year ago permalink-manager-uri-functions.php 1 year ago
permalink-manager-admin-functions.php
322 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 ) && is_a( $object, 'WP_Post' ) ) {
49 $new_url = get_permalink( $object->ID );
50 } else if ( ! empty( $object->taxonomy ) && is_a( $object, 'WP_Term' ) ) {
51 $new_url = get_term_link( $object, $object->taxonomy );
52 }
53 $permalink_manager_ignore_permalink_filters = false;
54
55 if ( ! empty( $new_url ) ) {
56 // 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
57 $new_url = filter_var( $new_url, FILTER_SANITIZE_URL );
58 $customize_url = preg_replace( '/url=([^&]+)/', "url={$new_url}", $customize->href );
59
60 $wp_admin_bar->add_node( array(
61 'id' => 'customize',
62 'href' => $customize_url,
63 ) );
64 }
65 }
66
67 /**
68 * Get current section of Permalink Manager admin panel
69 */
70 public function get_current_section() {
71 global $active_section, $active_subsection, $current_admin_tax;
72
73 // 1. Get current section
74 if ( isset( $_GET['page'] ) && $_GET['page'] == PERMALINK_MANAGER_PLUGIN_SLUG ) {
75 if ( isset( $_POST['section'] ) ) {
76 $this->active_section = sanitize_title_with_dashes( $_POST['section'] );
77 } else if ( isset( $_GET['section'] ) ) {
78 $this->active_section = sanitize_title_with_dashes( $_GET['section'] );
79 } else {
80 $sections_names = array_keys( $this->sections );
81 $this->active_section = $sections_names[0];
82 }
83 }
84
85 // 2. Get current subsection
86 if ( $this->active_section && isset( $this->sections[ $this->active_section ]['subsections'] ) ) {
87 if ( isset( $_POST['subsection'] ) ) {
88 $this->active_subsection = sanitize_title_with_dashes( $_POST['subsection'] );
89 } else if ( isset( $_GET['subsection'] ) ) {
90 $this->active_subsection = sanitize_title_with_dashes( $_GET['subsection'] );
91 } else {
92 $subsections_names = array_keys( $this->sections[ $this->active_section ]['subsections'] );
93 $this->active_subsection = $subsections_names[0];
94 }
95 }
96
97 // 3. Check if current admin page is related to taxonomies
98 if ( ! empty( $this->active_subsection ) && substr( $this->active_subsection, 0, 4 ) == 'tax_' ) {
99 $current_admin_tax = substr( $this->active_subsection, 4, strlen( $this->active_subsection ) );
100 } else {
101 $current_admin_tax = false;
102 }
103
104 // Set globals
105 $active_section = $this->active_section;
106 $active_subsection = $this->active_subsection;
107 }
108
109 /**
110 * Add "Tools -> Permalink Manager" to the admin sidebar menu
111 */
112 public function add_menu_page() {
113 $this->menu_name = add_management_page( __( 'Permalink Manager', 'permalink-manager' ), __( 'Permalink Manager', 'permalink-manager' ), 'manage_options', PERMALINK_MANAGER_PLUGIN_SLUG, array( $this, 'display_section' ) );
114
115 add_action( 'admin_init', array( $this, 'enqueue_styles' ) );
116 add_action( 'admin_init', array( $this, 'enqueue_scripts' ) );
117 }
118
119 /**
120 * Display the plugin sections
121 */
122 public function display_section() {
123 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
124 echo Permalink_Manager_UI_Elements::get_plugin_sections_html( $this->sections, $this->active_section, $this->active_subsection );
125 }
126
127 /**
128 * Register the CSS files for the plugin's dashboard
129 */
130 public function enqueue_styles() {
131 wp_enqueue_style( 'permalink-manager-plugins', PERMALINK_MANAGER_URL . '/out/permalink-manager-plugins.css', array(), PERMALINK_MANAGER_VERSION );
132 wp_enqueue_style( 'permalink-manager', PERMALINK_MANAGER_URL . '/out/permalink-manager-admin.css', array( 'permalink-manager-plugins' ), PERMALINK_MANAGER_VERSION );
133 }
134
135 /**
136 * Register the JavaScript files for the plugin's dashboard.
137 */
138 public function enqueue_scripts() {
139 wp_enqueue_script( 'permalink-manager-plugins', PERMALINK_MANAGER_URL . '/out/permalink-manager-plugins.js', array( 'jquery', ), PERMALINK_MANAGER_VERSION );
140 wp_enqueue_script( 'permalink-manager', PERMALINK_MANAGER_URL . '/out/permalink-manager-admin.js', array( 'jquery', 'permalink-manager-plugins' ), PERMALINK_MANAGER_VERSION );
141
142 wp_localize_script( 'permalink-manager', 'permalink_manager', array(
143 'ajax_url' => admin_url( 'admin-ajax.php' ),
144 'url' => PERMALINK_MANAGER_URL,
145 'confirm' => __( 'Are you sure? This action cannot be undone!', 'permalink-manager' ),
146 'spinners' => admin_url( 'images' )
147 ) );
148
149 }
150
151 /**
152 * Get the URL of the plugin's dashboard
153 *
154 * @param string $append
155 *
156 * @return string
157 */
158 public static function get_admin_url( $append = '' ) {
159 //return menu_page_url(PERMALINK_MANAGER_PLUGIN_SLUG, false) . $append;
160 $admin_page = sprintf( "tools.php?page=%s", PERMALINK_MANAGER_PLUGIN_SLUG . $append );
161
162 return admin_url( $admin_page );
163 }
164
165 /**
166 * Add shortcut links for Permalink Manager on "Plugins" page
167 *
168 * @param array $links
169 *
170 * @return array
171 */
172 public function plugins_page_links( $links ) {
173 $new_links = array(
174 sprintf( '<a href="%s">%s</a>', $this->get_admin_url(), __( 'URI Editor', 'permalink-manager' ) ),
175 sprintf( '<a href="%s">%s</a>', $this->get_admin_url( '&section=settings' ), __( 'Settings', 'permalink-manager' ) ),
176 );
177
178 return array_merge( $links, $new_links );
179 }
180
181 /**
182 * Add shortcut meta links for Permalink Manager on "Plugins" page
183 *
184 * @param array $links
185 * @param string $file
186 *
187 * @return array
188 */
189 public function plugins_page_meta( $links, $file ) {
190 if ( $file == PERMALINK_MANAGER_BASENAME ) {
191 $new_links = array(
192 'doc' => sprintf( '<a href="%s?utm_source=plugin_admin_page" target="_blank">%s</a>', 'https://permalinkmanager.pro/docs/', __( 'Documentation', 'permalink-manager' ) )
193 );
194
195 if ( ! defined( 'PERMALINK_MANAGER_PRO' ) ) {
196 $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' ) );
197 }
198
199 $links = array_merge( $links, $new_links );
200 }
201
202 return $links;
203 }
204
205 /**
206 * Check if URI Editor should be displayed for current user
207 *
208 * @return bool
209 */
210 public static function current_user_can_edit_uris() {
211 global $permalink_manager_options;
212
213 $edit_uris_cap = ( ! empty( $permalink_manager_options['general']['edit_uris_cap'] ) ) ? $permalink_manager_options['general']['edit_uris_cap'] : 'publish_posts';
214
215 return current_user_can( $edit_uris_cap );
216 }
217
218 /**
219 * Display global notices (throughout wp-admin dashboard)
220 */
221 function display_global_notices() {
222 global $permalink_manager_alerts, $active_section;
223
224 $html = "";
225 if ( ! empty( $permalink_manager_alerts ) && is_array( $permalink_manager_alerts ) ) {
226 foreach ( $permalink_manager_alerts as $alert_id => $alert ) {
227 $dismissed_transient_name = sprintf( 'permalink-manager-notice_%s', sanitize_title( $alert_id ) );
228 $dismissed = get_transient( $dismissed_transient_name );
229
230 // Check if alert was dismissed
231 if ( empty( $dismissed ) ) {
232 // Display the notice only on the plugin pages
233 if ( empty( $active_section ) && ! empty( $alert['plugin_only'] ) ) {
234 continue;
235 }
236
237 // Check if the notice did not expire
238 if ( isset( $alert['until'] ) && ( time() > strtotime( $alert['until'] ) ) ) {
239 continue;
240 }
241
242 $html .= Permalink_Manager_UI_Elements::get_alert_message( $alert['txt'], $alert['type'], true, $alert_id );
243 }
244 }
245 }
246
247 echo wp_kses_post( $html );
248 }
249
250 /**
251 * Display notices generated by Permalink Manager tools
252 */
253 function display_plugin_notices() {
254 global $permalink_manager_before_sections_html;
255
256 echo wp_kses_post( $permalink_manager_before_sections_html );
257 }
258
259 /**
260 * Get the list of all duplicated redirects and custom permalinks
261 *
262 * @param bool $include_custom_uris
263 *
264 * @return array
265 */
266 public static function get_all_duplicates( $include_custom_uris = true ) {
267 global $permalink_manager_redirects;
268
269 // Make sure that both variables are arrays
270 $all_uris = ( $include_custom_uris ) ? Permalink_Manager_URI_Functions::get_all_uris() : array();
271 $permalink_manager_redirects = ( is_array( $permalink_manager_redirects ) ) ? $permalink_manager_redirects : array();
272
273 // Convert redirects list, so it can be merged with custom permalinks array
274 foreach ( $permalink_manager_redirects as $element_id => $redirects ) {
275 if ( is_array( $redirects ) ) {
276 foreach ( $redirects as $index => $uri ) {
277 $all_uris["redirect-{$index}_{$element_id}"] = $uri;
278 }
279 }
280 }
281
282 // Count duplicates
283 $duplicates_groups = array();
284 $duplicates_list = array_count_values( $all_uris );
285 $duplicates_list = array_filter( $duplicates_list, function ( $x ) {
286 return $x >= 2;
287 } );
288
289 // Assign keys to duplicates (group them)
290 if ( count( $duplicates_list ) > 0 ) {
291 foreach ( $duplicates_list as $duplicated_uri => $count ) {
292 $duplicated_ids = array_keys( $all_uris, $duplicated_uri );
293
294 // Ignore duplicates in different langauges
295 if ( Permalink_Manager_URI_Functions::is_uri_duplicated( $duplicated_uri, $duplicated_ids[0], $duplicated_ids ) ) {
296 $duplicates_groups[ $duplicated_uri ] = $duplicated_ids;
297 }
298 }
299 }
300
301 return $duplicates_groups;
302 }
303
304 /**
305 * Check if Permalink Manager Pro is active
306 *
307 * @return bool
308 */
309 public static function is_pro_active() {
310 if ( defined( 'PERMALINK_MANAGER_PRO' ) && class_exists( 'Permalink_Manager_Pro_Functions' ) ) {
311 // Check if license is active
312 $exp_date = Permalink_Manager_Pro_Functions::get_expiration_date( true );
313
314 $is_pro = ( $exp_date > 2 ) ? false : true;
315 } else {
316 $is_pro = false;
317 }
318
319 return $is_pro;
320 }
321 }
322