PluginProbe
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance / 4.7.0
WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance v4.7.0
4.7.0 4.6.1 4.6.0 4.5.5 4.5.4 4.5.3 4.5.2 3.2.20 3.2.21 3.2.22 3.2.3 3.2.5 3.2.6 3.2.7 3.2.9 3.3.0 3.3.1 3.3.2 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.7.0 3.7.1 All 111 releases
wp-optimize / compatibility / class-wpo-custom-permalink-compatibility.php

class-wpo-custom-permalink-compatibility.php in WP-Optimize – Cache, Compress images, Minify & Clean database to boost page speed & performance 4.7.0, at compatibility/class-wpo-custom-permalink-compatibility.php

139 lines 3.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if (!defined('ABSPATH')) die('No direct access allowed');
3
4 /**
5 * Adds compatibility for Custom Permalink plugin.
6 */
7 class WPO_Custom_Permalink_Compatibility {
8
9 /**
10 * Constructor.
11 */
12 private function __construct() {
13 add_filter('custom_permalink_before_saving', array($this, 'custom_permalink_before_saving'));
14 add_filter('pre_update_option_custom_permalink_table', array($this, 'pre_update_option_custom_permalink_table'), 10, 2);
15 add_action('update_option_permalink_structure', array($this, 'update_option_permalink_structure'), 10, 2);
16 }
17
18 /**
19 * Returns singleton instance.
20 *
21 * @return self
22 */
23 public static function instance() {
24 static $instance = null;
25 if (null === $instance) {
26 $instance = new self();
27 }
28
29 return $instance;
30 }
31
32 /**
33 * Modifies post's custom permalink based on site's permalink structure
34 *
35 * @param string $permalink
36 *
37 * @return string
38 */
39 public function custom_permalink_before_saving($permalink) {
40 return $this->fix_permalink($permalink);
41 }
42
43 /**
44 * Updates custom permalink table option based on site's permalink structure
45 *
46 * @param mixed $value
47 * @param mixed $old_value
48 *
49 * @return mixed
50 */
51 public function pre_update_option_custom_permalink_table($value, $old_value) {
52 if ($old_value === $value) return $value;
53
54 if (is_string($value)) {
55 return $this->fix_permalink($value);
56 }
57 $new_value = array();
58 foreach ($value as $key => $item) {
59 $new_key = $this->fix_permalink($key);
60 $new_value[$new_key] = $item;
61 }
62 return $new_value;
63 }
64
65 /**
66 * Decides whether permalink structure has trailing slash or not
67 *
68 * @return bool
69 */
70 private function does_permalink_have_trailingslash() {
71 $permalink_structure = get_option('permalink_structure', '');
72 return '/' === substr($permalink_structure, -1);
73 }
74
75 /**
76 * Adds/Removes trailing slash based on permalink structure
77 *
78 * @param string $permalink
79 *
80 * @return string
81 */
82 private function fix_permalink($permalink) {
83 if ($this->does_permalink_have_trailingslash()) {
84 $permalink = trailingslashit($permalink);
85 } else {
86 $permalink = untrailingslashit($permalink);
87 }
88 return $permalink;
89 }
90
91 /**
92 * Updates custom permalinks
93 *
94 * @param mixed $old_value
95 * @param mixed $new_value
96 *
97 * @return void
98 */
99 public function update_option_permalink_structure($old_value, $new_value) {
100 if ($old_value === $new_value) return;
101 $this->update_terms_custom_permalinks();
102 $this->update_posts_custom_permalinks();
103 }
104
105 /**
106 * Updates terms custom permalinks
107 *
108 * @return void
109 */
110 private function update_terms_custom_permalinks() {
111 $terms_custom_permalinks = get_option('custom_permalink_table');
112 if ($terms_custom_permalinks) {
113 $custom_permalink_table = $this->pre_update_option_custom_permalink_table($terms_custom_permalinks, array());
114 update_option('custom_permalink_table', $custom_permalink_table);
115 }
116 }
117
118 /**
119 * Updates post custom permalinks for all post types
120 *
121 * @return void
122 */
123 private function update_posts_custom_permalinks() {
124 $post_types = get_post_types(array('public' => true));
125 $args = array(
126 'post_type' => $post_types,
127 'posts_per_page' => -1,
128 );
129 $posts = get_posts($args);
130
131 foreach ($posts as $post) {
132 $permalink = get_post_meta($post->ID, 'custom_permalink', true);
133 if (empty($permalink)) continue;
134 $permalink = $this->fix_permalink($permalink);
135 update_post_meta($post->ID, 'custom_permalink', $permalink);
136 }
137 }
138 }
139