@@ -29,131 +29,131 @@ |
||
29 | 29 | * Exit if called directly. |
30 | 30 | */ |
31 | 31 | if ( ! defined( 'ABSPATH' ) ) { |
32 | - die; |
|
32 | + die; |
|
33 | 33 | } |
34 | 34 | |
35 | 35 | if ( ! class_exists( 'PAnD' ) ) { |
36 | 36 | |
37 | - /** |
|
38 | - * Class PAnD |
|
39 | - */ |
|
40 | - class PAnD { |
|
41 | - |
|
42 | - /** |
|
43 | - * Init hooks. |
|
44 | - */ |
|
45 | - public static function init() { |
|
46 | - add_action( 'admin_enqueue_scripts', array( __CLASS__, 'load_script' ) ); |
|
47 | - add_action( 'wp_ajax_dismiss_admin_notice', array( __CLASS__, 'dismiss_admin_notice' ) ); |
|
48 | - } |
|
49 | - |
|
50 | - /** |
|
51 | - * Enqueue javascript and variables. |
|
52 | - */ |
|
53 | - public static function load_script() { |
|
54 | - |
|
55 | - if ( is_customize_preview() ) { |
|
56 | - return; |
|
57 | - } |
|
58 | - |
|
59 | - wp_enqueue_script( |
|
60 | - 'dismissible-notices', |
|
61 | - plugins_url( 'dismiss-notice.js', __FILE__ ), |
|
62 | - array( 'jquery', 'common' ), |
|
63 | - false, |
|
64 | - true |
|
65 | - ); |
|
66 | - |
|
67 | - wp_localize_script( |
|
68 | - 'dismissible-notices', |
|
69 | - 'dismissible_notice', |
|
70 | - array( |
|
71 | - 'nonce' => wp_create_nonce( 'dismissible-notice' ), |
|
72 | - ) |
|
73 | - ); |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * Handles Ajax request to persist notices dismissal. |
|
78 | - * Uses check_ajax_referer to verify nonce. |
|
79 | - */ |
|
80 | - public static function dismiss_admin_notice() { |
|
81 | - $option_name = sanitize_text_field( $_POST['option_name'] ); |
|
82 | - $dismissible_length = sanitize_text_field( $_POST['dismissible_length'] ); |
|
83 | - |
|
84 | - if ( 'forever' != $dismissible_length ) { |
|
85 | - // If $dismissible_length is not an integer default to 1 |
|
86 | - $dismissible_length = ( 0 == absint( $dismissible_length ) ) ? 1 : $dismissible_length; |
|
87 | - $dismissible_length = strtotime( absint( $dismissible_length ) . ' days' ); |
|
88 | - } |
|
89 | - |
|
90 | - check_ajax_referer( 'dismissible-notice', 'nonce' ); |
|
91 | - self::set_admin_notice_cache( $option_name, $dismissible_length ); |
|
92 | - wp_die(); |
|
93 | - } |
|
94 | - |
|
95 | - /** |
|
96 | - * Is admin notice active? |
|
97 | - * |
|
98 | - * @param string $arg data-dismissible content of notice. |
|
99 | - * |
|
100 | - * @return bool |
|
101 | - */ |
|
102 | - public static function is_admin_notice_active( $arg ) { |
|
103 | - $array = explode( '-', $arg ); |
|
104 | - $length = array_pop( $array ); |
|
105 | - $option_name = implode( '-', $array ); |
|
106 | - $db_record = self::get_admin_notice_cache( $option_name ); |
|
107 | - if ( 'forever' == $db_record ) { |
|
108 | - return false; |
|
109 | - } elseif ( absint( $db_record ) >= time() ) { |
|
110 | - return false; |
|
111 | - } else { |
|
112 | - return true; |
|
113 | - } |
|
114 | - } |
|
115 | - |
|
116 | - /** |
|
117 | - * Returns admin notice cached timeout. |
|
118 | - * |
|
119 | - * @access public |
|
120 | - * |
|
121 | - * @param string|bool $id admin notice name or false. |
|
122 | - * |
|
123 | - * @return array|bool The timeout. False if expired. |
|
124 | - */ |
|
125 | - public static function get_admin_notice_cache( $id = false ) { |
|
126 | - if ( ! $id ) { |
|
127 | - return false; |
|
128 | - } |
|
129 | - $cache_key = 'pand-' . md5( $id ); |
|
130 | - $timeout = get_site_option( $cache_key ); |
|
131 | - $timeout = 'forever' === $timeout ? time() + 60 : $timeout; |
|
132 | - |
|
133 | - if ( empty( $timeout ) || time() > $timeout ) { |
|
134 | - return false; |
|
135 | - } |
|
136 | - |
|
137 | - return $timeout; |
|
138 | - } |
|
139 | - |
|
140 | - /** |
|
141 | - * Sets admin notice timeout in site option. |
|
142 | - * |
|
143 | - * @access public |
|
144 | - * |
|
145 | - * @param string $id Data Identifier. |
|
146 | - * @param string|bool $timeout Timeout for admin notice. |
|
147 | - * |
|
148 | - * @return bool |
|
149 | - */ |
|
150 | - public static function set_admin_notice_cache( $id, $timeout ) { |
|
151 | - $cache_key = 'pand-' . md5( $id ); |
|
152 | - update_site_option( $cache_key, $timeout ); |
|
153 | - |
|
154 | - return true; |
|
155 | - } |
|
156 | - |
|
157 | - } |
|
37 | + /** |
|
38 | + * Class PAnD |
|
39 | + */ |
|
40 | + class PAnD { |
|
41 | + |
|
42 | + /** |
|
43 | + * Init hooks. |
|
44 | + */ |
|
45 | + public static function init() { |
|
46 | + add_action( 'admin_enqueue_scripts', array( __CLASS__, 'load_script' ) ); |
|
47 | + add_action( 'wp_ajax_dismiss_admin_notice', array( __CLASS__, 'dismiss_admin_notice' ) ); |
|
48 | + } |
|
49 | + |
|
50 | + /** |
|
51 | + * Enqueue javascript and variables. |
|
52 | + */ |
|
53 | + public static function load_script() { |
|
54 | + |
|
55 | + if ( is_customize_preview() ) { |
|
56 | + return; |
|
57 | + } |
|
58 | + |
|
59 | + wp_enqueue_script( |
|
60 | + 'dismissible-notices', |
|
61 | + plugins_url( 'dismiss-notice.js', __FILE__ ), |
|
62 | + array( 'jquery', 'common' ), |
|
63 | + false, |
|
64 | + true |
|
65 | + ); |
|
66 | + |
|
67 | + wp_localize_script( |
|
68 | + 'dismissible-notices', |
|
69 | + 'dismissible_notice', |
|
70 | + array( |
|
71 | + 'nonce' => wp_create_nonce( 'dismissible-notice' ), |
|
72 | + ) |
|
73 | + ); |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * Handles Ajax request to persist notices dismissal. |
|
78 | + * Uses check_ajax_referer to verify nonce. |
|
79 | + */ |
|
80 | + public static function dismiss_admin_notice() { |
|
81 | + $option_name = sanitize_text_field( $_POST['option_name'] ); |
|
82 | + $dismissible_length = sanitize_text_field( $_POST['dismissible_length'] ); |
|
83 | + |
|
84 | + if ( 'forever' != $dismissible_length ) { |
|
85 | + // If $dismissible_length is not an integer default to 1 |
|
86 | + $dismissible_length = ( 0 == absint( $dismissible_length ) ) ? 1 : $dismissible_length; |
|
87 | + $dismissible_length = strtotime( absint( $dismissible_length ) . ' days' ); |
|
88 | + } |
|
89 | + |
|
90 | + check_ajax_referer( 'dismissible-notice', 'nonce' ); |
|
91 | + self::set_admin_notice_cache( $option_name, $dismissible_length ); |
|
92 | + wp_die(); |
|
93 | + } |
|
94 | + |
|
95 | + /** |
|
96 | + * Is admin notice active? |
|
97 | + * |
|
98 | + * @param string $arg data-dismissible content of notice. |
|
99 | + * |
|
100 | + * @return bool |
|
101 | + */ |
|
102 | + public static function is_admin_notice_active( $arg ) { |
|
103 | + $array = explode( '-', $arg ); |
|
104 | + $length = array_pop( $array ); |
|
105 | + $option_name = implode( '-', $array ); |
|
106 | + $db_record = self::get_admin_notice_cache( $option_name ); |
|
107 | + if ( 'forever' == $db_record ) { |
|
108 | + return false; |
|
109 | + } elseif ( absint( $db_record ) >= time() ) { |
|
110 | + return false; |
|
111 | + } else { |
|
112 | + return true; |
|
113 | + } |
|
114 | + } |
|
115 | + |
|
116 | + /** |
|
117 | + * Returns admin notice cached timeout. |
|
118 | + * |
|
119 | + * @access public |
|
120 | + * |
|
121 | + * @param string|bool $id admin notice name or false. |
|
122 | + * |
|
123 | + * @return array|bool The timeout. False if expired. |
|
124 | + */ |
|
125 | + public static function get_admin_notice_cache( $id = false ) { |
|
126 | + if ( ! $id ) { |
|
127 | + return false; |
|
128 | + } |
|
129 | + $cache_key = 'pand-' . md5( $id ); |
|
130 | + $timeout = get_site_option( $cache_key ); |
|
131 | + $timeout = 'forever' === $timeout ? time() + 60 : $timeout; |
|
132 | + |
|
133 | + if ( empty( $timeout ) || time() > $timeout ) { |
|
134 | + return false; |
|
135 | + } |
|
136 | + |
|
137 | + return $timeout; |
|
138 | + } |
|
139 | + |
|
140 | + /** |
|
141 | + * Sets admin notice timeout in site option. |
|
142 | + * |
|
143 | + * @access public |
|
144 | + * |
|
145 | + * @param string $id Data Identifier. |
|
146 | + * @param string|bool $timeout Timeout for admin notice. |
|
147 | + * |
|
148 | + * @return bool |
|
149 | + */ |
|
150 | + public static function set_admin_notice_cache( $id, $timeout ) { |
|
151 | + $cache_key = 'pand-' . md5( $id ); |
|
152 | + update_site_option( $cache_key, $timeout ); |
|
153 | + |
|
154 | + return true; |
|
155 | + } |
|
156 | + |
|
157 | + } |
|
158 | 158 | |
159 | 159 | } |
@@ -28,11 +28,11 @@ discard block |
||
28 | 28 | /** |
29 | 29 | * Exit if called directly. |
30 | 30 | */ |
31 | -if ( ! defined( 'ABSPATH' ) ) { |
|
31 | +if (!defined('ABSPATH')) { |
|
32 | 32 | die; |
33 | 33 | } |
34 | 34 | |
35 | -if ( ! class_exists( 'PAnD' ) ) { |
|
35 | +if (!class_exists('PAnD')) { |
|
36 | 36 | |
37 | 37 | /** |
38 | 38 | * Class PAnD |
@@ -43,8 +43,8 @@ discard block |
||
43 | 43 | * Init hooks. |
44 | 44 | */ |
45 | 45 | public static function init() { |
46 | - add_action( 'admin_enqueue_scripts', array( __CLASS__, 'load_script' ) ); |
|
47 | - add_action( 'wp_ajax_dismiss_admin_notice', array( __CLASS__, 'dismiss_admin_notice' ) ); |
|
46 | + add_action('admin_enqueue_scripts', array(__CLASS__, 'load_script')); |
|
47 | + add_action('wp_ajax_dismiss_admin_notice', array(__CLASS__, 'dismiss_admin_notice')); |
|
48 | 48 | } |
49 | 49 | |
50 | 50 | /** |
@@ -52,14 +52,14 @@ discard block |
||
52 | 52 | */ |
53 | 53 | public static function load_script() { |
54 | 54 | |
55 | - if ( is_customize_preview() ) { |
|
55 | + if (is_customize_preview()) { |
|
56 | 56 | return; |
57 | 57 | } |
58 | 58 | |
59 | 59 | wp_enqueue_script( |
60 | 60 | 'dismissible-notices', |
61 | - plugins_url( 'dismiss-notice.js', __FILE__ ), |
|
62 | - array( 'jquery', 'common' ), |
|
61 | + plugins_url('dismiss-notice.js', __FILE__), |
|
62 | + array('jquery', 'common'), |
|
63 | 63 | false, |
64 | 64 | true |
65 | 65 | ); |
@@ -68,7 +68,7 @@ discard block |
||
68 | 68 | 'dismissible-notices', |
69 | 69 | 'dismissible_notice', |
70 | 70 | array( |
71 | - 'nonce' => wp_create_nonce( 'dismissible-notice' ), |
|
71 | + 'nonce' => wp_create_nonce('dismissible-notice'), |
|
72 | 72 | ) |
73 | 73 | ); |
74 | 74 | } |
@@ -78,17 +78,17 @@ discard block |
||
78 | 78 | * Uses check_ajax_referer to verify nonce. |
79 | 79 | */ |
80 | 80 | public static function dismiss_admin_notice() { |
81 | - $option_name = sanitize_text_field( $_POST['option_name'] ); |
|
82 | - $dismissible_length = sanitize_text_field( $_POST['dismissible_length'] ); |
|
81 | + $option_name = sanitize_text_field($_POST['option_name']); |
|
82 | + $dismissible_length = sanitize_text_field($_POST['dismissible_length']); |
|
83 | 83 | |
84 | - if ( 'forever' != $dismissible_length ) { |
|
84 | + if ('forever' != $dismissible_length) { |
|
85 | 85 | // If $dismissible_length is not an integer default to 1 |
86 | - $dismissible_length = ( 0 == absint( $dismissible_length ) ) ? 1 : $dismissible_length; |
|
87 | - $dismissible_length = strtotime( absint( $dismissible_length ) . ' days' ); |
|
86 | + $dismissible_length = (0 == absint($dismissible_length)) ? 1 : $dismissible_length; |
|
87 | + $dismissible_length = strtotime(absint($dismissible_length).' days'); |
|
88 | 88 | } |
89 | 89 | |
90 | - check_ajax_referer( 'dismissible-notice', 'nonce' ); |
|
91 | - self::set_admin_notice_cache( $option_name, $dismissible_length ); |
|
90 | + check_ajax_referer('dismissible-notice', 'nonce'); |
|
91 | + self::set_admin_notice_cache($option_name, $dismissible_length); |
|
92 | 92 | wp_die(); |
93 | 93 | } |
94 | 94 | |
@@ -99,14 +99,14 @@ discard block |
||
99 | 99 | * |
100 | 100 | * @return bool |
101 | 101 | */ |
102 | - public static function is_admin_notice_active( $arg ) { |
|
103 | - $array = explode( '-', $arg ); |
|
104 | - $length = array_pop( $array ); |
|
105 | - $option_name = implode( '-', $array ); |
|
106 | - $db_record = self::get_admin_notice_cache( $option_name ); |
|
107 | - if ( 'forever' == $db_record ) { |
|
102 | + public static function is_admin_notice_active($arg) { |
|
103 | + $array = explode('-', $arg); |
|
104 | + $length = array_pop($array); |
|
105 | + $option_name = implode('-', $array); |
|
106 | + $db_record = self::get_admin_notice_cache($option_name); |
|
107 | + if ('forever' == $db_record) { |
|
108 | 108 | return false; |
109 | - } elseif ( absint( $db_record ) >= time() ) { |
|
109 | + } elseif (absint($db_record) >= time()) { |
|
110 | 110 | return false; |
111 | 111 | } else { |
112 | 112 | return true; |
@@ -122,15 +122,15 @@ discard block |
||
122 | 122 | * |
123 | 123 | * @return array|bool The timeout. False if expired. |
124 | 124 | */ |
125 | - public static function get_admin_notice_cache( $id = false ) { |
|
126 | - if ( ! $id ) { |
|
125 | + public static function get_admin_notice_cache($id = false) { |
|
126 | + if (!$id) { |
|
127 | 127 | return false; |
128 | 128 | } |
129 | - $cache_key = 'pand-' . md5( $id ); |
|
130 | - $timeout = get_site_option( $cache_key ); |
|
129 | + $cache_key = 'pand-'.md5($id); |
|
130 | + $timeout = get_site_option($cache_key); |
|
131 | 131 | $timeout = 'forever' === $timeout ? time() + 60 : $timeout; |
132 | 132 | |
133 | - if ( empty( $timeout ) || time() > $timeout ) { |
|
133 | + if (empty($timeout) || time() > $timeout) { |
|
134 | 134 | return false; |
135 | 135 | } |
136 | 136 | |
@@ -147,9 +147,9 @@ discard block |
||
147 | 147 | * |
148 | 148 | * @return bool |
149 | 149 | */ |
150 | - public static function set_admin_notice_cache( $id, $timeout ) { |
|
151 | - $cache_key = 'pand-' . md5( $id ); |
|
152 | - update_site_option( $cache_key, $timeout ); |
|
150 | + public static function set_admin_notice_cache($id, $timeout) { |
|
151 | + $cache_key = 'pand-'.md5($id); |
|
152 | + update_site_option($cache_key, $timeout); |
|
153 | 153 | |
154 | 154 | return true; |
155 | 155 | } |
@@ -70,7 +70,7 @@ discard block |
||
70 | 70 | protected $inputLength = 0; |
71 | 71 | protected $lookAhead = null; |
72 | 72 | protected $output = ''; |
73 | - protected $lastByteOut = ''; |
|
73 | + protected $lastByteOut = ''; |
|
74 | 74 | protected $keptComment = ''; |
75 | 75 | |
76 | 76 | /** |
@@ -106,7 +106,7 @@ discard block |
||
106 | 106 | } |
107 | 107 | |
108 | 108 | $mbIntEnc = null; |
109 | - if (function_exists('mb_strlen') && ((int)ini_get('mbstring.func_overload') & 2)) { |
|
109 | + if (function_exists('mb_strlen') && ((int) ini_get('mbstring.func_overload') & 2)) { |
|
110 | 110 | $mbIntEnc = mb_internal_encoding(); |
111 | 111 | mb_internal_encoding('8bit'); |
112 | 112 | } |
@@ -128,7 +128,7 @@ discard block |
||
128 | 128 | && ($this->b === $this->lastByteOut)) { |
129 | 129 | // Don't delete this space. If we do, the addition/subtraction |
130 | 130 | // could be parsed as a post-increment |
131 | - } elseif (! $this->isAlphaNum($this->b)) { |
|
131 | + } elseif (!$this->isAlphaNum($this->b)) { |
|
132 | 132 | $command = self::ACTION_DELETE_A; |
133 | 133 | } |
134 | 134 | } elseif ($this->a === "\n") { |
@@ -139,10 +139,10 @@ discard block |
||
139 | 139 | // otherwise mb_strpos will give WARNING |
140 | 140 | } elseif ($this->b === null |
141 | 141 | || (false === strpos('{[(+-!~', $this->b) |
142 | - && ! $this->isAlphaNum($this->b))) { |
|
142 | + && !$this->isAlphaNum($this->b))) { |
|
143 | 143 | $command = self::ACTION_DELETE_A; |
144 | 144 | } |
145 | - } elseif (! $this->isAlphaNum($this->a)) { |
|
145 | + } elseif (!$this->isAlphaNum($this->a)) { |
|
146 | 146 | if ($this->b === ' ' |
147 | 147 | || ($this->b === "\n" |
148 | 148 | && (false === strpos('}])+-"\'', $this->a)))) { |
@@ -199,7 +199,7 @@ discard block |
||
199 | 199 | if ($this->a === "'" || $this->a === '"' || $this->a === '`') { // string/template literal |
200 | 200 | $delimiter = $this->a; |
201 | 201 | $str = $this->a; // in case needed for exception |
202 | - for(;;) { |
|
202 | + for (;;) { |
|
203 | 203 | $this->output .= $this->a; |
204 | 204 | $this->lastByteOut = $this->a; |
205 | 205 | |
@@ -229,13 +229,13 @@ discard block |
||
229 | 229 | case self::ACTION_DELETE_A_B: // 3 |
230 | 230 | $this->b = $this->next(); |
231 | 231 | if ($this->b === '/' && $this->isRegexpLiteral()) { |
232 | - $this->output .= $this->a . $this->b; |
|
232 | + $this->output .= $this->a.$this->b; |
|
233 | 233 | $pattern = '/'; // keep entire pattern in case we need to report it in the exception |
234 | - for(;;) { |
|
234 | + for (;;) { |
|
235 | 235 | $this->a = $this->get(); |
236 | 236 | $pattern .= $this->a; |
237 | 237 | if ($this->a === '[') { |
238 | - for(;;) { |
|
238 | + for (;;) { |
|
239 | 239 | $this->output .= $this->a; |
240 | 240 | $this->a = $this->get(); |
241 | 241 | $pattern .= $this->a; |
@@ -250,7 +250,7 @@ discard block |
||
250 | 250 | if ($this->isEOF($this->a)) { |
251 | 251 | throw new JSMin_UnterminatedRegExpException( |
252 | 252 | "JSMin: Unterminated set in RegExp at byte " |
253 | - . $this->inputIndex .": {$pattern}"); |
|
253 | + . $this->inputIndex.": {$pattern}"); |
|
254 | 254 | } |
255 | 255 | } |
256 | 256 | } |
@@ -295,7 +295,7 @@ discard block |
||
295 | 295 | |
296 | 296 | // if the "/" follows a keyword, it must be a regexp, otherwise it's best to assume division |
297 | 297 | |
298 | - $subject = $this->output . trim($this->a); |
|
298 | + $subject = $this->output.trim($this->a); |
|
299 | 299 | if (!preg_match('/(?:case|else|in|return|typeof)$/', $subject, $m)) { |
300 | 300 | // not a keyword |
301 | 301 | return false; |
@@ -406,7 +406,7 @@ discard block |
||
406 | 406 | { |
407 | 407 | $this->get(); |
408 | 408 | $comment = ''; |
409 | - for(;;) { |
|
409 | + for (;;) { |
|
410 | 410 | $get = $this->get(); |
411 | 411 | if ($get === '*') { |
412 | 412 | if ($this->peek() === '/') { // end of comment reached |
@@ -417,7 +417,7 @@ discard block |
||
417 | 417 | // don't prepend a newline if two comments right after one another |
418 | 418 | $this->keptComment = "\n"; |
419 | 419 | } |
420 | - $this->keptComment .= "/*!" . substr($comment, 1) . "*/\n"; |
|
420 | + $this->keptComment .= "/*!".substr($comment, 1)."*/\n"; |
|
421 | 421 | } else if (preg_match('/^@(?:cc_on|if|elif|else|end)\\b/', $comment)) { |
422 | 422 | // IE conditional |
423 | 423 | $this->keptComment .= "/*{$comment}*/"; |
@@ -3,7 +3,7 @@ discard block |
||
3 | 3 | * Handles version updates and should only be instantiated in autoptimize.php if/when needed. |
4 | 4 | */ |
5 | 5 | |
6 | -if ( ! defined( 'ABSPATH' ) ) { |
|
6 | +if (!defined('ABSPATH')) { |
|
7 | 7 | exit; |
8 | 8 | } |
9 | 9 | |
@@ -16,9 +16,9 @@ discard block |
||
16 | 16 | */ |
17 | 17 | protected $current_major_version = null; |
18 | 18 | |
19 | - public function __construct( $current_version ) |
|
19 | + public function __construct($current_version) |
|
20 | 20 | { |
21 | - $this->current_major_version = substr( $current_version, 0, 3 ); |
|
21 | + $this->current_major_version = substr($current_version, 0, 3); |
|
22 | 22 | } |
23 | 23 | |
24 | 24 | /** |
@@ -29,7 +29,7 @@ discard block |
||
29 | 29 | { |
30 | 30 | $major_update = false; |
31 | 31 | |
32 | - switch ( $this->current_major_version ) { |
|
32 | + switch ($this->current_major_version) { |
|
33 | 33 | case '1.6': |
34 | 34 | $this->upgrade_from_1_6(); |
35 | 35 | $major_update = true; |
@@ -47,14 +47,14 @@ discard block |
||
47 | 47 | $major_update = true; |
48 | 48 | // No break, intentionally, so all upgrades are ran during a single request... |
49 | 49 | case '2.4': |
50 | - if ( get_option( 'autoptimize_version', 'none' ) == '2.4.2' ) { |
|
50 | + if (get_option('autoptimize_version', 'none') == '2.4.2') { |
|
51 | 51 | $this->upgrade_from_2_4_2(); |
52 | 52 | } |
53 | 53 | $major_update = false; |
54 | 54 | // No break, intentionally, so all upgrades are ran during a single request... |
55 | 55 | } |
56 | 56 | |
57 | - if ( true === $major_update ) { |
|
57 | + if (true === $major_update) { |
|
58 | 58 | $this->on_major_version_update(); |
59 | 59 | } |
60 | 60 | } |
@@ -66,19 +66,19 @@ discard block |
||
66 | 66 | * |
67 | 67 | * @param string $target Target version to check against (ie., the currently running one). |
68 | 68 | */ |
69 | - public static function check_installed_and_update( $target ) |
|
69 | + public static function check_installed_and_update($target) |
|
70 | 70 | { |
71 | - $db_version = get_option( 'autoptimize_version', 'none' ); |
|
72 | - if ( $db_version !== $target ) { |
|
73 | - if ( 'none' === $db_version ) { |
|
74 | - add_action( 'admin_notices', 'autoptimizeMain::notice_installed' ); |
|
71 | + $db_version = get_option('autoptimize_version', 'none'); |
|
72 | + if ($db_version !== $target) { |
|
73 | + if ('none' === $db_version) { |
|
74 | + add_action('admin_notices', 'autoptimizeMain::notice_installed'); |
|
75 | 75 | } else { |
76 | - $updater = new self( $db_version ); |
|
76 | + $updater = new self($db_version); |
|
77 | 77 | $updater->run_needed_major_upgrades(); |
78 | 78 | } |
79 | 79 | |
80 | 80 | // Versions differed, upgrades happened if needed, store the new version. |
81 | - update_option( 'autoptimize_version', $target ); |
|
81 | + update_option('autoptimize_version', $target); |
|
82 | 82 | } |
83 | 83 | } |
84 | 84 | |
@@ -89,10 +89,10 @@ discard block |
||
89 | 89 | protected function on_major_version_update() |
90 | 90 | { |
91 | 91 | // The transients guard here prevents stale object caches from busting the cache on every request. |
92 | - if ( false == get_transient( 'autoptimize_stale_option_buster' ) ) { |
|
93 | - set_transient( 'autoptimize_stale_option_buster', 'Mamsie & Liessie zehhe: ZWIJH!', HOUR_IN_SECONDS ); |
|
92 | + if (false == get_transient('autoptimize_stale_option_buster')) { |
|
93 | + set_transient('autoptimize_stale_option_buster', 'Mamsie & Liessie zehhe: ZWIJH!', HOUR_IN_SECONDS); |
|
94 | 94 | autoptimizeCache::clearall(); |
95 | - add_action( 'admin_notices', 'autoptimizeMain::notice_updated' ); |
|
95 | + add_action('admin_notices', 'autoptimizeMain::notice_updated'); |
|
96 | 96 | } |
97 | 97 | } |
98 | 98 | |
@@ -102,7 +102,7 @@ discard block |
||
102 | 102 | private function upgrade_from_1_6() |
103 | 103 | { |
104 | 104 | // If user was on version 1.6.x, force advanced options to be shown by default. |
105 | - update_option( 'autoptimize_show_adv', '1' ); |
|
105 | + update_option('autoptimize_show_adv', '1'); |
|
106 | 106 | |
107 | 107 | // And remove old options. |
108 | 108 | $to_delete_options = array( |
@@ -114,8 +114,8 @@ discard block |
||
114 | 114 | 'autoptimize_cdn_img_url', |
115 | 115 | 'autoptimize_css_yui', |
116 | 116 | ); |
117 | - foreach ( $to_delete_options as $del_opt ) { |
|
118 | - delete_option( $del_opt ); |
|
117 | + foreach ($to_delete_options as $del_opt) { |
|
118 | + delete_option($del_opt); |
|
119 | 119 | } |
120 | 120 | } |
121 | 121 | |
@@ -126,29 +126,29 @@ discard block |
||
126 | 126 | */ |
127 | 127 | private function upgrade_from_1_7() |
128 | 128 | { |
129 | - if ( ! is_multisite() ) { |
|
130 | - $css_exclude = get_option( 'autoptimize_css_exclude' ); |
|
131 | - if ( empty( $css_exclude ) ) { |
|
129 | + if (!is_multisite()) { |
|
130 | + $css_exclude = get_option('autoptimize_css_exclude'); |
|
131 | + if (empty($css_exclude)) { |
|
132 | 132 | $css_exclude = 'admin-bar.min.css, dashicons.min.css'; |
133 | - } elseif ( false === strpos( $css_exclude, 'dashicons.min.css' ) ) { |
|
133 | + } elseif (false === strpos($css_exclude, 'dashicons.min.css')) { |
|
134 | 134 | $css_exclude .= ', dashicons.min.css'; |
135 | 135 | } |
136 | - update_option( 'autoptimize_css_exclude', $css_exclude ); |
|
136 | + update_option('autoptimize_css_exclude', $css_exclude); |
|
137 | 137 | } else { |
138 | 138 | global $wpdb; |
139 | - $blog_ids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" ); |
|
139 | + $blog_ids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs"); |
|
140 | 140 | $original_blog_id = get_current_blog_id(); |
141 | - foreach ( $blog_ids as $blog_id ) { |
|
142 | - switch_to_blog( $blog_id ); |
|
143 | - $css_exclude = get_option( 'autoptimize_css_exclude' ); |
|
144 | - if ( empty( $css_exclude ) ) { |
|
141 | + foreach ($blog_ids as $blog_id) { |
|
142 | + switch_to_blog($blog_id); |
|
143 | + $css_exclude = get_option('autoptimize_css_exclude'); |
|
144 | + if (empty($css_exclude)) { |
|
145 | 145 | $css_exclude = 'admin-bar.min.css, dashicons.min.css'; |
146 | - } elseif ( false === strpos( $css_exclude, 'dashicons.min.css' ) ) { |
|
146 | + } elseif (false === strpos($css_exclude, 'dashicons.min.css')) { |
|
147 | 147 | $css_exclude .= ', dashicons.min.css'; |
148 | 148 | } |
149 | - update_option( 'autoptimize_css_exclude', $css_exclude ); |
|
149 | + update_option('autoptimize_css_exclude', $css_exclude); |
|
150 | 150 | } |
151 | - switch_to_blog( $original_blog_id ); |
|
151 | + switch_to_blog($original_blog_id); |
|
152 | 152 | } |
153 | 153 | } |
154 | 154 | |
@@ -160,19 +160,19 @@ discard block |
||
160 | 160 | */ |
161 | 161 | private function upgrade_from_1_9() |
162 | 162 | { |
163 | - if ( ! is_multisite() ) { |
|
164 | - update_option( 'autoptimize_css_include_inline', 'on' ); |
|
165 | - update_option( 'autoptimize_js_include_inline', 'on' ); |
|
163 | + if (!is_multisite()) { |
|
164 | + update_option('autoptimize_css_include_inline', 'on'); |
|
165 | + update_option('autoptimize_js_include_inline', 'on'); |
|
166 | 166 | } else { |
167 | 167 | global $wpdb; |
168 | - $blog_ids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" ); |
|
168 | + $blog_ids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs"); |
|
169 | 169 | $original_blog_id = get_current_blog_id(); |
170 | - foreach ( $blog_ids as $blog_id ) { |
|
171 | - switch_to_blog( $blog_id ); |
|
172 | - update_option( 'autoptimize_css_include_inline', 'on' ); |
|
173 | - update_option( 'autoptimize_js_include_inline', 'on' ); |
|
170 | + foreach ($blog_ids as $blog_id) { |
|
171 | + switch_to_blog($blog_id); |
|
172 | + update_option('autoptimize_css_include_inline', 'on'); |
|
173 | + update_option('autoptimize_js_include_inline', 'on'); |
|
174 | 174 | } |
175 | - switch_to_blog( $original_blog_id ); |
|
175 | + switch_to_blog($original_blog_id); |
|
176 | 176 | } |
177 | 177 | } |
178 | 178 | |
@@ -183,17 +183,17 @@ discard block |
||
183 | 183 | */ |
184 | 184 | private function upgrade_from_2_2() |
185 | 185 | { |
186 | - if ( ! is_multisite() ) { |
|
186 | + if (!is_multisite()) { |
|
187 | 187 | $this->do_2_2_settings_update(); |
188 | 188 | } else { |
189 | 189 | global $wpdb; |
190 | - $blog_ids = $wpdb->get_col( "SELECT blog_id FROM $wpdb->blogs" ); |
|
190 | + $blog_ids = $wpdb->get_col("SELECT blog_id FROM $wpdb->blogs"); |
|
191 | 191 | $original_blog_id = get_current_blog_id(); |
192 | - foreach ( $blog_ids as $blog_id ) { |
|
193 | - switch_to_blog( $blog_id ); |
|
192 | + foreach ($blog_ids as $blog_id) { |
|
193 | + switch_to_blog($blog_id); |
|
194 | 194 | $this->do_2_2_settings_update(); |
195 | 195 | } |
196 | - switch_to_blog( $original_blog_id ); |
|
196 | + switch_to_blog($original_blog_id); |
|
197 | 197 | } |
198 | 198 | } |
199 | 199 | |
@@ -202,12 +202,12 @@ discard block |
||
202 | 202 | */ |
203 | 203 | private function do_2_2_settings_update() |
204 | 204 | { |
205 | - $nogooglefont = get_option( 'autoptimize_css_nogooglefont', '' ); |
|
206 | - $ao_extrasetting = get_option( 'autoptimize_extra_settings', '' ); |
|
207 | - if ( ( $nogooglefont ) && ( empty( $ao_extrasetting ) ) ) { |
|
208 | - update_option( 'autoptimize_extra_settings', autoptimizeConfig::get_ao_extra_default_options() ); |
|
205 | + $nogooglefont = get_option('autoptimize_css_nogooglefont', ''); |
|
206 | + $ao_extrasetting = get_option('autoptimize_extra_settings', ''); |
|
207 | + if (($nogooglefont) && (empty($ao_extrasetting))) { |
|
208 | + update_option('autoptimize_extra_settings', autoptimizeConfig::get_ao_extra_default_options()); |
|
209 | 209 | } |
210 | - delete_option( 'autoptimize_css_nogooglefont' ); |
|
210 | + delete_option('autoptimize_css_nogooglefont'); |
|
211 | 211 | } |
212 | 212 | |
213 | 213 | /** |
@@ -219,15 +219,15 @@ discard block |
||
219 | 219 | $jobs = _get_cron_array(); |
220 | 220 | |
221 | 221 | // Remove all ao_cachechecker cron jobs (for now). |
222 | - foreach ( $jobs as $when => $job ) { |
|
223 | - $name = key( $job ); |
|
222 | + foreach ($jobs as $when => $job) { |
|
223 | + $name = key($job); |
|
224 | 224 | |
225 | - if ( false !== strpos( $name, 'ao_cachechecker' ) ) { |
|
226 | - unset( $jobs[ $when ] ); |
|
225 | + if (false !== strpos($name, 'ao_cachechecker')) { |
|
226 | + unset($jobs[$when]); |
|
227 | 227 | } |
228 | 228 | } |
229 | 229 | |
230 | 230 | // Save the data. |
231 | - _set_cron_array( $jobs ); |
|
231 | + _set_cron_array($jobs); |
|
232 | 232 | } |
233 | 233 | } |
@@ -128,7 +128,7 @@ discard block |
||
128 | 128 | /** |
129 | 129 | * Determines and returns the service launch status. |
130 | 130 | * |
131 | - * @return bool |
|
131 | + * @return null|boolean |
|
132 | 132 | */ |
133 | 133 | public function launch_ok() |
134 | 134 | { |
@@ -400,6 +400,9 @@ discard block |
||
400 | 400 | } |
401 | 401 | } |
402 | 402 | |
403 | + /** |
|
404 | + * @return string |
|
405 | + */ |
|
403 | 406 | private function get_imgopt_base_url() |
404 | 407 | { |
405 | 408 | static $imgopt_base_url = null; |
@@ -3,7 +3,7 @@ discard block |
||
3 | 3 | * Handles optimizing images. |
4 | 4 | */ |
5 | 5 | |
6 | -if ( ! defined( 'ABSPATH' ) ) { |
|
6 | +if (!defined('ABSPATH')) { |
|
7 | 7 | exit; |
8 | 8 | } |
9 | 9 | |
@@ -23,18 +23,18 @@ discard block |
||
23 | 23 | */ |
24 | 24 | protected static $instance = null; |
25 | 25 | |
26 | - public function __construct( array $options = array() ) |
|
26 | + public function __construct(array $options = array()) |
|
27 | 27 | { |
28 | 28 | // If options are not provided, grab them from autoptimizeExtra, as |
29 | 29 | // that's what we're relying on to do image optimizations for now... |
30 | - if ( empty( $options ) ) { |
|
30 | + if (empty($options)) { |
|
31 | 31 | $options = autoptimizeExtra::fetch_options(); |
32 | 32 | } |
33 | 33 | |
34 | - $this->set_options( $options ); |
|
34 | + $this->set_options($options); |
|
35 | 35 | } |
36 | 36 | |
37 | - public function set_options( array $options ) |
|
37 | + public function set_options(array $options) |
|
38 | 38 | { |
39 | 39 | $this->options = $options; |
40 | 40 | |
@@ -51,7 +51,7 @@ discard block |
||
51 | 51 | */ |
52 | 52 | public static function instance() |
53 | 53 | { |
54 | - if ( null === self::$instance ) { |
|
54 | + if (null === self::$instance) { |
|
55 | 55 | self::$instance = new self(); |
56 | 56 | } |
57 | 57 | |
@@ -60,36 +60,36 @@ discard block |
||
60 | 60 | |
61 | 61 | public function run() |
62 | 62 | { |
63 | - if ( ! $this->should_run() ) { |
|
63 | + if (!$this->should_run()) { |
|
64 | 64 | return; |
65 | 65 | } |
66 | 66 | |
67 | 67 | $active = false; |
68 | 68 | |
69 | - if ( apply_filters( 'autoptimize_filter_extra_imgopt_do', true ) ) { |
|
69 | + if (apply_filters('autoptimize_filter_extra_imgopt_do', true)) { |
|
70 | 70 | add_filter( |
71 | 71 | 'autoptimize_html_after_minify', |
72 | - array( $this, 'filter_optimize_images' ), |
|
72 | + array($this, 'filter_optimize_images'), |
|
73 | 73 | 10, |
74 | 74 | 1 |
75 | 75 | ); |
76 | 76 | $active = true; |
77 | 77 | } |
78 | 78 | |
79 | - if ( apply_filters( 'autoptimize_filter_extra_imgopt_do_css', true ) ) { |
|
79 | + if (apply_filters('autoptimize_filter_extra_imgopt_do_css', true)) { |
|
80 | 80 | add_filter( |
81 | 81 | 'autoptimize_filter_base_replace_cdn', |
82 | - array( $this, 'filter_optimize_css_images' ), |
|
82 | + array($this, 'filter_optimize_css_images'), |
|
83 | 83 | 10, |
84 | 84 | 1 |
85 | 85 | ); |
86 | 86 | $active = true; |
87 | 87 | } |
88 | 88 | |
89 | - if ( $active ) { |
|
89 | + if ($active) { |
|
90 | 90 | add_filter( |
91 | 91 | 'autoptimize_extra_filter_tobepreconn', |
92 | - array( $this, 'filter_preconnect_imgopt_url' ), |
|
92 | + array($this, 'filter_preconnect_imgopt_url'), |
|
93 | 93 | 10, |
94 | 94 | 1 |
95 | 95 | ); |
@@ -104,8 +104,8 @@ discard block |
||
104 | 104 | protected function should_run() |
105 | 105 | { |
106 | 106 | $opts = $this->options; |
107 | - $service_not_down = ( 'down' !== $opts['availabilities']['extra_imgopt']['status'] ); |
|
108 | - $not_launch_status = ( 'launch' !== $opts['availabilities']['extra_imgopt']['status'] ); |
|
107 | + $service_not_down = ('down' !== $opts['availabilities']['extra_imgopt']['status']); |
|
108 | + $not_launch_status = ('launch' !== $opts['availabilities']['extra_imgopt']['status']); |
|
109 | 109 | |
110 | 110 | $do_cdn = true; |
111 | 111 | $_userstatus = $this->get_imgopt_provider_userstatus(); |
@@ -114,10 +114,10 @@ discard block |
||
114 | 114 | } |
115 | 115 | |
116 | 116 | if ( |
117 | - ! empty( $opts['autoptimize_extra_checkbox_field_5'] ) |
|
117 | + !empty($opts['autoptimize_extra_checkbox_field_5']) |
|
118 | 118 | && $do_cdn |
119 | 119 | && $service_not_down |
120 | - && ( $not_launch_status || $this->launch_ok() ) |
|
120 | + && ($not_launch_status || $this->launch_ok()) |
|
121 | 121 | ) { |
122 | 122 | return true; |
123 | 123 | } |
@@ -134,15 +134,15 @@ discard block |
||
134 | 134 | { |
135 | 135 | static $launch_status = null; |
136 | 136 | |
137 | - if ( null === $launch_status ) { |
|
137 | + if (null === $launch_status) { |
|
138 | 138 | $avail_imgopt = $this->options['availabilities']['extra_imgopt']; |
139 | - $magic_number = intval( substr( md5( parse_url( AUTOPTIMIZE_WP_SITE_URL, PHP_URL_HOST ) ), 0, 3 ), 16 ); |
|
140 | - $has_launched = get_option( 'autoptimize_imgopt_launched', '' ); |
|
139 | + $magic_number = intval(substr(md5(parse_url(AUTOPTIMIZE_WP_SITE_URL, PHP_URL_HOST)), 0, 3), 16); |
|
140 | + $has_launched = get_option('autoptimize_imgopt_launched', ''); |
|
141 | 141 | $launch_status = false; |
142 | - if ( $has_launched || ( is_array( $avail_imgopt ) && array_key_exists( 'launch-threshold', $avail_imgopt ) && $magic_number < $avail_imgopt['launch-threshold'] ) ) { |
|
142 | + if ($has_launched || (is_array($avail_imgopt) && array_key_exists('launch-threshold', $avail_imgopt) && $magic_number < $avail_imgopt['launch-threshold'])) { |
|
143 | 143 | $launch_status = true; |
144 | - if ( ! $has_launched ) { |
|
145 | - update_option( 'autoptimize_imgopt_launched', 'on' ); |
|
144 | + if (!$has_launched) { |
|
145 | + update_option('autoptimize_imgopt_launched', 'on'); |
|
146 | 146 | } |
147 | 147 | } |
148 | 148 | } |
@@ -154,11 +154,11 @@ discard block |
||
154 | 154 | { |
155 | 155 | static $imgopt_host = null; |
156 | 156 | |
157 | - if ( null === $imgopt_host ) { |
|
157 | + if (null === $imgopt_host) { |
|
158 | 158 | $imgopt_host = 'https://cdn.shortpixel.ai/'; |
159 | 159 | $avail_imgopt = $this->options['availabilities']['extra_imgopt']; |
160 | - if ( ! empty( $avail_imgopt ) && array_key_exists( 'hosts', $avail_imgopt ) && is_array( $avail_imgopt['hosts'] ) ) { |
|
161 | - $imgopt_host = array_rand( array_flip( $avail_imgopt['hosts'] ) ); |
|
160 | + if (!empty($avail_imgopt) && array_key_exists('hosts', $avail_imgopt) && is_array($avail_imgopt['hosts'])) { |
|
161 | + $imgopt_host = array_rand(array_flip($avail_imgopt['hosts'])); |
|
162 | 162 | } |
163 | 163 | } |
164 | 164 | |
@@ -168,16 +168,16 @@ discard block |
||
168 | 168 | public function get_imgopt_provider_userstatus() { |
169 | 169 | static $_provider_userstatus = null; |
170 | 170 | |
171 | - if ( is_null( $_provider_userstatus ) ) { |
|
172 | - $_stat = get_option( 'autoptimize_imgopt_provider_stat', '' ); |
|
173 | - if ( is_array( $_stat ) ) { |
|
174 | - if ( array_key_exists( 'Status', $_stat ) ) { |
|
171 | + if (is_null($_provider_userstatus)) { |
|
172 | + $_stat = get_option('autoptimize_imgopt_provider_stat', ''); |
|
173 | + if (is_array($_stat)) { |
|
174 | + if (array_key_exists('Status', $_stat)) { |
|
175 | 175 | $_provider_userstatus['Status'] = $_stat['Status']; |
176 | 176 | } else { |
177 | 177 | // if no stats then we assume all is well. |
178 | 178 | $_provider_userstatus['Status'] = 2; |
179 | 179 | } |
180 | - if ( array_key_exists( 'timestamp', $_stat ) ) { |
|
180 | + if (array_key_exists('timestamp', $_stat)) { |
|
181 | 181 | $_provider_userstatus['timestamp'] = $_stat['timestamp']; |
182 | 182 | } else { |
183 | 183 | // if no timestamp then we return "". |
@@ -192,24 +192,24 @@ discard block |
||
192 | 192 | public function get_status_notice() |
193 | 193 | { |
194 | 194 | $opts = $this->options; |
195 | - if ( ! empty( $opts ) && is_array( $opts ) && array_key_exists( 'autoptimize_extra_checkbox_field_5', $opts ) && ! empty( $opts['autoptimize_extra_checkbox_field_5'] ) ) { |
|
195 | + if (!empty($opts) && is_array($opts) && array_key_exists('autoptimize_extra_checkbox_field_5', $opts) && !empty($opts['autoptimize_extra_checkbox_field_5'])) { |
|
196 | 196 | $notice = ''; |
197 | 197 | $stat = $this->get_imgopt_provider_userstatus(); |
198 | - $upsell = 'https://shortpixel.com/aospai/af/GWRGFLW109483/' . AUTOPTIMIZE_SITE_DOMAIN; |
|
198 | + $upsell = 'https://shortpixel.com/aospai/af/GWRGFLW109483/'.AUTOPTIMIZE_SITE_DOMAIN; |
|
199 | 199 | |
200 | - if ( is_array( $stat ) ) { |
|
201 | - if ( 1 == $stat['Status'] ) { |
|
200 | + if (is_array($stat)) { |
|
201 | + if (1 == $stat['Status']) { |
|
202 | 202 | // translators: "add more credits" will appear in a "a href". |
203 | - $notice = sprintf( __( 'Your ShortPixel image optimization and CDN quota is almost used, make sure you %1$sadd more credits%2$s to avoid slowing down your website.', 'autoptimize' ), '<a rel="noopener noreferrer" href="' . $upsell . '" target="_blank">', '</a>' ); |
|
203 | + $notice = sprintf(__('Your ShortPixel image optimization and CDN quota is almost used, make sure you %1$sadd more credits%2$s to avoid slowing down your website.', 'autoptimize'), '<a rel="noopener noreferrer" href="'.$upsell.'" target="_blank">', '</a>'); |
|
204 | 204 | } elseif ( -1 == $stat['Status'] || -2 == $stat['Status'] ) { |
205 | 205 | // translators: "add more credits" will appear in a "a href". |
206 | - $notice = sprintf( __( 'Your ShortPixel image optimization and CDN quota was used, %1$sadd more credits%2$s to keep fast serving optimized images on your site.', 'autoptimize' ), '<a rel="noopener noreferrer" href="' . $upsell . '" target="_blank">', '</a>' ); |
|
206 | + $notice = sprintf(__('Your ShortPixel image optimization and CDN quota was used, %1$sadd more credits%2$s to keep fast serving optimized images on your site.', 'autoptimize'), '<a rel="noopener noreferrer" href="'.$upsell.'" target="_blank">', '</a>'); |
|
207 | 207 | } else { |
208 | 208 | $upsell = 'https://shortpixel.com/g/af/GWRGFLW109483'; |
209 | 209 | // translators: "log in to check your account" will appear in a "a href". |
210 | - $notice = sprintf( __( 'Your ShortPixel image optimization and CDN quota are in good shape, %1$slog in to check your account%2$s.', 'autoptimize' ), '<a rel="noopener noreferrer" href="' . $upsell . '" target="_blank">', '</a>' ); |
|
210 | + $notice = sprintf(__('Your ShortPixel image optimization and CDN quota are in good shape, %1$slog in to check your account%2$s.', 'autoptimize'), '<a rel="noopener noreferrer" href="'.$upsell.'" target="_blank">', '</a>'); |
|
211 | 211 | } |
212 | - $notice = apply_filters( 'autoptimize_filter_imgopt_notice', $notice ); |
|
212 | + $notice = apply_filters('autoptimize_filter_imgopt_notice', $notice); |
|
213 | 213 | |
214 | 214 | return array( |
215 | 215 | 'status' => $stat['Status'], |
@@ -222,21 +222,21 @@ discard block |
||
222 | 222 | |
223 | 223 | public static function get_service_url_suffix() |
224 | 224 | { |
225 | - $suffix = '/af/GWRGFLW109483/' . AUTOPTIMIZE_SITE_DOMAIN; |
|
225 | + $suffix = '/af/GWRGFLW109483/'.AUTOPTIMIZE_SITE_DOMAIN; |
|
226 | 226 | |
227 | 227 | return $suffix; |
228 | 228 | } |
229 | 229 | |
230 | 230 | public function query_img_provider_stats() |
231 | 231 | { |
232 | - if ( ! empty( $this->options['autoptimize_extra_checkbox_field_5'] ) ) { |
|
232 | + if (!empty($this->options['autoptimize_extra_checkbox_field_5'])) { |
|
233 | 233 | $url = ''; |
234 | - $endpoint = $this->get_imgopt_host() . 'read-domain/'; |
|
234 | + $endpoint = $this->get_imgopt_host().'read-domain/'; |
|
235 | 235 | $domain = AUTOPTIMIZE_SITE_DOMAIN; |
236 | 236 | |
237 | 237 | // make sure parse_url result makes sense, keeping $url empty if not. |
238 | - if ( $domain && ! empty( $domain ) ) { |
|
239 | - $url = $endpoint . $domain; |
|
238 | + if ($domain && !empty($domain)) { |
|
239 | + $url = $endpoint.$domain; |
|
240 | 240 | } |
241 | 241 | |
242 | 242 | $url = apply_filters( |
@@ -246,12 +246,12 @@ discard block |
||
246 | 246 | |
247 | 247 | // only do the remote call if $url is not empty to make sure no parse_url |
248 | 248 | // weirdness results in useless calls. |
249 | - if ( ! empty( $url ) ) { |
|
250 | - $response = wp_remote_get( $url ); |
|
251 | - if ( ! is_wp_error( $response ) ) { |
|
252 | - if ( '200' == wp_remote_retrieve_response_code( $response ) ) { |
|
253 | - $stats = json_decode( wp_remote_retrieve_body( $response ), true ); |
|
254 | - update_option( 'autoptimize_imgopt_provider_stat', $stats ); |
|
249 | + if (!empty($url)) { |
|
250 | + $response = wp_remote_get($url); |
|
251 | + if (!is_wp_error($response)) { |
|
252 | + if ('200' == wp_remote_retrieve_response_code($response)) { |
|
253 | + $stats = json_decode(wp_remote_retrieve_body($response), true); |
|
254 | + update_option('autoptimize_imgopt_provider_stat', $stats); |
|
255 | 255 | } |
256 | 256 | } |
257 | 257 | } |
@@ -262,12 +262,12 @@ discard block |
||
262 | 262 | { |
263 | 263 | static $quality = null; |
264 | 264 | |
265 | - if ( null === $quality ) { |
|
265 | + if (null === $quality) { |
|
266 | 266 | $q_array = $this->get_img_quality_array(); |
267 | 267 | $setting = $this->get_img_quality_setting(); |
268 | 268 | $quality = apply_filters( |
269 | 269 | 'autoptimize_filter_extra_imgopt_quality', |
270 | - 'q_' . $q_array[ $setting ] |
|
270 | + 'q_'.$q_array[$setting] |
|
271 | 271 | ); |
272 | 272 | } |
273 | 273 | |
@@ -278,7 +278,7 @@ discard block |
||
278 | 278 | { |
279 | 279 | static $map = null; |
280 | 280 | |
281 | - if ( null === $map ) { |
|
281 | + if (null === $map) { |
|
282 | 282 | $map = array( |
283 | 283 | '1' => 'lossy', |
284 | 284 | '2' => 'glossy', |
@@ -297,12 +297,12 @@ discard block |
||
297 | 297 | { |
298 | 298 | static $q = null; |
299 | 299 | |
300 | - if ( null === $q ) { |
|
301 | - if ( is_array( $this->options ) && array_key_exists( 'autoptimize_extra_select_field_6', $this->options ) ) { |
|
300 | + if (null === $q) { |
|
301 | + if (is_array($this->options) && array_key_exists('autoptimize_extra_select_field_6', $this->options)) { |
|
302 | 302 | $_setting = $this->options['autoptimize_extra_select_field_6']; |
303 | 303 | } |
304 | 304 | |
305 | - if ( ! isset( $setting ) || empty( $setting ) || ( '1' !== $setting && '3' !== $setting ) ) { |
|
305 | + if (!isset($setting) || empty($setting) || ('1' !== $setting && '3' !== $setting)) { |
|
306 | 306 | // default image opt. value is 2 ("glossy"). |
307 | 307 | $q = '2'; |
308 | 308 | } else { |
@@ -313,10 +313,10 @@ discard block |
||
313 | 313 | return $q; |
314 | 314 | } |
315 | 315 | |
316 | - public function filter_preconnect_imgopt_url( array $in ) |
|
316 | + public function filter_preconnect_imgopt_url(array $in) |
|
317 | 317 | { |
318 | - $url_parts = parse_url( $this->get_imgopt_base_url() ); |
|
319 | - $in[] = $url_parts['scheme'] . '://' . $url_parts['host']; |
|
318 | + $url_parts = parse_url($this->get_imgopt_base_url()); |
|
319 | + $in[] = $url_parts['scheme'].'://'.$url_parts['host']; |
|
320 | 320 | |
321 | 321 | return $in; |
322 | 322 | } |
@@ -329,20 +329,20 @@ discard block |
||
329 | 329 | * |
330 | 330 | * @return string |
331 | 331 | */ |
332 | - private function normalize_img_url( $in ) |
|
332 | + private function normalize_img_url($in) |
|
333 | 333 | { |
334 | 334 | // Only parse the site url once. |
335 | 335 | static $parsed_site_url = null; |
336 | - if ( null === $parsed_site_url ) { |
|
337 | - $parsed_site_url = parse_url( site_url() ); |
|
336 | + if (null === $parsed_site_url) { |
|
337 | + $parsed_site_url = parse_url(site_url()); |
|
338 | 338 | } |
339 | 339 | |
340 | 340 | // get CDN domain once. |
341 | 341 | static $cdn_domain = null; |
342 | - if ( is_null( $cdn_domain ) ) { |
|
343 | - $cdn_url = apply_filters( 'autoptimize_filter_base_cdnurl', get_option( 'autoptimize_cdn_url', '' ) ); |
|
344 | - if ( ! empty( $cdn_url ) ) { |
|
345 | - $cdn_domain = parse_url( $cdn_url, PHP_URL_HOST ); |
|
342 | + if (is_null($cdn_domain)) { |
|
343 | + $cdn_url = apply_filters('autoptimize_filter_base_cdnurl', get_option('autoptimize_cdn_url', '')); |
|
344 | + if (!empty($cdn_url)) { |
|
345 | + $cdn_domain = parse_url($cdn_url, PHP_URL_HOST); |
|
346 | 346 | } else { |
347 | 347 | $cdn_domain = ''; |
348 | 348 | } |
@@ -358,43 +358,43 @@ discard block |
||
358 | 358 | * identical string operations). |
359 | 359 | */ |
360 | 360 | static $cache = null; |
361 | - if ( null === $cache ) { |
|
361 | + if (null === $cache) { |
|
362 | 362 | $cache = array(); |
363 | 363 | } |
364 | 364 | |
365 | 365 | // Do the work on cache miss only. |
366 | - if ( ! isset( $cache[ $in ] ) ) { |
|
366 | + if (!isset($cache[$in])) { |
|
367 | 367 | // Default to what was given to us. |
368 | 368 | $result = $in; |
369 | - if ( autoptimizeUtils::is_protocol_relative( $in ) ) { |
|
370 | - $result = $parsed_site_url['scheme'] . ':' . $in; |
|
371 | - } elseif ( 0 === strpos( $in, '/' ) ) { |
|
369 | + if (autoptimizeUtils::is_protocol_relative($in)) { |
|
370 | + $result = $parsed_site_url['scheme'].':'.$in; |
|
371 | + } elseif (0 === strpos($in, '/')) { |
|
372 | 372 | // Root-relative... |
373 | - $result = $parsed_site_url['scheme'] . '://' . $parsed_site_url['host']; |
|
373 | + $result = $parsed_site_url['scheme'].'://'.$parsed_site_url['host']; |
|
374 | 374 | // Add the path for subfolder installs. |
375 | - if ( isset( $parsed_site_url['path'] ) ) { |
|
375 | + if (isset($parsed_site_url['path'])) { |
|
376 | 376 | $result .= $parsed_site_url['path']; |
377 | 377 | } |
378 | 378 | $result .= $in; |
379 | - } elseif ( ! empty( $cdn_domain ) && strpos( $in, $cdn_domain ) !== 0 ) { |
|
380 | - $result = str_replace( $cdn_domain, $parsed_site_url['host'], $in ); |
|
379 | + } elseif (!empty($cdn_domain) && strpos($in, $cdn_domain) !== 0) { |
|
380 | + $result = str_replace($cdn_domain, $parsed_site_url['host'], $in); |
|
381 | 381 | } |
382 | 382 | |
383 | - $result = apply_filters( 'autoptimize_filter_extra_imgopt_normalized_url', $result ); |
|
383 | + $result = apply_filters('autoptimize_filter_extra_imgopt_normalized_url', $result); |
|
384 | 384 | |
385 | 385 | // Store in cache. |
386 | - $cache[ $in ] = $result; |
|
386 | + $cache[$in] = $result; |
|
387 | 387 | } |
388 | 388 | |
389 | - return $cache[ $in ]; |
|
389 | + return $cache[$in]; |
|
390 | 390 | } |
391 | 391 | |
392 | - public function filter_optimize_css_images( $in ) |
|
392 | + public function filter_optimize_css_images($in) |
|
393 | 393 | { |
394 | - $in = $this->normalize_img_url( $in ); |
|
394 | + $in = $this->normalize_img_url($in); |
|
395 | 395 | |
396 | - if ( $this->can_optimize_image( $in ) ) { |
|
397 | - return $this->build_imgopt_url( $in, '', '' ); |
|
396 | + if ($this->can_optimize_image($in)) { |
|
397 | + return $this->build_imgopt_url($in, '', ''); |
|
398 | 398 | } else { |
399 | 399 | return $in; |
400 | 400 | } |
@@ -404,50 +404,50 @@ discard block |
||
404 | 404 | { |
405 | 405 | static $imgopt_base_url = null; |
406 | 406 | |
407 | - if ( null === $imgopt_base_url ) { |
|
407 | + if (null === $imgopt_base_url) { |
|
408 | 408 | $imgopt_host = $this->get_imgopt_host(); |
409 | 409 | $quality = $this->get_img_quality_string(); |
410 | - $ret_val = apply_filters( 'autoptimize_filter_extra_imgopt_wait', 'ret_img' ); // values: ret_wait, ret_img, ret_json, ret_blank. |
|
411 | - $imgopt_base_url = $imgopt_host . 'client/' . $quality . ',' . $ret_val; |
|
412 | - $imgopt_base_url = apply_filters( 'autoptimize_filter_extra_imgopt_base_url', $imgopt_base_url ); |
|
410 | + $ret_val = apply_filters('autoptimize_filter_extra_imgopt_wait', 'ret_img'); // values: ret_wait, ret_img, ret_json, ret_blank. |
|
411 | + $imgopt_base_url = $imgopt_host.'client/'.$quality.','.$ret_val; |
|
412 | + $imgopt_base_url = apply_filters('autoptimize_filter_extra_imgopt_base_url', $imgopt_base_url); |
|
413 | 413 | } |
414 | 414 | |
415 | 415 | return $imgopt_base_url; |
416 | 416 | } |
417 | 417 | |
418 | - private function can_optimize_image( $url ) |
|
418 | + private function can_optimize_image($url) |
|
419 | 419 | { |
420 | 420 | static $cdn_url = null; |
421 | 421 | static $nopti_images = null; |
422 | 422 | |
423 | - if ( null === $cdn_url ) { |
|
423 | + if (null === $cdn_url) { |
|
424 | 424 | $cdn_url = apply_filters( |
425 | 425 | 'autoptimize_filter_base_cdnurl', |
426 | - get_option( 'autoptimize_cdn_url', '' ) |
|
426 | + get_option('autoptimize_cdn_url', '') |
|
427 | 427 | ); |
428 | 428 | } |
429 | 429 | |
430 | - if ( null === $nopti_images ) { |
|
431 | - $nopti_images = apply_filters( 'autoptimize_filter_extra_imgopt_noptimize', '' ); |
|
430 | + if (null === $nopti_images) { |
|
431 | + $nopti_images = apply_filters('autoptimize_filter_extra_imgopt_noptimize', ''); |
|
432 | 432 | } |
433 | 433 | |
434 | 434 | $site_host = AUTOPTIMIZE_SITE_DOMAIN; |
435 | - $url = $this->normalize_img_url( $url ); |
|
436 | - $url_parsed = parse_url( $url ); |
|
435 | + $url = $this->normalize_img_url($url); |
|
436 | + $url_parsed = parse_url($url); |
|
437 | 437 | |
438 | - if ( array_key_exists( 'host', $url_parsed ) && $url_parsed['host'] !== $site_host && empty( $cdn_url ) ) { |
|
438 | + if (array_key_exists('host', $url_parsed) && $url_parsed['host'] !== $site_host && empty($cdn_url)) { |
|
439 | 439 | return false; |
440 | - } elseif ( ! empty( $cdn_url ) && strpos( $url, $cdn_url ) === false && array_key_exists( 'host', $url_parsed ) && $url_parsed['host'] !== $site_host ) { |
|
440 | + } elseif (!empty($cdn_url) && strpos($url, $cdn_url) === false && array_key_exists('host', $url_parsed) && $url_parsed['host'] !== $site_host) { |
|
441 | 441 | return false; |
442 | - } elseif ( strpos( $url, '.php' ) !== false ) { |
|
442 | + } elseif (strpos($url, '.php') !== false) { |
|
443 | 443 | return false; |
444 | - } elseif ( str_ireplace( array( '.png', '.gif', '.jpg', '.jpeg', '.webp' ), '', $url_parsed['path'] ) === $url_parsed['path'] ) { |
|
444 | + } elseif (str_ireplace(array('.png', '.gif', '.jpg', '.jpeg', '.webp'), '', $url_parsed['path']) === $url_parsed['path']) { |
|
445 | 445 | // fixme: better check against end of string. |
446 | 446 | return false; |
447 | - } elseif ( ! empty( $nopti_images ) ) { |
|
448 | - $nopti_images_array = array_filter( array_map( 'trim', explode( ',', $nopti_images ) ) ); |
|
449 | - foreach ( $nopti_images_array as $nopti_image ) { |
|
450 | - if ( strpos( $url, $nopti_image ) !== false ) { |
|
447 | + } elseif (!empty($nopti_images)) { |
|
448 | + $nopti_images_array = array_filter(array_map('trim', explode(',', $nopti_images))); |
|
449 | + foreach ($nopti_images_array as $nopti_image) { |
|
450 | + if (strpos($url, $nopti_image) !== false) { |
|
451 | 451 | return false; |
452 | 452 | } |
453 | 453 | } |
@@ -455,13 +455,13 @@ discard block |
||
455 | 455 | return true; |
456 | 456 | } |
457 | 457 | |
458 | - private function build_imgopt_url( $orig_url, $width = 0, $height = 0 ) |
|
458 | + private function build_imgopt_url($orig_url, $width = 0, $height = 0) |
|
459 | 459 | { |
460 | 460 | // sanitize width and height. |
461 | - if ( strpos( $width, '%' ) !== false ) { |
|
461 | + if (strpos($width, '%') !== false) { |
|
462 | 462 | $width = 0; |
463 | 463 | } |
464 | - if ( strpos( $height, '%' ) !== false ) { |
|
464 | + if (strpos($height, '%') !== false) { |
|
465 | 465 | $height = 0; |
466 | 466 | } |
467 | 467 | $width = (int) $width; |
@@ -475,42 +475,42 @@ discard block |
||
475 | 475 | ); |
476 | 476 | |
477 | 477 | // If filter modified the url, return that. |
478 | - if ( $filtered_url !== $orig_url ) { |
|
478 | + if ($filtered_url !== $orig_url) { |
|
479 | 479 | return $filtered_url; |
480 | 480 | } |
481 | 481 | |
482 | - $orig_url = $this->normalize_img_url( $orig_url ); |
|
482 | + $orig_url = $this->normalize_img_url($orig_url); |
|
483 | 483 | $imgopt_base_url = $this->get_imgopt_base_url(); |
484 | 484 | $imgopt_size = ''; |
485 | 485 | |
486 | - if ( $width && 0 !== $width ) { |
|
487 | - $imgopt_size = ',w_' . $width; |
|
486 | + if ($width && 0 !== $width) { |
|
487 | + $imgopt_size = ',w_'.$width; |
|
488 | 488 | } |
489 | 489 | |
490 | - if ( $height && 0 !== $height ) { |
|
491 | - $imgopt_size .= ',h_' . $height; |
|
490 | + if ($height && 0 !== $height) { |
|
491 | + $imgopt_size .= ',h_'.$height; |
|
492 | 492 | } |
493 | 493 | |
494 | - $url = $imgopt_base_url . $imgopt_size . '/' . $orig_url; |
|
494 | + $url = $imgopt_base_url.$imgopt_size.'/'.$orig_url; |
|
495 | 495 | |
496 | 496 | return $url; |
497 | 497 | } |
498 | 498 | |
499 | - public function replace_data_thumbs( $matches ) |
|
499 | + public function replace_data_thumbs($matches) |
|
500 | 500 | { |
501 | - $this->replace_img_callback( $matches, 150, 150 ); |
|
501 | + $this->replace_img_callback($matches, 150, 150); |
|
502 | 502 | } |
503 | 503 | |
504 | - public function replace_img_callback( $matches, $width=0 , $height=0 ) |
|
504 | + public function replace_img_callback($matches, $width = 0, $height = 0) |
|
505 | 505 | { |
506 | - if ( $this->can_optimize_image( $matches[1] ) ) { |
|
507 | - return str_replace( $matches[1], $this->build_imgopt_url( $matches[1], $width, $height ), $matches[0] ); |
|
506 | + if ($this->can_optimize_image($matches[1])) { |
|
507 | + return str_replace($matches[1], $this->build_imgopt_url($matches[1], $width, $height), $matches[0]); |
|
508 | 508 | } else { |
509 | 509 | return $matches[0]; |
510 | 510 | } |
511 | 511 | } |
512 | 512 | |
513 | - public function filter_optimize_images( $in ) |
|
513 | + public function filter_optimize_images($in) |
|
514 | 514 | { |
515 | 515 | /* |
516 | 516 | * potential future functional improvements: |
@@ -521,26 +521,26 @@ discard block |
||
521 | 521 | $to_replace = array(); |
522 | 522 | |
523 | 523 | // extract img tags. |
524 | - if ( preg_match_all( '#<img[^>]*src[^>]*>#Usmi', $in, $matches ) ) { |
|
525 | - foreach ( $matches[0] as $tag ) { |
|
524 | + if (preg_match_all('#<img[^>]*src[^>]*>#Usmi', $in, $matches)) { |
|
525 | + foreach ($matches[0] as $tag) { |
|
526 | 526 | $orig_tag = $tag; |
527 | 527 | $imgopt_w = ''; |
528 | 528 | $imgopt_h = ''; |
529 | 529 | |
530 | 530 | // first do (data-)srcsets. |
531 | - if ( preg_match_all( '#srcset=("|\')(.*)("|\')#Usmi', $tag, $allsrcsets, PREG_SET_ORDER ) ) { |
|
532 | - foreach ( $allsrcsets as $srcset ) { |
|
531 | + if (preg_match_all('#srcset=("|\')(.*)("|\')#Usmi', $tag, $allsrcsets, PREG_SET_ORDER)) { |
|
532 | + foreach ($allsrcsets as $srcset) { |
|
533 | 533 | $srcset = $srcset[2]; |
534 | - $srcsets = explode( ',', $srcset ); |
|
535 | - foreach ( $srcsets as $indiv_srcset ) { |
|
536 | - $indiv_srcset_parts = explode( ' ', trim( $indiv_srcset ) ); |
|
537 | - if ( $indiv_srcset_parts[1] && rtrim( $indiv_srcset_parts[1], 'w' ) !== $indiv_srcset_parts[1] ) { |
|
538 | - $imgopt_w = rtrim( $indiv_srcset_parts[1], 'w' ); |
|
534 | + $srcsets = explode(',', $srcset); |
|
535 | + foreach ($srcsets as $indiv_srcset) { |
|
536 | + $indiv_srcset_parts = explode(' ', trim($indiv_srcset)); |
|
537 | + if ($indiv_srcset_parts[1] && rtrim($indiv_srcset_parts[1], 'w') !== $indiv_srcset_parts[1]) { |
|
538 | + $imgopt_w = rtrim($indiv_srcset_parts[1], 'w'); |
|
539 | 539 | } |
540 | - if ( $this->can_optimize_image( $indiv_srcset_parts[0] ) ) { |
|
541 | - $imgopt_url = $this->build_imgopt_url( $indiv_srcset_parts[0], $imgopt_w, '' ); |
|
542 | - $tag = str_replace( $indiv_srcset_parts[0], $imgopt_url, $tag ); |
|
543 | - $to_replace[ $orig_tag ] = $tag; |
|
540 | + if ($this->can_optimize_image($indiv_srcset_parts[0])) { |
|
541 | + $imgopt_url = $this->build_imgopt_url($indiv_srcset_parts[0], $imgopt_w, ''); |
|
542 | + $tag = str_replace($indiv_srcset_parts[0], $imgopt_url, $tag); |
|
543 | + $to_replace[$orig_tag] = $tag; |
|
544 | 544 | } |
545 | 545 | } |
546 | 546 | } |
@@ -550,45 +550,45 @@ discard block |
||
550 | 550 | // first reset and then get width and height and add to $imgopt_size. |
551 | 551 | $imgopt_w = ''; |
552 | 552 | $imgopt_h = ''; |
553 | - if ( preg_match( '#width=("|\')(.*)("|\')#Usmi', $tag, $width ) ) { |
|
553 | + if (preg_match('#width=("|\')(.*)("|\')#Usmi', $tag, $width)) { |
|
554 | 554 | $imgopt_w = $width[2]; |
555 | 555 | } |
556 | - if ( preg_match( '#height=("|\')(.*)("|\')#Usmi', $tag, $height ) ) { |
|
556 | + if (preg_match('#height=("|\')(.*)("|\')#Usmi', $tag, $height)) { |
|
557 | 557 | $imgopt_h = $height[2]; |
558 | 558 | } |
559 | 559 | |
560 | 560 | // then start replacing images src. |
561 | - if ( preg_match_all( '#src=(?:"|\')(?!data)(.*)(?:"|\')#Usmi', $tag, $urls, PREG_SET_ORDER ) ) { |
|
562 | - foreach ( $urls as $url ) { |
|
561 | + if (preg_match_all('#src=(?:"|\')(?!data)(.*)(?:"|\')#Usmi', $tag, $urls, PREG_SET_ORDER)) { |
|
562 | + foreach ($urls as $url) { |
|
563 | 563 | $full_src_orig = $url[0]; |
564 | 564 | $url = $url[1]; |
565 | - if ( $this->can_optimize_image( $url ) ) { |
|
566 | - $imgopt_url = $this->build_imgopt_url( $url, $imgopt_w, $imgopt_h ); |
|
567 | - $full_imgopt_src = str_replace( $url, $imgopt_url, $full_src_orig ); |
|
568 | - $tag = str_replace( $full_src_orig, $full_imgopt_src, $tag ); |
|
569 | - $to_replace[ $orig_tag ] = $tag; |
|
565 | + if ($this->can_optimize_image($url)) { |
|
566 | + $imgopt_url = $this->build_imgopt_url($url, $imgopt_w, $imgopt_h); |
|
567 | + $full_imgopt_src = str_replace($url, $imgopt_url, $full_src_orig); |
|
568 | + $tag = str_replace($full_src_orig, $full_imgopt_src, $tag); |
|
569 | + $to_replace[$orig_tag] = $tag; |
|
570 | 570 | } |
571 | 571 | } |
572 | 572 | } |
573 | 573 | } |
574 | 574 | } |
575 | 575 | |
576 | - $out = str_replace( array_keys( $to_replace ), array_values( $to_replace ), $in ); |
|
576 | + $out = str_replace(array_keys($to_replace), array_values($to_replace), $in); |
|
577 | 577 | |
578 | 578 | // img thumbnails in e.g. woocommerce. |
579 | - if ( strpos( $out, 'data-thumb' ) !== false && apply_filters( 'autoptimize_filter_extra_imgopt_datathumbs', true ) ) { |
|
579 | + if (strpos($out, 'data-thumb') !== false && apply_filters('autoptimize_filter_extra_imgopt_datathumbs', true)) { |
|
580 | 580 | $out = preg_replace_callback( |
581 | 581 | '/\<div(?:[^>]?)\sdata-thumb\=(?:\"|\')(.+?)(?:\"|\')(?:[^>]*)?\>/s', |
582 | - array( $this, 'replace_data_thumbs' ), |
|
582 | + array($this, 'replace_data_thumbs'), |
|
583 | 583 | $out |
584 | 584 | ); |
585 | 585 | } |
586 | 586 | |
587 | 587 | // background-image in inline style |
588 | - if ( strpos( $out, 'background-image:' ) !== false && apply_filters( 'autoptimize_filter_extra_imgopt_backgroundimages', true ) ) { |
|
588 | + if (strpos($out, 'background-image:') !== false && apply_filters('autoptimize_filter_extra_imgopt_backgroundimages', true)) { |
|
589 | 589 | $out = preg_replace_callback( |
590 | 590 | '/style=(?:"|\').*?background-image:\s?url\((?:"|\')?([^"\')]*)(?:"|\')?\)/s', |
591 | - array( $this, 'replace_img_callback' ), |
|
591 | + array($this, 'replace_img_callback'), |
|
592 | 592 | $out |
593 | 593 | ); |
594 | 594 | } |
@@ -8,6 +8,9 @@ |
||
8 | 8 | protected $updateChecker; |
9 | 9 | protected $panelClass = 'Puc_v4p4_DebugBar_Panel'; |
10 | 10 | |
11 | + /** |
|
12 | + * @param string $panelClass |
|
13 | + */ |
|
11 | 14 | public function __construct($updateChecker, $panelClass = null) { |
12 | 15 | $this->updateChecker = $updateChecker; |
13 | 16 | if ( isset($panelClass) ) { |
@@ -1,177 +1,177 @@ |
||
1 | 1 | <?php |
2 | 2 | if ( !class_exists('Puc_v4p4_DebugBar_Extension', false) ): |
3 | 3 | |
4 | - class Puc_v4p4_DebugBar_Extension { |
|
5 | - const RESPONSE_BODY_LENGTH_LIMIT = 4000; |
|
6 | - |
|
7 | - /** @var Puc_v4p4_UpdateChecker */ |
|
8 | - protected $updateChecker; |
|
9 | - protected $panelClass = 'Puc_v4p4_DebugBar_Panel'; |
|
10 | - |
|
11 | - public function __construct($updateChecker, $panelClass = null) { |
|
12 | - $this->updateChecker = $updateChecker; |
|
13 | - if ( isset($panelClass) ) { |
|
14 | - $this->panelClass = $panelClass; |
|
15 | - } |
|
16 | - |
|
17 | - add_filter('debug_bar_panels', array($this, 'addDebugBarPanel')); |
|
18 | - add_action('debug_bar_enqueue_scripts', array($this, 'enqueuePanelDependencies')); |
|
19 | - |
|
20 | - add_action('wp_ajax_puc_v4_debug_check_now', array($this, 'ajaxCheckNow')); |
|
21 | - } |
|
22 | - |
|
23 | - /** |
|
24 | - * Register the PUC Debug Bar panel. |
|
25 | - * |
|
26 | - * @param array $panels |
|
27 | - * @return array |
|
28 | - */ |
|
29 | - public function addDebugBarPanel($panels) { |
|
30 | - if ( $this->updateChecker->userCanInstallUpdates() ) { |
|
31 | - $panels[] = new $this->panelClass($this->updateChecker); |
|
32 | - } |
|
33 | - return $panels; |
|
34 | - } |
|
35 | - |
|
36 | - /** |
|
37 | - * Enqueue our Debug Bar scripts and styles. |
|
38 | - */ |
|
39 | - public function enqueuePanelDependencies() { |
|
40 | - wp_enqueue_style( |
|
41 | - 'puc-debug-bar-style-v4', |
|
42 | - $this->getLibraryUrl("/css/puc-debug-bar.css"), |
|
43 | - array('debug-bar'), |
|
44 | - '20171124' |
|
45 | - ); |
|
46 | - |
|
47 | - wp_enqueue_script( |
|
48 | - 'puc-debug-bar-js-v4', |
|
49 | - $this->getLibraryUrl("/js/debug-bar.js"), |
|
50 | - array('jquery'), |
|
51 | - '20170516' |
|
52 | - ); |
|
53 | - } |
|
54 | - |
|
55 | - /** |
|
56 | - * Run an update check and output the result. Useful for making sure that |
|
57 | - * the update checking process works as expected. |
|
58 | - */ |
|
59 | - public function ajaxCheckNow() { |
|
60 | - if ( $_POST['uid'] !== $this->updateChecker->getUniqueName('uid') ) { |
|
61 | - return; |
|
62 | - } |
|
63 | - $this->preAjaxRequest(); |
|
64 | - $update = $this->updateChecker->checkForUpdates(); |
|
65 | - if ( $update !== null ) { |
|
66 | - echo "An update is available:"; |
|
67 | - echo '<pre>', htmlentities(print_r($update, true)), '</pre>'; |
|
68 | - } else { |
|
69 | - echo 'No updates found.'; |
|
70 | - } |
|
71 | - |
|
72 | - $errors = $this->updateChecker->getLastRequestApiErrors(); |
|
73 | - if ( !empty($errors) ) { |
|
74 | - printf('<p>The update checker encountered %d API error%s.</p>', count($errors), (count($errors) > 1) ? 's' : ''); |
|
75 | - |
|
76 | - foreach (array_values($errors) as $num => $item) { |
|
77 | - $wpError = $item['error']; |
|
78 | - /** @var WP_Error $wpError */ |
|
79 | - printf('<h4>%d) %s</h4>', $num + 1, esc_html($wpError->get_error_message())); |
|
80 | - |
|
81 | - echo '<dl>'; |
|
82 | - printf('<dt>Error code:</dt><dd><code>%s</code></dd>', esc_html($wpError->get_error_code())); |
|
83 | - |
|
84 | - if ( isset($item['url']) ) { |
|
85 | - printf('<dt>Requested URL:</dt><dd><code>%s</code></dd>', esc_html($item['url'])); |
|
86 | - } |
|
87 | - |
|
88 | - if ( isset($item['httpResponse']) ) { |
|
89 | - if ( is_wp_error($item['httpResponse']) ) { |
|
90 | - $httpError = $item['httpResponse']; |
|
91 | - /** @var WP_Error $httpError */ |
|
92 | - printf( |
|
93 | - '<dt>WordPress HTTP API error:</dt><dd>%s (<code>%s</code>)</dd>', |
|
94 | - esc_html($httpError->get_error_message()), |
|
95 | - esc_html($httpError->get_error_code()) |
|
96 | - ); |
|
97 | - } else { |
|
98 | - //Status code. |
|
99 | - printf( |
|
100 | - '<dt>HTTP status:</dt><dd><code>%d %s</code></dd>', |
|
101 | - wp_remote_retrieve_response_code($item['httpResponse']), |
|
102 | - wp_remote_retrieve_response_message($item['httpResponse']) |
|
103 | - ); |
|
104 | - |
|
105 | - //Headers. |
|
106 | - echo '<dt>Response headers:</dt><dd><pre>'; |
|
107 | - foreach (wp_remote_retrieve_headers($item['httpResponse']) as $name => $value) { |
|
108 | - printf("%s: %s\n", esc_html($name), esc_html($value)); |
|
109 | - } |
|
110 | - echo '</pre></dd>'; |
|
111 | - |
|
112 | - //Body. |
|
113 | - $body = wp_remote_retrieve_body($item['httpResponse']); |
|
114 | - if ( $body === '' ) { |
|
115 | - $body = '(Empty response.)'; |
|
116 | - } else if ( strlen($body) > self::RESPONSE_BODY_LENGTH_LIMIT ) { |
|
117 | - $length = strlen($body); |
|
118 | - $body = substr($body, 0, self::RESPONSE_BODY_LENGTH_LIMIT) |
|
119 | - . sprintf("\n(Long string truncated. Total length: %d bytes.)", $length); |
|
120 | - } |
|
121 | - |
|
122 | - printf('<dt>Response body:</dt><dd><pre>%s</pre></dd>', esc_html($body)); |
|
123 | - } |
|
124 | - } |
|
125 | - echo '<dl>'; |
|
126 | - } |
|
127 | - } |
|
128 | - |
|
129 | - exit; |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * Check access permissions and enable error display (for debugging). |
|
134 | - */ |
|
135 | - protected function preAjaxRequest() { |
|
136 | - if ( !$this->updateChecker->userCanInstallUpdates() ) { |
|
137 | - die('Access denied'); |
|
138 | - } |
|
139 | - check_ajax_referer('puc-ajax'); |
|
140 | - |
|
141 | - error_reporting(E_ALL); |
|
142 | - @ini_set('display_errors', 'On'); |
|
143 | - } |
|
144 | - |
|
145 | - /** |
|
146 | - * @param string $filePath |
|
147 | - * @return string |
|
148 | - */ |
|
149 | - private function getLibraryUrl($filePath) { |
|
150 | - $absolutePath = realpath(dirname(__FILE__) . '/../../../' . ltrim($filePath, '/')); |
|
151 | - |
|
152 | - //Where is the library located inside the WordPress directory structure? |
|
153 | - $absolutePath = Puc_v4p4_Factory::normalizePath($absolutePath); |
|
154 | - |
|
155 | - $pluginDir = Puc_v4p4_Factory::normalizePath(WP_PLUGIN_DIR); |
|
156 | - $muPluginDir = Puc_v4p4_Factory::normalizePath(WPMU_PLUGIN_DIR); |
|
157 | - $themeDir = Puc_v4p4_Factory::normalizePath(get_theme_root()); |
|
158 | - |
|
159 | - if ( (strpos($absolutePath, $pluginDir) === 0) || (strpos($absolutePath, $muPluginDir) === 0) ) { |
|
160 | - //It's part of a plugin. |
|
161 | - return plugins_url(basename($absolutePath), $absolutePath); |
|
162 | - } else if ( strpos($absolutePath, $themeDir) === 0 ) { |
|
163 | - //It's part of a theme. |
|
164 | - $relativePath = substr($absolutePath, strlen($themeDir) + 1); |
|
165 | - $template = substr($relativePath, 0, strpos($relativePath, '/')); |
|
166 | - $baseUrl = get_theme_root_uri($template); |
|
167 | - |
|
168 | - if ( !empty($baseUrl) && $relativePath ) { |
|
169 | - return $baseUrl . '/' . $relativePath; |
|
170 | - } |
|
171 | - } |
|
172 | - |
|
173 | - return ''; |
|
174 | - } |
|
175 | - } |
|
4 | + class Puc_v4p4_DebugBar_Extension { |
|
5 | + const RESPONSE_BODY_LENGTH_LIMIT = 4000; |
|
6 | + |
|
7 | + /** @var Puc_v4p4_UpdateChecker */ |
|
8 | + protected $updateChecker; |
|
9 | + protected $panelClass = 'Puc_v4p4_DebugBar_Panel'; |
|
10 | + |
|
11 | + public function __construct($updateChecker, $panelClass = null) { |
|
12 | + $this->updateChecker = $updateChecker; |
|
13 | + if ( isset($panelClass) ) { |
|
14 | + $this->panelClass = $panelClass; |
|
15 | + } |
|
16 | + |
|
17 | + add_filter('debug_bar_panels', array($this, 'addDebugBarPanel')); |
|
18 | + add_action('debug_bar_enqueue_scripts', array($this, 'enqueuePanelDependencies')); |
|
19 | + |
|
20 | + add_action('wp_ajax_puc_v4_debug_check_now', array($this, 'ajaxCheckNow')); |
|
21 | + } |
|
22 | + |
|
23 | + /** |
|
24 | + * Register the PUC Debug Bar panel. |
|
25 | + * |
|
26 | + * @param array $panels |
|
27 | + * @return array |
|
28 | + */ |
|
29 | + public function addDebugBarPanel($panels) { |
|
30 | + if ( $this->updateChecker->userCanInstallUpdates() ) { |
|
31 | + $panels[] = new $this->panelClass($this->updateChecker); |
|
32 | + } |
|
33 | + return $panels; |
|
34 | + } |
|
35 | + |
|
36 | + /** |
|
37 | + * Enqueue our Debug Bar scripts and styles. |
|
38 | + */ |
|
39 | + public function enqueuePanelDependencies() { |
|
40 | + wp_enqueue_style( |
|
41 | + 'puc-debug-bar-style-v4', |
|
42 | + $this->getLibraryUrl("/css/puc-debug-bar.css"), |
|
43 | + array('debug-bar'), |
|
44 | + '20171124' |
|
45 | + ); |
|
46 | + |
|
47 | + wp_enqueue_script( |
|
48 | + 'puc-debug-bar-js-v4', |
|
49 | + $this->getLibraryUrl("/js/debug-bar.js"), |
|
50 | + array('jquery'), |
|
51 | + '20170516' |
|
52 | + ); |
|
53 | + } |
|
54 | + |
|
55 | + /** |
|
56 | + * Run an update check and output the result. Useful for making sure that |
|
57 | + * the update checking process works as expected. |
|
58 | + */ |
|
59 | + public function ajaxCheckNow() { |
|
60 | + if ( $_POST['uid'] !== $this->updateChecker->getUniqueName('uid') ) { |
|
61 | + return; |
|
62 | + } |
|
63 | + $this->preAjaxRequest(); |
|
64 | + $update = $this->updateChecker->checkForUpdates(); |
|
65 | + if ( $update !== null ) { |
|
66 | + echo "An update is available:"; |
|
67 | + echo '<pre>', htmlentities(print_r($update, true)), '</pre>'; |
|
68 | + } else { |
|
69 | + echo 'No updates found.'; |
|
70 | + } |
|
71 | + |
|
72 | + $errors = $this->updateChecker->getLastRequestApiErrors(); |
|
73 | + if ( !empty($errors) ) { |
|
74 | + printf('<p>The update checker encountered %d API error%s.</p>', count($errors), (count($errors) > 1) ? 's' : ''); |
|
75 | + |
|
76 | + foreach (array_values($errors) as $num => $item) { |
|
77 | + $wpError = $item['error']; |
|
78 | + /** @var WP_Error $wpError */ |
|
79 | + printf('<h4>%d) %s</h4>', $num + 1, esc_html($wpError->get_error_message())); |
|
80 | + |
|
81 | + echo '<dl>'; |
|
82 | + printf('<dt>Error code:</dt><dd><code>%s</code></dd>', esc_html($wpError->get_error_code())); |
|
83 | + |
|
84 | + if ( isset($item['url']) ) { |
|
85 | + printf('<dt>Requested URL:</dt><dd><code>%s</code></dd>', esc_html($item['url'])); |
|
86 | + } |
|
87 | + |
|
88 | + if ( isset($item['httpResponse']) ) { |
|
89 | + if ( is_wp_error($item['httpResponse']) ) { |
|
90 | + $httpError = $item['httpResponse']; |
|
91 | + /** @var WP_Error $httpError */ |
|
92 | + printf( |
|
93 | + '<dt>WordPress HTTP API error:</dt><dd>%s (<code>%s</code>)</dd>', |
|
94 | + esc_html($httpError->get_error_message()), |
|
95 | + esc_html($httpError->get_error_code()) |
|
96 | + ); |
|
97 | + } else { |
|
98 | + //Status code. |
|
99 | + printf( |
|
100 | + '<dt>HTTP status:</dt><dd><code>%d %s</code></dd>', |
|
101 | + wp_remote_retrieve_response_code($item['httpResponse']), |
|
102 | + wp_remote_retrieve_response_message($item['httpResponse']) |
|
103 | + ); |
|
104 | + |
|
105 | + //Headers. |
|
106 | + echo '<dt>Response headers:</dt><dd><pre>'; |
|
107 | + foreach (wp_remote_retrieve_headers($item['httpResponse']) as $name => $value) { |
|
108 | + printf("%s: %s\n", esc_html($name), esc_html($value)); |
|
109 | + } |
|
110 | + echo '</pre></dd>'; |
|
111 | + |
|
112 | + //Body. |
|
113 | + $body = wp_remote_retrieve_body($item['httpResponse']); |
|
114 | + if ( $body === '' ) { |
|
115 | + $body = '(Empty response.)'; |
|
116 | + } else if ( strlen($body) > self::RESPONSE_BODY_LENGTH_LIMIT ) { |
|
117 | + $length = strlen($body); |
|
118 | + $body = substr($body, 0, self::RESPONSE_BODY_LENGTH_LIMIT) |
|
119 | + . sprintf("\n(Long string truncated. Total length: %d bytes.)", $length); |
|
120 | + } |
|
121 | + |
|
122 | + printf('<dt>Response body:</dt><dd><pre>%s</pre></dd>', esc_html($body)); |
|
123 | + } |
|
124 | + } |
|
125 | + echo '<dl>'; |
|
126 | + } |
|
127 | + } |
|
128 | + |
|
129 | + exit; |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * Check access permissions and enable error display (for debugging). |
|
134 | + */ |
|
135 | + protected function preAjaxRequest() { |
|
136 | + if ( !$this->updateChecker->userCanInstallUpdates() ) { |
|
137 | + die('Access denied'); |
|
138 | + } |
|
139 | + check_ajax_referer('puc-ajax'); |
|
140 | + |
|
141 | + error_reporting(E_ALL); |
|
142 | + @ini_set('display_errors', 'On'); |
|
143 | + } |
|
144 | + |
|
145 | + /** |
|
146 | + * @param string $filePath |
|
147 | + * @return string |
|
148 | + */ |
|
149 | + private function getLibraryUrl($filePath) { |
|
150 | + $absolutePath = realpath(dirname(__FILE__) . '/../../../' . ltrim($filePath, '/')); |
|
151 | + |
|
152 | + //Where is the library located inside the WordPress directory structure? |
|
153 | + $absolutePath = Puc_v4p4_Factory::normalizePath($absolutePath); |
|
154 | + |
|
155 | + $pluginDir = Puc_v4p4_Factory::normalizePath(WP_PLUGIN_DIR); |
|
156 | + $muPluginDir = Puc_v4p4_Factory::normalizePath(WPMU_PLUGIN_DIR); |
|
157 | + $themeDir = Puc_v4p4_Factory::normalizePath(get_theme_root()); |
|
158 | + |
|
159 | + if ( (strpos($absolutePath, $pluginDir) === 0) || (strpos($absolutePath, $muPluginDir) === 0) ) { |
|
160 | + //It's part of a plugin. |
|
161 | + return plugins_url(basename($absolutePath), $absolutePath); |
|
162 | + } else if ( strpos($absolutePath, $themeDir) === 0 ) { |
|
163 | + //It's part of a theme. |
|
164 | + $relativePath = substr($absolutePath, strlen($themeDir) + 1); |
|
165 | + $template = substr($relativePath, 0, strpos($relativePath, '/')); |
|
166 | + $baseUrl = get_theme_root_uri($template); |
|
167 | + |
|
168 | + if ( !empty($baseUrl) && $relativePath ) { |
|
169 | + return $baseUrl . '/' . $relativePath; |
|
170 | + } |
|
171 | + } |
|
172 | + |
|
173 | + return ''; |
|
174 | + } |
|
175 | + } |
|
176 | 176 | |
177 | 177 | endif; |
@@ -1,5 +1,5 @@ discard block |
||
1 | 1 | <?php |
2 | -if ( !class_exists('Puc_v4p4_DebugBar_Extension', false) ): |
|
2 | +if (!class_exists('Puc_v4p4_DebugBar_Extension', false)): |
|
3 | 3 | |
4 | 4 | class Puc_v4p4_DebugBar_Extension { |
5 | 5 | const RESPONSE_BODY_LENGTH_LIMIT = 4000; |
@@ -10,7 +10,7 @@ discard block |
||
10 | 10 | |
11 | 11 | public function __construct($updateChecker, $panelClass = null) { |
12 | 12 | $this->updateChecker = $updateChecker; |
13 | - if ( isset($panelClass) ) { |
|
13 | + if (isset($panelClass)) { |
|
14 | 14 | $this->panelClass = $panelClass; |
15 | 15 | } |
16 | 16 | |
@@ -27,7 +27,7 @@ discard block |
||
27 | 27 | * @return array |
28 | 28 | */ |
29 | 29 | public function addDebugBarPanel($panels) { |
30 | - if ( $this->updateChecker->userCanInstallUpdates() ) { |
|
30 | + if ($this->updateChecker->userCanInstallUpdates()) { |
|
31 | 31 | $panels[] = new $this->panelClass($this->updateChecker); |
32 | 32 | } |
33 | 33 | return $panels; |
@@ -57,12 +57,12 @@ discard block |
||
57 | 57 | * the update checking process works as expected. |
58 | 58 | */ |
59 | 59 | public function ajaxCheckNow() { |
60 | - if ( $_POST['uid'] !== $this->updateChecker->getUniqueName('uid') ) { |
|
60 | + if ($_POST['uid'] !== $this->updateChecker->getUniqueName('uid')) { |
|
61 | 61 | return; |
62 | 62 | } |
63 | 63 | $this->preAjaxRequest(); |
64 | 64 | $update = $this->updateChecker->checkForUpdates(); |
65 | - if ( $update !== null ) { |
|
65 | + if ($update !== null) { |
|
66 | 66 | echo "An update is available:"; |
67 | 67 | echo '<pre>', htmlentities(print_r($update, true)), '</pre>'; |
68 | 68 | } else { |
@@ -70,7 +70,7 @@ discard block |
||
70 | 70 | } |
71 | 71 | |
72 | 72 | $errors = $this->updateChecker->getLastRequestApiErrors(); |
73 | - if ( !empty($errors) ) { |
|
73 | + if (!empty($errors)) { |
|
74 | 74 | printf('<p>The update checker encountered %d API error%s.</p>', count($errors), (count($errors) > 1) ? 's' : ''); |
75 | 75 | |
76 | 76 | foreach (array_values($errors) as $num => $item) { |
@@ -81,12 +81,12 @@ discard block |
||
81 | 81 | echo '<dl>'; |
82 | 82 | printf('<dt>Error code:</dt><dd><code>%s</code></dd>', esc_html($wpError->get_error_code())); |
83 | 83 | |
84 | - if ( isset($item['url']) ) { |
|
84 | + if (isset($item['url'])) { |
|
85 | 85 | printf('<dt>Requested URL:</dt><dd><code>%s</code></dd>', esc_html($item['url'])); |
86 | 86 | } |
87 | 87 | |
88 | - if ( isset($item['httpResponse']) ) { |
|
89 | - if ( is_wp_error($item['httpResponse']) ) { |
|
88 | + if (isset($item['httpResponse'])) { |
|
89 | + if (is_wp_error($item['httpResponse'])) { |
|
90 | 90 | $httpError = $item['httpResponse']; |
91 | 91 | /** @var WP_Error $httpError */ |
92 | 92 | printf( |
@@ -111,9 +111,9 @@ discard block |
||
111 | 111 | |
112 | 112 | //Body. |
113 | 113 | $body = wp_remote_retrieve_body($item['httpResponse']); |
114 | - if ( $body === '' ) { |
|
114 | + if ($body === '') { |
|
115 | 115 | $body = '(Empty response.)'; |
116 | - } else if ( strlen($body) > self::RESPONSE_BODY_LENGTH_LIMIT ) { |
|
116 | + } else if (strlen($body) > self::RESPONSE_BODY_LENGTH_LIMIT) { |
|
117 | 117 | $length = strlen($body); |
118 | 118 | $body = substr($body, 0, self::RESPONSE_BODY_LENGTH_LIMIT) |
119 | 119 | . sprintf("\n(Long string truncated. Total length: %d bytes.)", $length); |
@@ -133,7 +133,7 @@ discard block |
||
133 | 133 | * Check access permissions and enable error display (for debugging). |
134 | 134 | */ |
135 | 135 | protected function preAjaxRequest() { |
136 | - if ( !$this->updateChecker->userCanInstallUpdates() ) { |
|
136 | + if (!$this->updateChecker->userCanInstallUpdates()) { |
|
137 | 137 | die('Access denied'); |
138 | 138 | } |
139 | 139 | check_ajax_referer('puc-ajax'); |
@@ -147,7 +147,7 @@ discard block |
||
147 | 147 | * @return string |
148 | 148 | */ |
149 | 149 | private function getLibraryUrl($filePath) { |
150 | - $absolutePath = realpath(dirname(__FILE__) . '/../../../' . ltrim($filePath, '/')); |
|
150 | + $absolutePath = realpath(dirname(__FILE__).'/../../../'.ltrim($filePath, '/')); |
|
151 | 151 | |
152 | 152 | //Where is the library located inside the WordPress directory structure? |
153 | 153 | $absolutePath = Puc_v4p4_Factory::normalizePath($absolutePath); |
@@ -156,17 +156,17 @@ discard block |
||
156 | 156 | $muPluginDir = Puc_v4p4_Factory::normalizePath(WPMU_PLUGIN_DIR); |
157 | 157 | $themeDir = Puc_v4p4_Factory::normalizePath(get_theme_root()); |
158 | 158 | |
159 | - if ( (strpos($absolutePath, $pluginDir) === 0) || (strpos($absolutePath, $muPluginDir) === 0) ) { |
|
159 | + if ((strpos($absolutePath, $pluginDir) === 0) || (strpos($absolutePath, $muPluginDir) === 0)) { |
|
160 | 160 | //It's part of a plugin. |
161 | 161 | return plugins_url(basename($absolutePath), $absolutePath); |
162 | - } else if ( strpos($absolutePath, $themeDir) === 0 ) { |
|
162 | + } else if (strpos($absolutePath, $themeDir) === 0) { |
|
163 | 163 | //It's part of a theme. |
164 | 164 | $relativePath = substr($absolutePath, strlen($themeDir) + 1); |
165 | 165 | $template = substr($relativePath, 0, strpos($relativePath, '/')); |
166 | 166 | $baseUrl = get_theme_root_uri($template); |
167 | 167 | |
168 | - if ( !empty($baseUrl) && $relativePath ) { |
|
169 | - return $baseUrl . '/' . $relativePath; |
|
168 | + if (!empty($baseUrl) && $relativePath) { |
|
169 | + return $baseUrl.'/'.$relativePath; |
|
170 | 170 | } |
171 | 171 | } |
172 | 172 |
@@ -84,7 +84,7 @@ |
||
84 | 84 | /** |
85 | 85 | * Transform plugin info into the format used by the native WordPress.org API |
86 | 86 | * |
87 | - * @return object |
|
87 | + * @return stdClass |
|
88 | 88 | */ |
89 | 89 | public function toWpFormat(){ |
90 | 90 | $info = new stdClass; |
@@ -1,130 +1,130 @@ |
||
1 | 1 | <?php |
2 | 2 | if ( !class_exists('Puc_v4p4_Plugin_Info', false) ): |
3 | 3 | |
4 | - /** |
|
5 | - * A container class for holding and transforming various plugin metadata. |
|
6 | - * |
|
7 | - * @author Janis Elsts |
|
8 | - * @copyright 2016 |
|
9 | - * @access public |
|
10 | - */ |
|
11 | - class Puc_v4p4_Plugin_Info extends Puc_v4p4_Metadata { |
|
12 | - //Most fields map directly to the contents of the plugin's info.json file. |
|
13 | - //See the relevant docs for a description of their meaning. |
|
14 | - public $name; |
|
15 | - public $slug; |
|
16 | - public $version; |
|
17 | - public $homepage; |
|
18 | - public $sections = array(); |
|
19 | - public $download_url; |
|
20 | - |
|
21 | - public $banners; |
|
22 | - public $icons = array(); |
|
23 | - public $translations = array(); |
|
24 | - |
|
25 | - public $author; |
|
26 | - public $author_homepage; |
|
27 | - |
|
28 | - public $requires; |
|
29 | - public $tested; |
|
30 | - public $upgrade_notice; |
|
31 | - |
|
32 | - public $rating; |
|
33 | - public $num_ratings; |
|
34 | - public $downloaded; |
|
35 | - public $active_installs; |
|
36 | - public $last_updated; |
|
37 | - |
|
38 | - public $id = 0; //The native WP.org API returns numeric plugin IDs, but they're not used for anything. |
|
39 | - |
|
40 | - public $filename; //Plugin filename relative to the plugins directory. |
|
41 | - |
|
42 | - /** |
|
43 | - * Create a new instance of Plugin Info from JSON-encoded plugin info |
|
44 | - * returned by an external update API. |
|
45 | - * |
|
46 | - * @param string $json Valid JSON string representing plugin info. |
|
47 | - * @return self|null New instance of Plugin Info, or NULL on error. |
|
48 | - */ |
|
49 | - public static function fromJson($json){ |
|
50 | - $instance = new self(); |
|
51 | - |
|
52 | - if ( !parent::createFromJson($json, $instance) ) { |
|
53 | - return null; |
|
54 | - } |
|
55 | - |
|
56 | - //json_decode decodes assoc. arrays as objects. We want them as arrays. |
|
57 | - $instance->sections = (array)$instance->sections; |
|
58 | - $instance->icons = (array)$instance->icons; |
|
59 | - |
|
60 | - return $instance; |
|
61 | - } |
|
62 | - |
|
63 | - /** |
|
64 | - * Very, very basic validation. |
|
65 | - * |
|
66 | - * @param StdClass $apiResponse |
|
67 | - * @return bool|WP_Error |
|
68 | - */ |
|
69 | - protected function validateMetadata($apiResponse) { |
|
70 | - if ( |
|
71 | - !isset($apiResponse->name, $apiResponse->version) |
|
72 | - || empty($apiResponse->name) |
|
73 | - || empty($apiResponse->version) |
|
74 | - ) { |
|
75 | - return new WP_Error( |
|
76 | - 'puc-invalid-metadata', |
|
77 | - "The plugin metadata file does not contain the required 'name' and/or 'version' keys." |
|
78 | - ); |
|
79 | - } |
|
80 | - return true; |
|
81 | - } |
|
82 | - |
|
83 | - |
|
84 | - /** |
|
85 | - * Transform plugin info into the format used by the native WordPress.org API |
|
86 | - * |
|
87 | - * @return object |
|
88 | - */ |
|
89 | - public function toWpFormat(){ |
|
90 | - $info = new stdClass; |
|
91 | - |
|
92 | - //The custom update API is built so that many fields have the same name and format |
|
93 | - //as those returned by the native WordPress.org API. These can be assigned directly. |
|
94 | - $sameFormat = array( |
|
95 | - 'name', 'slug', 'version', 'requires', 'tested', 'rating', 'upgrade_notice', |
|
96 | - 'num_ratings', 'downloaded', 'active_installs', 'homepage', 'last_updated', |
|
97 | - ); |
|
98 | - foreach($sameFormat as $field){ |
|
99 | - if ( isset($this->$field) ) { |
|
100 | - $info->$field = $this->$field; |
|
101 | - } else { |
|
102 | - $info->$field = null; |
|
103 | - } |
|
104 | - } |
|
105 | - |
|
106 | - //Other fields need to be renamed and/or transformed. |
|
107 | - $info->download_link = $this->download_url; |
|
108 | - $info->author = $this->getFormattedAuthor(); |
|
109 | - $info->sections = array_merge(array('description' => ''), $this->sections); |
|
110 | - |
|
111 | - if ( !empty($this->banners) ) { |
|
112 | - //WP expects an array with two keys: "high" and "low". Both are optional. |
|
113 | - //Docs: https://wordpress.org/plugins/about/faq/#banners |
|
114 | - $info->banners = is_object($this->banners) ? get_object_vars($this->banners) : $this->banners; |
|
115 | - $info->banners = array_intersect_key($info->banners, array('high' => true, 'low' => true)); |
|
116 | - } |
|
117 | - |
|
118 | - return $info; |
|
119 | - } |
|
120 | - |
|
121 | - protected function getFormattedAuthor() { |
|
122 | - if ( !empty($this->author_homepage) ){ |
|
123 | - /** @noinspection HtmlUnknownTarget */ |
|
124 | - return sprintf('<a href="%s">%s</a>', $this->author_homepage, $this->author); |
|
125 | - } |
|
126 | - return $this->author; |
|
127 | - } |
|
128 | - } |
|
4 | + /** |
|
5 | + * A container class for holding and transforming various plugin metadata. |
|
6 | + * |
|
7 | + * @author Janis Elsts |
|
8 | + * @copyright 2016 |
|
9 | + * @access public |
|
10 | + */ |
|
11 | + class Puc_v4p4_Plugin_Info extends Puc_v4p4_Metadata { |
|
12 | + //Most fields map directly to the contents of the plugin's info.json file. |
|
13 | + //See the relevant docs for a description of their meaning. |
|
14 | + public $name; |
|
15 | + public $slug; |
|
16 | + public $version; |
|
17 | + public $homepage; |
|
18 | + public $sections = array(); |
|
19 | + public $download_url; |
|
20 | + |
|
21 | + public $banners; |
|
22 | + public $icons = array(); |
|
23 | + public $translations = array(); |
|
24 | + |
|
25 | + public $author; |
|
26 | + public $author_homepage; |
|
27 | + |
|
28 | + public $requires; |
|
29 | + public $tested; |
|
30 | + public $upgrade_notice; |
|
31 | + |
|
32 | + public $rating; |
|
33 | + public $num_ratings; |
|
34 | + public $downloaded; |
|
35 | + public $active_installs; |
|
36 | + public $last_updated; |
|
37 | + |
|
38 | + public $id = 0; //The native WP.org API returns numeric plugin IDs, but they're not used for anything. |
|
39 | + |
|
40 | + public $filename; //Plugin filename relative to the plugins directory. |
|
41 | + |
|
42 | + /** |
|
43 | + * Create a new instance of Plugin Info from JSON-encoded plugin info |
|
44 | + * returned by an external update API. |
|
45 | + * |
|
46 | + * @param string $json Valid JSON string representing plugin info. |
|
47 | + * @return self|null New instance of Plugin Info, or NULL on error. |
|
48 | + */ |
|
49 | + public static function fromJson($json){ |
|
50 | + $instance = new self(); |
|
51 | + |
|
52 | + if ( !parent::createFromJson($json, $instance) ) { |
|
53 | + return null; |
|
54 | + } |
|
55 | + |
|
56 | + //json_decode decodes assoc. arrays as objects. We want them as arrays. |
|
57 | + $instance->sections = (array)$instance->sections; |
|
58 | + $instance->icons = (array)$instance->icons; |
|
59 | + |
|
60 | + return $instance; |
|
61 | + } |
|
62 | + |
|
63 | + /** |
|
64 | + * Very, very basic validation. |
|
65 | + * |
|
66 | + * @param StdClass $apiResponse |
|
67 | + * @return bool|WP_Error |
|
68 | + */ |
|
69 | + protected function validateMetadata($apiResponse) { |
|
70 | + if ( |
|
71 | + !isset($apiResponse->name, $apiResponse->version) |
|
72 | + || empty($apiResponse->name) |
|
73 | + || empty($apiResponse->version) |
|
74 | + ) { |
|
75 | + return new WP_Error( |
|
76 | + 'puc-invalid-metadata', |
|
77 | + "The plugin metadata file does not contain the required 'name' and/or 'version' keys." |
|
78 | + ); |
|
79 | + } |
|
80 | + return true; |
|
81 | + } |
|
82 | + |
|
83 | + |
|
84 | + /** |
|
85 | + * Transform plugin info into the format used by the native WordPress.org API |
|
86 | + * |
|
87 | + * @return object |
|
88 | + */ |
|
89 | + public function toWpFormat(){ |
|
90 | + $info = new stdClass; |
|
91 | + |
|
92 | + //The custom update API is built so that many fields have the same name and format |
|
93 | + //as those returned by the native WordPress.org API. These can be assigned directly. |
|
94 | + $sameFormat = array( |
|
95 | + 'name', 'slug', 'version', 'requires', 'tested', 'rating', 'upgrade_notice', |
|
96 | + 'num_ratings', 'downloaded', 'active_installs', 'homepage', 'last_updated', |
|
97 | + ); |
|
98 | + foreach($sameFormat as $field){ |
|
99 | + if ( isset($this->$field) ) { |
|
100 | + $info->$field = $this->$field; |
|
101 | + } else { |
|
102 | + $info->$field = null; |
|
103 | + } |
|
104 | + } |
|
105 | + |
|
106 | + //Other fields need to be renamed and/or transformed. |
|
107 | + $info->download_link = $this->download_url; |
|
108 | + $info->author = $this->getFormattedAuthor(); |
|
109 | + $info->sections = array_merge(array('description' => ''), $this->sections); |
|
110 | + |
|
111 | + if ( !empty($this->banners) ) { |
|
112 | + //WP expects an array with two keys: "high" and "low". Both are optional. |
|
113 | + //Docs: https://wordpress.org/plugins/about/faq/#banners |
|
114 | + $info->banners = is_object($this->banners) ? get_object_vars($this->banners) : $this->banners; |
|
115 | + $info->banners = array_intersect_key($info->banners, array('high' => true, 'low' => true)); |
|
116 | + } |
|
117 | + |
|
118 | + return $info; |
|
119 | + } |
|
120 | + |
|
121 | + protected function getFormattedAuthor() { |
|
122 | + if ( !empty($this->author_homepage) ){ |
|
123 | + /** @noinspection HtmlUnknownTarget */ |
|
124 | + return sprintf('<a href="%s">%s</a>', $this->author_homepage, $this->author); |
|
125 | + } |
|
126 | + return $this->author; |
|
127 | + } |
|
128 | + } |
|
129 | 129 | |
130 | 130 | endif; |
131 | 131 | \ No newline at end of file |
@@ -1,5 +1,5 @@ discard block |
||
1 | 1 | <?php |
2 | -if ( !class_exists('Puc_v4p4_Plugin_Info', false) ): |
|
2 | +if (!class_exists('Puc_v4p4_Plugin_Info', false)): |
|
3 | 3 | |
4 | 4 | /** |
5 | 5 | * A container class for holding and transforming various plugin metadata. |
@@ -46,16 +46,16 @@ discard block |
||
46 | 46 | * @param string $json Valid JSON string representing plugin info. |
47 | 47 | * @return self|null New instance of Plugin Info, or NULL on error. |
48 | 48 | */ |
49 | - public static function fromJson($json){ |
|
49 | + public static function fromJson($json) { |
|
50 | 50 | $instance = new self(); |
51 | 51 | |
52 | - if ( !parent::createFromJson($json, $instance) ) { |
|
52 | + if (!parent::createFromJson($json, $instance)) { |
|
53 | 53 | return null; |
54 | 54 | } |
55 | 55 | |
56 | 56 | //json_decode decodes assoc. arrays as objects. We want them as arrays. |
57 | - $instance->sections = (array)$instance->sections; |
|
58 | - $instance->icons = (array)$instance->icons; |
|
57 | + $instance->sections = (array) $instance->sections; |
|
58 | + $instance->icons = (array) $instance->icons; |
|
59 | 59 | |
60 | 60 | return $instance; |
61 | 61 | } |
@@ -86,7 +86,7 @@ discard block |
||
86 | 86 | * |
87 | 87 | * @return object |
88 | 88 | */ |
89 | - public function toWpFormat(){ |
|
89 | + public function toWpFormat() { |
|
90 | 90 | $info = new stdClass; |
91 | 91 | |
92 | 92 | //The custom update API is built so that many fields have the same name and format |
@@ -95,8 +95,8 @@ discard block |
||
95 | 95 | 'name', 'slug', 'version', 'requires', 'tested', 'rating', 'upgrade_notice', |
96 | 96 | 'num_ratings', 'downloaded', 'active_installs', 'homepage', 'last_updated', |
97 | 97 | ); |
98 | - foreach($sameFormat as $field){ |
|
99 | - if ( isset($this->$field) ) { |
|
98 | + foreach ($sameFormat as $field) { |
|
99 | + if (isset($this->$field)) { |
|
100 | 100 | $info->$field = $this->$field; |
101 | 101 | } else { |
102 | 102 | $info->$field = null; |
@@ -108,7 +108,7 @@ discard block |
||
108 | 108 | $info->author = $this->getFormattedAuthor(); |
109 | 109 | $info->sections = array_merge(array('description' => ''), $this->sections); |
110 | 110 | |
111 | - if ( !empty($this->banners) ) { |
|
111 | + if (!empty($this->banners)) { |
|
112 | 112 | //WP expects an array with two keys: "high" and "low". Both are optional. |
113 | 113 | //Docs: https://wordpress.org/plugins/about/faq/#banners |
114 | 114 | $info->banners = is_object($this->banners) ? get_object_vars($this->banners) : $this->banners; |
@@ -119,7 +119,7 @@ discard block |
||
119 | 119 | } |
120 | 120 | |
121 | 121 | protected function getFormattedAuthor() { |
122 | - if ( !empty($this->author_homepage) ){ |
|
122 | + if (!empty($this->author_homepage)) { |
|
123 | 123 | /** @noinspection HtmlUnknownTarget */ |
124 | 124 | return sprintf('<a href="%s">%s</a>', $this->author_homepage, $this->author); |
125 | 125 | } |
@@ -628,7 +628,7 @@ |
||
628 | 628 | /** |
629 | 629 | * Check if the plugin file is inside the mu-plugins directory. |
630 | 630 | * |
631 | - * @return bool |
|
631 | + * @return boolean|null |
|
632 | 632 | */ |
633 | 633 | protected function isMuPlugin() { |
634 | 634 | static $cachedResult = null; |
@@ -1,740 +1,740 @@ |
||
1 | 1 | <?php |
2 | 2 | if ( !class_exists('Puc_v4p4_Plugin_UpdateChecker', false) ): |
3 | 3 | |
4 | - /** |
|
5 | - * A custom plugin update checker. |
|
6 | - * |
|
7 | - * @author Janis Elsts |
|
8 | - * @copyright 2016 |
|
9 | - * @access public |
|
10 | - */ |
|
11 | - class Puc_v4p4_Plugin_UpdateChecker extends Puc_v4p4_UpdateChecker { |
|
12 | - protected $updateTransient = 'update_plugins'; |
|
13 | - protected $translationType = 'plugin'; |
|
14 | - |
|
15 | - public $pluginAbsolutePath = ''; //Full path of the main plugin file. |
|
16 | - public $pluginFile = ''; //Plugin filename relative to the plugins directory. Many WP APIs use this to identify plugins. |
|
17 | - public $muPluginFile = ''; //For MU plugins, the plugin filename relative to the mu-plugins directory. |
|
18 | - |
|
19 | - private $cachedInstalledVersion = null; |
|
20 | - private $manualCheckErrorTransient = ''; |
|
21 | - |
|
22 | - /** |
|
23 | - * Class constructor. |
|
24 | - * |
|
25 | - * @param string $metadataUrl The URL of the plugin's metadata file. |
|
26 | - * @param string $pluginFile Fully qualified path to the main plugin file. |
|
27 | - * @param string $slug The plugin's 'slug'. If not specified, the filename part of $pluginFile sans '.php' will be used as the slug. |
|
28 | - * @param integer $checkPeriod How often to check for updates (in hours). Defaults to checking every 12 hours. Set to 0 to disable automatic update checks. |
|
29 | - * @param string $optionName Where to store book-keeping info about update checks. Defaults to 'external_updates-$slug'. |
|
30 | - * @param string $muPluginFile Optional. The plugin filename relative to the mu-plugins directory. |
|
31 | - */ |
|
32 | - public function __construct($metadataUrl, $pluginFile, $slug = '', $checkPeriod = 12, $optionName = '', $muPluginFile = ''){ |
|
33 | - $this->pluginAbsolutePath = $pluginFile; |
|
34 | - $this->pluginFile = plugin_basename($this->pluginAbsolutePath); |
|
35 | - $this->muPluginFile = $muPluginFile; |
|
36 | - |
|
37 | - //If no slug is specified, use the name of the main plugin file as the slug. |
|
38 | - //For example, 'my-cool-plugin/cool-plugin.php' becomes 'cool-plugin'. |
|
39 | - if ( empty($slug) ){ |
|
40 | - $slug = basename($this->pluginFile, '.php'); |
|
41 | - } |
|
42 | - |
|
43 | - //Plugin slugs must be unique. |
|
44 | - $slugCheckFilter = 'puc_is_slug_in_use-' . $this->slug; |
|
45 | - $slugUsedBy = apply_filters($slugCheckFilter, false); |
|
46 | - if ( $slugUsedBy ) { |
|
47 | - $this->triggerError(sprintf( |
|
48 | - 'Plugin slug "%s" is already in use by %s. Slugs must be unique.', |
|
49 | - htmlentities($this->slug), |
|
50 | - htmlentities($slugUsedBy) |
|
51 | - ), E_USER_ERROR); |
|
52 | - } |
|
53 | - add_filter($slugCheckFilter, array($this, 'getAbsolutePath')); |
|
54 | - |
|
55 | - //Backwards compatibility: If the plugin is a mu-plugin but no $muPluginFile is specified, assume |
|
56 | - //it's the same as $pluginFile given that it's not in a subdirectory (WP only looks in the base dir). |
|
57 | - if ( (strpbrk($this->pluginFile, '/\\') === false) && $this->isUnknownMuPlugin() ) { |
|
58 | - $this->muPluginFile = $this->pluginFile; |
|
59 | - } |
|
60 | - |
|
61 | - //To prevent a crash during plugin uninstallation, remove updater hooks when the user removes the plugin. |
|
62 | - //Details: https://github.com/YahnisElsts/plugin-update-checker/issues/138#issuecomment-335590964 |
|
63 | - add_action('uninstall_' . $this->pluginFile, array($this, 'removeHooks')); |
|
64 | - |
|
65 | - $this->manualCheckErrorTransient = $this->getUniqueName('manual_check_errors'); |
|
66 | - |
|
67 | - parent::__construct($metadataUrl, dirname($this->pluginFile), $slug, $checkPeriod, $optionName); |
|
68 | - } |
|
69 | - |
|
70 | - /** |
|
71 | - * Create an instance of the scheduler. |
|
72 | - * |
|
73 | - * @param int $checkPeriod |
|
74 | - * @return Puc_v4p4_Scheduler |
|
75 | - */ |
|
76 | - protected function createScheduler($checkPeriod) { |
|
77 | - $scheduler = new Puc_v4p4_Scheduler($this, $checkPeriod, array('load-plugins.php')); |
|
78 | - register_deactivation_hook($this->pluginFile, array($scheduler, 'removeUpdaterCron')); |
|
79 | - return $scheduler; |
|
80 | - } |
|
81 | - |
|
82 | - /** |
|
83 | - * Install the hooks required to run periodic update checks and inject update info |
|
84 | - * into WP data structures. |
|
85 | - * |
|
86 | - * @return void |
|
87 | - */ |
|
88 | - protected function installHooks(){ |
|
89 | - //Override requests for plugin information |
|
90 | - add_filter('plugins_api', array($this, 'injectInfo'), 20, 3); |
|
91 | - |
|
92 | - add_filter('plugin_row_meta', array($this, 'addViewDetailsLink'), 10, 3); |
|
93 | - add_filter('plugin_row_meta', array($this, 'addCheckForUpdatesLink'), 10, 2); |
|
94 | - add_action('admin_init', array($this, 'handleManualCheck')); |
|
95 | - add_action('all_admin_notices', array($this, 'displayManualCheckResult')); |
|
96 | - |
|
97 | - //Clear the version number cache when something - anything - is upgraded or WP clears the update cache. |
|
98 | - add_filter('upgrader_post_install', array($this, 'clearCachedVersion')); |
|
99 | - add_action('delete_site_transient_update_plugins', array($this, 'clearCachedVersion')); |
|
100 | - |
|
101 | - parent::installHooks(); |
|
102 | - } |
|
103 | - |
|
104 | - /** |
|
105 | - * Remove update checker hooks. |
|
106 | - * |
|
107 | - * The intent is to prevent a fatal error that can happen if the plugin has an uninstall |
|
108 | - * hook. During uninstallation, WP includes the main plugin file (which creates a PUC instance), |
|
109 | - * the uninstall hook runs, WP deletes the plugin files and then updates some transients. |
|
110 | - * If PUC hooks are still around at this time, they could throw an error while trying to |
|
111 | - * autoload classes from files that no longer exist. |
|
112 | - * |
|
113 | - * The "site_transient_{$transient}" filter is the main problem here, but let's also remove |
|
114 | - * most other PUC hooks to be safe. |
|
115 | - * |
|
116 | - * @internal |
|
117 | - */ |
|
118 | - public function removeHooks() { |
|
119 | - parent::removeHooks(); |
|
120 | - |
|
121 | - remove_filter('plugins_api', array($this, 'injectInfo'), 20); |
|
122 | - |
|
123 | - remove_filter('plugin_row_meta', array($this, 'addViewDetailsLink'), 10); |
|
124 | - remove_filter('plugin_row_meta', array($this, 'addCheckForUpdatesLink'), 10); |
|
125 | - remove_action('admin_init', array($this, 'handleManualCheck')); |
|
126 | - remove_action('all_admin_notices', array($this, 'displayManualCheckResult')); |
|
127 | - |
|
128 | - remove_filter('upgrader_post_install', array($this, 'clearCachedVersion')); |
|
129 | - remove_action('delete_site_transient_update_plugins', array($this, 'clearCachedVersion')); |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * Retrieve plugin info from the configured API endpoint. |
|
134 | - * |
|
135 | - * @uses wp_remote_get() |
|
136 | - * |
|
137 | - * @param array $queryArgs Additional query arguments to append to the request. Optional. |
|
138 | - * @return Puc_v4p4_Plugin_Info |
|
139 | - */ |
|
140 | - public function requestInfo($queryArgs = array()) { |
|
141 | - list($pluginInfo, $result) = $this->requestMetadata('Puc_v4p4_Plugin_Info', 'request_info', $queryArgs); |
|
142 | - |
|
143 | - if ( $pluginInfo !== null ) { |
|
144 | - /** @var Puc_v4p4_Plugin_Info $pluginInfo */ |
|
145 | - $pluginInfo->filename = $this->pluginFile; |
|
146 | - $pluginInfo->slug = $this->slug; |
|
147 | - } |
|
148 | - |
|
149 | - $pluginInfo = apply_filters($this->getUniqueName('request_info_result'), $pluginInfo, $result); |
|
150 | - return $pluginInfo; |
|
151 | - } |
|
152 | - |
|
153 | - /** |
|
154 | - * Retrieve the latest update (if any) from the configured API endpoint. |
|
155 | - * |
|
156 | - * @uses PluginUpdateChecker::requestInfo() |
|
157 | - * |
|
158 | - * @return Puc_v4p4_Update|null An instance of Plugin_Update, or NULL when no updates are available. |
|
159 | - */ |
|
160 | - public function requestUpdate() { |
|
161 | - //For the sake of simplicity, this function just calls requestInfo() |
|
162 | - //and transforms the result accordingly. |
|
163 | - $pluginInfo = $this->requestInfo(array('checking_for_updates' => '1')); |
|
164 | - if ( $pluginInfo === null ){ |
|
165 | - return null; |
|
166 | - } |
|
167 | - $update = Puc_v4p4_Plugin_Update::fromPluginInfo($pluginInfo); |
|
168 | - |
|
169 | - $update = $this->filterUpdateResult($update); |
|
170 | - |
|
171 | - return $update; |
|
172 | - } |
|
173 | - |
|
174 | - /** |
|
175 | - * Get the currently installed version of the plugin. |
|
176 | - * |
|
177 | - * @return string Version number. |
|
178 | - */ |
|
179 | - public function getInstalledVersion(){ |
|
180 | - if ( isset($this->cachedInstalledVersion) ) { |
|
181 | - return $this->cachedInstalledVersion; |
|
182 | - } |
|
183 | - |
|
184 | - $pluginHeader = $this->getPluginHeader(); |
|
185 | - if ( isset($pluginHeader['Version']) ) { |
|
186 | - $this->cachedInstalledVersion = $pluginHeader['Version']; |
|
187 | - return $pluginHeader['Version']; |
|
188 | - } else { |
|
189 | - //This can happen if the filename points to something that is not a plugin. |
|
190 | - $this->triggerError( |
|
191 | - sprintf( |
|
192 | - "Can't to read the Version header for '%s'. The filename is incorrect or is not a plugin.", |
|
193 | - $this->pluginFile |
|
194 | - ), |
|
195 | - E_USER_WARNING |
|
196 | - ); |
|
197 | - return null; |
|
198 | - } |
|
199 | - } |
|
200 | - |
|
201 | - /** |
|
202 | - * Get plugin's metadata from its file header. |
|
203 | - * |
|
204 | - * @return array |
|
205 | - */ |
|
206 | - protected function getPluginHeader() { |
|
207 | - if ( !is_file($this->pluginAbsolutePath) ) { |
|
208 | - //This can happen if the plugin filename is wrong. |
|
209 | - $this->triggerError( |
|
210 | - sprintf( |
|
211 | - "Can't to read the plugin header for '%s'. The file does not exist.", |
|
212 | - $this->pluginFile |
|
213 | - ), |
|
214 | - E_USER_WARNING |
|
215 | - ); |
|
216 | - return array(); |
|
217 | - } |
|
218 | - |
|
219 | - if ( !function_exists('get_plugin_data') ){ |
|
220 | - /** @noinspection PhpIncludeInspection */ |
|
221 | - require_once( ABSPATH . '/wp-admin/includes/plugin.php' ); |
|
222 | - } |
|
223 | - return get_plugin_data($this->pluginAbsolutePath, false, false); |
|
224 | - } |
|
225 | - |
|
226 | - /** |
|
227 | - * @return array |
|
228 | - */ |
|
229 | - protected function getHeaderNames() { |
|
230 | - return array( |
|
231 | - 'Name' => 'Plugin Name', |
|
232 | - 'PluginURI' => 'Plugin URI', |
|
233 | - 'Version' => 'Version', |
|
234 | - 'Description' => 'Description', |
|
235 | - 'Author' => 'Author', |
|
236 | - 'AuthorURI' => 'Author URI', |
|
237 | - 'TextDomain' => 'Text Domain', |
|
238 | - 'DomainPath' => 'Domain Path', |
|
239 | - 'Network' => 'Network', |
|
240 | - |
|
241 | - //The newest WordPress version that this plugin requires or has been tested with. |
|
242 | - //We support several different formats for compatibility with other libraries. |
|
243 | - 'Tested WP' => 'Tested WP', |
|
244 | - 'Requires WP' => 'Requires WP', |
|
245 | - 'Tested up to' => 'Tested up to', |
|
246 | - 'Requires at least' => 'Requires at least', |
|
247 | - ); |
|
248 | - } |
|
249 | - |
|
250 | - |
|
251 | - /** |
|
252 | - * Intercept plugins_api() calls that request information about our plugin and |
|
253 | - * use the configured API endpoint to satisfy them. |
|
254 | - * |
|
255 | - * @see plugins_api() |
|
256 | - * |
|
257 | - * @param mixed $result |
|
258 | - * @param string $action |
|
259 | - * @param array|object $args |
|
260 | - * @return mixed |
|
261 | - */ |
|
262 | - public function injectInfo($result, $action = null, $args = null){ |
|
263 | - $relevant = ($action == 'plugin_information') && isset($args->slug) && ( |
|
264 | - ($args->slug == $this->slug) || ($args->slug == dirname($this->pluginFile)) |
|
265 | - ); |
|
266 | - if ( !$relevant ) { |
|
267 | - return $result; |
|
268 | - } |
|
269 | - |
|
270 | - $pluginInfo = $this->requestInfo(); |
|
271 | - $pluginInfo = apply_filters($this->getUniqueName('pre_inject_info'), $pluginInfo); |
|
272 | - if ( $pluginInfo ) { |
|
273 | - return $pluginInfo->toWpFormat(); |
|
274 | - } |
|
275 | - |
|
276 | - return $result; |
|
277 | - } |
|
278 | - |
|
279 | - protected function shouldShowUpdates() { |
|
280 | - //No update notifications for mu-plugins unless explicitly enabled. The MU plugin file |
|
281 | - //is usually different from the main plugin file so the update wouldn't show up properly anyway. |
|
282 | - return !$this->isUnknownMuPlugin(); |
|
283 | - } |
|
284 | - |
|
285 | - /** |
|
286 | - * @param stdClass|null $updates |
|
287 | - * @param stdClass $updateToAdd |
|
288 | - * @return stdClass |
|
289 | - */ |
|
290 | - protected function addUpdateToList($updates, $updateToAdd) { |
|
291 | - if ( $this->isMuPlugin() ) { |
|
292 | - //WP does not support automatic update installation for mu-plugins, but we can |
|
293 | - //still display a notice. |
|
294 | - $updateToAdd->package = null; |
|
295 | - } |
|
296 | - return parent::addUpdateToList($updates, $updateToAdd); |
|
297 | - } |
|
298 | - |
|
299 | - /** |
|
300 | - * @param stdClass|null $updates |
|
301 | - * @return stdClass|null |
|
302 | - */ |
|
303 | - protected function removeUpdateFromList($updates) { |
|
304 | - $updates = parent::removeUpdateFromList($updates); |
|
305 | - if ( !empty($this->muPluginFile) && isset($updates, $updates->response) ) { |
|
306 | - unset($updates->response[$this->muPluginFile]); |
|
307 | - } |
|
308 | - return $updates; |
|
309 | - } |
|
310 | - |
|
311 | - /** |
|
312 | - * For plugins, the update array is indexed by the plugin filename relative to the "plugins" |
|
313 | - * directory. Example: "plugin-name/plugin.php". |
|
314 | - * |
|
315 | - * @return string |
|
316 | - */ |
|
317 | - protected function getUpdateListKey() { |
|
318 | - if ( $this->isMuPlugin() ) { |
|
319 | - return $this->muPluginFile; |
|
320 | - } |
|
321 | - return $this->pluginFile; |
|
322 | - } |
|
323 | - |
|
324 | - /** |
|
325 | - * Alias for isBeingUpgraded(). |
|
326 | - * |
|
327 | - * @deprecated |
|
328 | - * @param WP_Upgrader|null $upgrader The upgrader that's performing the current update. |
|
329 | - * @return bool |
|
330 | - */ |
|
331 | - public function isPluginBeingUpgraded($upgrader = null) { |
|
332 | - return $this->isBeingUpgraded($upgrader); |
|
333 | - } |
|
334 | - |
|
335 | - /** |
|
336 | - * Is there an update being installed for this plugin, right now? |
|
337 | - * |
|
338 | - * @param WP_Upgrader|null $upgrader |
|
339 | - * @return bool |
|
340 | - */ |
|
341 | - public function isBeingUpgraded($upgrader = null) { |
|
342 | - return $this->upgraderStatus->isPluginBeingUpgraded($this->pluginFile, $upgrader); |
|
343 | - } |
|
344 | - |
|
345 | - /** |
|
346 | - * Get the details of the currently available update, if any. |
|
347 | - * |
|
348 | - * If no updates are available, or if the last known update version is below or equal |
|
349 | - * to the currently installed version, this method will return NULL. |
|
350 | - * |
|
351 | - * Uses cached update data. To retrieve update information straight from |
|
352 | - * the metadata URL, call requestUpdate() instead. |
|
353 | - * |
|
354 | - * @return Puc_v4p4_Plugin_Update|null |
|
355 | - */ |
|
356 | - public function getUpdate() { |
|
357 | - $update = parent::getUpdate(); |
|
358 | - if ( isset($update) ) { |
|
359 | - /** @var Puc_v4p4_Plugin_Update $update */ |
|
360 | - $update->filename = $this->pluginFile; |
|
361 | - } |
|
362 | - return $update; |
|
363 | - } |
|
364 | - |
|
365 | - /** |
|
366 | - * Add a "Check for updates" link to the plugin row in the "Plugins" page. By default, |
|
367 | - * the new link will appear after the "Visit plugin site" link if present, otherwise |
|
368 | - * after the "View plugin details" link. |
|
369 | - * |
|
370 | - * You can change the link text by using the "puc_manual_check_link-$slug" filter. |
|
371 | - * Returning an empty string from the filter will disable the link. |
|
372 | - * |
|
373 | - * @param array $pluginMeta Array of meta links. |
|
374 | - * @param string $pluginFile |
|
375 | - * @return array |
|
376 | - */ |
|
377 | - public function addCheckForUpdatesLink($pluginMeta, $pluginFile) { |
|
378 | - $isRelevant = ($pluginFile == $this->pluginFile) |
|
379 | - || (!empty($this->muPluginFile) && $pluginFile == $this->muPluginFile); |
|
380 | - |
|
381 | - if ( $isRelevant && $this->userCanInstallUpdates() ) { |
|
382 | - $linkUrl = wp_nonce_url( |
|
383 | - add_query_arg( |
|
384 | - array( |
|
385 | - 'puc_check_for_updates' => 1, |
|
386 | - 'puc_slug' => $this->slug, |
|
387 | - ), |
|
388 | - self_admin_url('plugins.php') |
|
389 | - ), |
|
390 | - 'puc_check_for_updates' |
|
391 | - ); |
|
392 | - |
|
393 | - $linkText = apply_filters( |
|
394 | - $this->getUniqueName('manual_check_link'), |
|
395 | - __('Check for updates', 'plugin-update-checker') |
|
396 | - ); |
|
397 | - if ( !empty($linkText) ) { |
|
398 | - /** @noinspection HtmlUnknownTarget */ |
|
399 | - $pluginMeta[] = sprintf('<a href="%s">%s</a>', esc_attr($linkUrl), $linkText); |
|
400 | - } |
|
401 | - } |
|
402 | - return $pluginMeta; |
|
403 | - } |
|
404 | - |
|
405 | - /** |
|
406 | - * Add a "View Details" link to the plugin row in the "Plugins" page. By default, |
|
407 | - * the new link will appear before the "Visit plugin site" link (if present). |
|
408 | - * |
|
409 | - * You can change the link text by using the "puc_view_details_link-$slug" filter. |
|
410 | - * Returning an empty string from the filter will disable the link. |
|
411 | - * |
|
412 | - * You can change the position of the link using the |
|
413 | - * "puc_view_details_link_position-$slug" filter. |
|
414 | - * Returning 'before' or 'after' will place the link immediately before/after the |
|
415 | - * "Visit plugin site" link |
|
416 | - * Returning 'append' places the link after any existing links at the time of the hook. |
|
417 | - * Returning 'replace' replaces the "Visit plugin site" link |
|
418 | - * Returning anything else disables the link when there is a "Visit plugin site" link. |
|
419 | - * |
|
420 | - * If there is no "Visit plugin site" link 'append' is always used! |
|
421 | - * |
|
422 | - * @param array $pluginMeta Array of meta links. |
|
423 | - * @param string $pluginFile |
|
424 | - * @param array $pluginData Array of plugin header data. |
|
425 | - * @return array |
|
426 | - */ |
|
427 | - public function addViewDetailsLink($pluginMeta, $pluginFile, $pluginData = array()) { |
|
428 | - $isRelevant = ($pluginFile == $this->pluginFile) |
|
429 | - || (!empty($this->muPluginFile) && $pluginFile == $this->muPluginFile); |
|
430 | - |
|
431 | - if ( $isRelevant && $this->userCanInstallUpdates() && !isset($pluginData['slug']) ) { |
|
432 | - $linkText = apply_filters($this->getUniqueName('view_details_link'), __('View details')); |
|
433 | - if ( !empty($linkText) ) { |
|
434 | - $viewDetailsLinkPosition = 'append'; |
|
435 | - |
|
436 | - //Find the "Visit plugin site" link (if present). |
|
437 | - $visitPluginSiteLinkIndex = count($pluginMeta) - 1; |
|
438 | - if ( $pluginData['PluginURI'] ) { |
|
439 | - $escapedPluginUri = esc_url($pluginData['PluginURI']); |
|
440 | - foreach ($pluginMeta as $linkIndex => $existingLink) { |
|
441 | - if ( strpos($existingLink, $escapedPluginUri) !== false ) { |
|
442 | - $visitPluginSiteLinkIndex = $linkIndex; |
|
443 | - $viewDetailsLinkPosition = apply_filters( |
|
444 | - $this->getUniqueName('view_details_link_position'), |
|
445 | - 'before' |
|
446 | - ); |
|
447 | - break; |
|
448 | - } |
|
449 | - } |
|
450 | - } |
|
451 | - |
|
452 | - $viewDetailsLink = sprintf('<a href="%s" class="thickbox open-plugin-details-modal" aria-label="%s" data-title="%s">%s</a>', |
|
453 | - esc_url(network_admin_url('plugin-install.php?tab=plugin-information&plugin=' . urlencode($this->slug) . |
|
454 | - '&TB_iframe=true&width=600&height=550')), |
|
455 | - esc_attr(sprintf(__('More information about %s'), $pluginData['Name'])), |
|
456 | - esc_attr($pluginData['Name']), |
|
457 | - $linkText |
|
458 | - ); |
|
459 | - switch ($viewDetailsLinkPosition) { |
|
460 | - case 'before': |
|
461 | - array_splice($pluginMeta, $visitPluginSiteLinkIndex, 0, $viewDetailsLink); |
|
462 | - break; |
|
463 | - case 'after': |
|
464 | - array_splice($pluginMeta, $visitPluginSiteLinkIndex + 1, 0, $viewDetailsLink); |
|
465 | - break; |
|
466 | - case 'replace': |
|
467 | - $pluginMeta[$visitPluginSiteLinkIndex] = $viewDetailsLink; |
|
468 | - break; |
|
469 | - case 'append': |
|
470 | - default: |
|
471 | - $pluginMeta[] = $viewDetailsLink; |
|
472 | - break; |
|
473 | - } |
|
474 | - } |
|
475 | - } |
|
476 | - return $pluginMeta; |
|
477 | - } |
|
478 | - |
|
479 | - |
|
480 | - /** |
|
481 | - * Check for updates when the user clicks the "Check for updates" link. |
|
482 | - * @see self::addCheckForUpdatesLink() |
|
483 | - * |
|
484 | - * @return void |
|
485 | - */ |
|
486 | - public function handleManualCheck() { |
|
487 | - $shouldCheck = |
|
488 | - isset($_GET['puc_check_for_updates'], $_GET['puc_slug']) |
|
489 | - && $_GET['puc_slug'] == $this->slug |
|
490 | - && $this->userCanInstallUpdates() |
|
491 | - && check_admin_referer('puc_check_for_updates'); |
|
492 | - |
|
493 | - if ( $shouldCheck ) { |
|
494 | - $update = $this->checkForUpdates(); |
|
495 | - $status = ($update === null) ? 'no_update' : 'update_available'; |
|
496 | - |
|
497 | - if ( ($update === null) && !empty($this->lastRequestApiErrors) ) { |
|
498 | - //Some errors are not critical. For example, if PUC tries to retrieve the readme.txt |
|
499 | - //file from GitHub and gets a 404, that's an API error, but it doesn't prevent updates |
|
500 | - //from working. Maybe the plugin simply doesn't have a readme. |
|
501 | - //Let's only show important errors. |
|
502 | - $foundCriticalErrors = false; |
|
503 | - $questionableErrorCodes = array( |
|
504 | - 'puc-github-http-error', |
|
505 | - 'puc-gitlab-http-error', |
|
506 | - 'puc-bitbucket-http-error', |
|
507 | - ); |
|
508 | - |
|
509 | - foreach ($this->lastRequestApiErrors as $item) { |
|
510 | - $wpError = $item['error']; |
|
511 | - /** @var WP_Error $wpError */ |
|
512 | - if ( !in_array($wpError->get_error_code(), $questionableErrorCodes) ) { |
|
513 | - $foundCriticalErrors = true; |
|
514 | - break; |
|
515 | - } |
|
516 | - } |
|
517 | - |
|
518 | - if ( $foundCriticalErrors ) { |
|
519 | - $status = 'error'; |
|
520 | - set_site_transient($this->manualCheckErrorTransient, $this->lastRequestApiErrors, 60); |
|
521 | - } |
|
522 | - } |
|
523 | - |
|
524 | - wp_redirect(add_query_arg( |
|
525 | - array( |
|
526 | - 'puc_update_check_result' => $status, |
|
527 | - 'puc_slug' => $this->slug, |
|
528 | - ), |
|
529 | - self_admin_url('plugins.php') |
|
530 | - )); |
|
531 | - } |
|
532 | - } |
|
533 | - |
|
534 | - /** |
|
535 | - * Display the results of a manual update check. |
|
536 | - * @see self::handleManualCheck() |
|
537 | - * |
|
538 | - * You can change the result message by using the "puc_manual_check_message-$slug" filter. |
|
539 | - */ |
|
540 | - public function displayManualCheckResult() { |
|
541 | - if ( isset($_GET['puc_update_check_result'], $_GET['puc_slug']) && ($_GET['puc_slug'] == $this->slug) ) { |
|
542 | - $status = strval($_GET['puc_update_check_result']); |
|
543 | - $title = $this->getPluginTitle(); |
|
544 | - $noticeClass = 'updated notice-success'; |
|
545 | - $details = ''; |
|
546 | - |
|
547 | - if ( $status == 'no_update' ) { |
|
548 | - $message = sprintf(_x('The %s plugin is up to date.', 'the plugin title', 'plugin-update-checker'), $title); |
|
549 | - } else if ( $status == 'update_available' ) { |
|
550 | - $message = sprintf(_x('A new version of the %s plugin is available.', 'the plugin title', 'plugin-update-checker'), $title); |
|
551 | - } else if ( $status === 'error' ) { |
|
552 | - $message = sprintf(_x('Could not determine if updates are available for %s.', 'the plugin title', 'plugin-update-checker'), $title); |
|
553 | - $noticeClass = 'error notice-error'; |
|
554 | - |
|
555 | - $details = $this->formatManualCheckErrors(get_site_transient($this->manualCheckErrorTransient)); |
|
556 | - delete_site_transient($this->manualCheckErrorTransient); |
|
557 | - } else { |
|
558 | - $message = sprintf(__('Unknown update checker status "%s"', 'plugin-update-checker'), htmlentities($status)); |
|
559 | - $noticeClass = 'error notice-error'; |
|
560 | - } |
|
561 | - printf( |
|
562 | - '<div class="notice %s is-dismissible"><p>%s</p>%s</div>', |
|
563 | - $noticeClass, |
|
564 | - apply_filters($this->getUniqueName('manual_check_message'), $message, $status), |
|
565 | - $details |
|
566 | - ); |
|
567 | - } |
|
568 | - } |
|
569 | - |
|
570 | - /** |
|
571 | - * Format the list of errors that were thrown during an update check. |
|
572 | - * |
|
573 | - * @param array $errors |
|
574 | - * @return string |
|
575 | - */ |
|
576 | - protected function formatManualCheckErrors($errors) { |
|
577 | - if ( empty($errors) ) { |
|
578 | - return ''; |
|
579 | - } |
|
580 | - $output = ''; |
|
581 | - |
|
582 | - $showAsList = count($errors) > 1; |
|
583 | - if ( $showAsList ) { |
|
584 | - $output .= '<ol>'; |
|
585 | - $formatString = '<li>%1$s <code>%2$s</code></li>'; |
|
586 | - } else { |
|
587 | - $formatString = '<p>%1$s <code>%2$s</code></p>'; |
|
588 | - } |
|
589 | - foreach ($errors as $item) { |
|
590 | - $wpError = $item['error']; |
|
591 | - /** @var WP_Error $wpError */ |
|
592 | - $output .= sprintf( |
|
593 | - $formatString, |
|
594 | - $wpError->get_error_message(), |
|
595 | - $wpError->get_error_code() |
|
596 | - ); |
|
597 | - } |
|
598 | - if ( $showAsList ) { |
|
599 | - $output .= '</ol>'; |
|
600 | - } |
|
601 | - |
|
602 | - return $output; |
|
603 | - } |
|
604 | - |
|
605 | - /** |
|
606 | - * Get the translated plugin title. |
|
607 | - * |
|
608 | - * @return string |
|
609 | - */ |
|
610 | - protected function getPluginTitle() { |
|
611 | - $title = ''; |
|
612 | - $header = $this->getPluginHeader(); |
|
613 | - if ( $header && !empty($header['Name']) && isset($header['TextDomain']) ) { |
|
614 | - $title = translate($header['Name'], $header['TextDomain']); |
|
615 | - } |
|
616 | - return $title; |
|
617 | - } |
|
618 | - |
|
619 | - /** |
|
620 | - * Check if the current user has the required permissions to install updates. |
|
621 | - * |
|
622 | - * @return bool |
|
623 | - */ |
|
624 | - public function userCanInstallUpdates() { |
|
625 | - return current_user_can('update_plugins'); |
|
626 | - } |
|
627 | - |
|
628 | - /** |
|
629 | - * Check if the plugin file is inside the mu-plugins directory. |
|
630 | - * |
|
631 | - * @return bool |
|
632 | - */ |
|
633 | - protected function isMuPlugin() { |
|
634 | - static $cachedResult = null; |
|
635 | - |
|
636 | - if ( $cachedResult === null ) { |
|
637 | - //Convert both paths to the canonical form before comparison. |
|
638 | - $muPluginDir = realpath(WPMU_PLUGIN_DIR); |
|
639 | - $pluginPath = realpath($this->pluginAbsolutePath); |
|
640 | - |
|
641 | - $cachedResult = (strpos($pluginPath, $muPluginDir) === 0); |
|
642 | - } |
|
643 | - |
|
644 | - return $cachedResult; |
|
645 | - } |
|
646 | - |
|
647 | - /** |
|
648 | - * MU plugins are partially supported, but only when we know which file in mu-plugins |
|
649 | - * corresponds to this plugin. |
|
650 | - * |
|
651 | - * @return bool |
|
652 | - */ |
|
653 | - protected function isUnknownMuPlugin() { |
|
654 | - return empty($this->muPluginFile) && $this->isMuPlugin(); |
|
655 | - } |
|
656 | - |
|
657 | - /** |
|
658 | - * Clear the cached plugin version. This method can be set up as a filter (hook) and will |
|
659 | - * return the filter argument unmodified. |
|
660 | - * |
|
661 | - * @param mixed $filterArgument |
|
662 | - * @return mixed |
|
663 | - */ |
|
664 | - public function clearCachedVersion($filterArgument = null) { |
|
665 | - $this->cachedInstalledVersion = null; |
|
666 | - return $filterArgument; |
|
667 | - } |
|
668 | - |
|
669 | - /** |
|
670 | - * Get absolute path to the main plugin file. |
|
671 | - * |
|
672 | - * @return string |
|
673 | - */ |
|
674 | - public function getAbsolutePath() { |
|
675 | - return $this->pluginAbsolutePath; |
|
676 | - } |
|
677 | - |
|
678 | - /** |
|
679 | - * @return string |
|
680 | - */ |
|
681 | - public function getAbsoluteDirectoryPath() { |
|
682 | - return dirname($this->pluginAbsolutePath); |
|
683 | - } |
|
684 | - |
|
685 | - /** |
|
686 | - * Register a callback for filtering query arguments. |
|
687 | - * |
|
688 | - * The callback function should take one argument - an associative array of query arguments. |
|
689 | - * It should return a modified array of query arguments. |
|
690 | - * |
|
691 | - * @uses add_filter() This method is a convenience wrapper for add_filter(). |
|
692 | - * |
|
693 | - * @param callable $callback |
|
694 | - * @return void |
|
695 | - */ |
|
696 | - public function addQueryArgFilter($callback){ |
|
697 | - $this->addFilter('request_info_query_args', $callback); |
|
698 | - } |
|
699 | - |
|
700 | - /** |
|
701 | - * Register a callback for filtering arguments passed to wp_remote_get(). |
|
702 | - * |
|
703 | - * The callback function should take one argument - an associative array of arguments - |
|
704 | - * and return a modified array or arguments. See the WP documentation on wp_remote_get() |
|
705 | - * for details on what arguments are available and how they work. |
|
706 | - * |
|
707 | - * @uses add_filter() This method is a convenience wrapper for add_filter(). |
|
708 | - * |
|
709 | - * @param callable $callback |
|
710 | - * @return void |
|
711 | - */ |
|
712 | - public function addHttpRequestArgFilter($callback) { |
|
713 | - $this->addFilter('request_info_options', $callback); |
|
714 | - } |
|
715 | - |
|
716 | - /** |
|
717 | - * Register a callback for filtering the plugin info retrieved from the external API. |
|
718 | - * |
|
719 | - * The callback function should take two arguments. If the plugin info was retrieved |
|
720 | - * successfully, the first argument passed will be an instance of PluginInfo. Otherwise, |
|
721 | - * it will be NULL. The second argument will be the corresponding return value of |
|
722 | - * wp_remote_get (see WP docs for details). |
|
723 | - * |
|
724 | - * The callback function should return a new or modified instance of PluginInfo or NULL. |
|
725 | - * |
|
726 | - * @uses add_filter() This method is a convenience wrapper for add_filter(). |
|
727 | - * |
|
728 | - * @param callable $callback |
|
729 | - * @return void |
|
730 | - */ |
|
731 | - public function addResultFilter($callback) { |
|
732 | - $this->addFilter('request_info_result', $callback, 10, 2); |
|
733 | - } |
|
734 | - |
|
735 | - protected function createDebugBarExtension() { |
|
736 | - return new Puc_v4p4_DebugBar_PluginExtension($this); |
|
737 | - } |
|
738 | - } |
|
4 | + /** |
|
5 | + * A custom plugin update checker. |
|
6 | + * |
|
7 | + * @author Janis Elsts |
|
8 | + * @copyright 2016 |
|
9 | + * @access public |
|
10 | + */ |
|
11 | + class Puc_v4p4_Plugin_UpdateChecker extends Puc_v4p4_UpdateChecker { |
|
12 | + protected $updateTransient = 'update_plugins'; |
|
13 | + protected $translationType = 'plugin'; |
|
14 | + |
|
15 | + public $pluginAbsolutePath = ''; //Full path of the main plugin file. |
|
16 | + public $pluginFile = ''; //Plugin filename relative to the plugins directory. Many WP APIs use this to identify plugins. |
|
17 | + public $muPluginFile = ''; //For MU plugins, the plugin filename relative to the mu-plugins directory. |
|
18 | + |
|
19 | + private $cachedInstalledVersion = null; |
|
20 | + private $manualCheckErrorTransient = ''; |
|
21 | + |
|
22 | + /** |
|
23 | + * Class constructor. |
|
24 | + * |
|
25 | + * @param string $metadataUrl The URL of the plugin's metadata file. |
|
26 | + * @param string $pluginFile Fully qualified path to the main plugin file. |
|
27 | + * @param string $slug The plugin's 'slug'. If not specified, the filename part of $pluginFile sans '.php' will be used as the slug. |
|
28 | + * @param integer $checkPeriod How often to check for updates (in hours). Defaults to checking every 12 hours. Set to 0 to disable automatic update checks. |
|
29 | + * @param string $optionName Where to store book-keeping info about update checks. Defaults to 'external_updates-$slug'. |
|
30 | + * @param string $muPluginFile Optional. The plugin filename relative to the mu-plugins directory. |
|
31 | + */ |
|
32 | + public function __construct($metadataUrl, $pluginFile, $slug = '', $checkPeriod = 12, $optionName = '', $muPluginFile = ''){ |
|
33 | + $this->pluginAbsolutePath = $pluginFile; |
|
34 | + $this->pluginFile = plugin_basename($this->pluginAbsolutePath); |
|
35 | + $this->muPluginFile = $muPluginFile; |
|
36 | + |
|
37 | + //If no slug is specified, use the name of the main plugin file as the slug. |
|
38 | + //For example, 'my-cool-plugin/cool-plugin.php' becomes 'cool-plugin'. |
|
39 | + if ( empty($slug) ){ |
|
40 | + $slug = basename($this->pluginFile, '.php'); |
|
41 | + } |
|
42 | + |
|
43 | + //Plugin slugs must be unique. |
|
44 | + $slugCheckFilter = 'puc_is_slug_in_use-' . $this->slug; |
|
45 | + $slugUsedBy = apply_filters($slugCheckFilter, false); |
|
46 | + if ( $slugUsedBy ) { |
|
47 | + $this->triggerError(sprintf( |
|
48 | + 'Plugin slug "%s" is already in use by %s. Slugs must be unique.', |
|
49 | + htmlentities($this->slug), |
|
50 | + htmlentities($slugUsedBy) |
|
51 | + ), E_USER_ERROR); |
|
52 | + } |
|
53 | + add_filter($slugCheckFilter, array($this, 'getAbsolutePath')); |
|
54 | + |
|
55 | + //Backwards compatibility: If the plugin is a mu-plugin but no $muPluginFile is specified, assume |
|
56 | + //it's the same as $pluginFile given that it's not in a subdirectory (WP only looks in the base dir). |
|
57 | + if ( (strpbrk($this->pluginFile, '/\\') === false) && $this->isUnknownMuPlugin() ) { |
|
58 | + $this->muPluginFile = $this->pluginFile; |
|
59 | + } |
|
60 | + |
|
61 | + //To prevent a crash during plugin uninstallation, remove updater hooks when the user removes the plugin. |
|
62 | + //Details: https://github.com/YahnisElsts/plugin-update-checker/issues/138#issuecomment-335590964 |
|
63 | + add_action('uninstall_' . $this->pluginFile, array($this, 'removeHooks')); |
|
64 | + |
|
65 | + $this->manualCheckErrorTransient = $this->getUniqueName('manual_check_errors'); |
|
66 | + |
|
67 | + parent::__construct($metadataUrl, dirname($this->pluginFile), $slug, $checkPeriod, $optionName); |
|
68 | + } |
|
69 | + |
|
70 | + /** |
|
71 | + * Create an instance of the scheduler. |
|
72 | + * |
|
73 | + * @param int $checkPeriod |
|
74 | + * @return Puc_v4p4_Scheduler |
|
75 | + */ |
|
76 | + protected function createScheduler($checkPeriod) { |
|
77 | + $scheduler = new Puc_v4p4_Scheduler($this, $checkPeriod, array('load-plugins.php')); |
|
78 | + register_deactivation_hook($this->pluginFile, array($scheduler, 'removeUpdaterCron')); |
|
79 | + return $scheduler; |
|
80 | + } |
|
81 | + |
|
82 | + /** |
|
83 | + * Install the hooks required to run periodic update checks and inject update info |
|
84 | + * into WP data structures. |
|
85 | + * |
|
86 | + * @return void |
|
87 | + */ |
|
88 | + protected function installHooks(){ |
|
89 | + //Override requests for plugin information |
|
90 | + add_filter('plugins_api', array($this, 'injectInfo'), 20, 3); |
|
91 | + |
|
92 | + add_filter('plugin_row_meta', array($this, 'addViewDetailsLink'), 10, 3); |
|
93 | + add_filter('plugin_row_meta', array($this, 'addCheckForUpdatesLink'), 10, 2); |
|
94 | + add_action('admin_init', array($this, 'handleManualCheck')); |
|
95 | + add_action('all_admin_notices', array($this, 'displayManualCheckResult')); |
|
96 | + |
|
97 | + //Clear the version number cache when something - anything - is upgraded or WP clears the update cache. |
|
98 | + add_filter('upgrader_post_install', array($this, 'clearCachedVersion')); |
|
99 | + add_action('delete_site_transient_update_plugins', array($this, 'clearCachedVersion')); |
|
100 | + |
|
101 | + parent::installHooks(); |
|
102 | + } |
|
103 | + |
|
104 | + /** |
|
105 | + * Remove update checker hooks. |
|
106 | + * |
|
107 | + * The intent is to prevent a fatal error that can happen if the plugin has an uninstall |
|
108 | + * hook. During uninstallation, WP includes the main plugin file (which creates a PUC instance), |
|
109 | + * the uninstall hook runs, WP deletes the plugin files and then updates some transients. |
|
110 | + * If PUC hooks are still around at this time, they could throw an error while trying to |
|
111 | + * autoload classes from files that no longer exist. |
|
112 | + * |
|
113 | + * The "site_transient_{$transient}" filter is the main problem here, but let's also remove |
|
114 | + * most other PUC hooks to be safe. |
|
115 | + * |
|
116 | + * @internal |
|
117 | + */ |
|
118 | + public function removeHooks() { |
|
119 | + parent::removeHooks(); |
|
120 | + |
|
121 | + remove_filter('plugins_api', array($this, 'injectInfo'), 20); |
|
122 | + |
|
123 | + remove_filter('plugin_row_meta', array($this, 'addViewDetailsLink'), 10); |
|
124 | + remove_filter('plugin_row_meta', array($this, 'addCheckForUpdatesLink'), 10); |
|
125 | + remove_action('admin_init', array($this, 'handleManualCheck')); |
|
126 | + remove_action('all_admin_notices', array($this, 'displayManualCheckResult')); |
|
127 | + |
|
128 | + remove_filter('upgrader_post_install', array($this, 'clearCachedVersion')); |
|
129 | + remove_action('delete_site_transient_update_plugins', array($this, 'clearCachedVersion')); |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * Retrieve plugin info from the configured API endpoint. |
|
134 | + * |
|
135 | + * @uses wp_remote_get() |
|
136 | + * |
|
137 | + * @param array $queryArgs Additional query arguments to append to the request. Optional. |
|
138 | + * @return Puc_v4p4_Plugin_Info |
|
139 | + */ |
|
140 | + public function requestInfo($queryArgs = array()) { |
|
141 | + list($pluginInfo, $result) = $this->requestMetadata('Puc_v4p4_Plugin_Info', 'request_info', $queryArgs); |
|
142 | + |
|
143 | + if ( $pluginInfo !== null ) { |
|
144 | + /** @var Puc_v4p4_Plugin_Info $pluginInfo */ |
|
145 | + $pluginInfo->filename = $this->pluginFile; |
|
146 | + $pluginInfo->slug = $this->slug; |
|
147 | + } |
|
148 | + |
|
149 | + $pluginInfo = apply_filters($this->getUniqueName('request_info_result'), $pluginInfo, $result); |
|
150 | + return $pluginInfo; |
|
151 | + } |
|
152 | + |
|
153 | + /** |
|
154 | + * Retrieve the latest update (if any) from the configured API endpoint. |
|
155 | + * |
|
156 | + * @uses PluginUpdateChecker::requestInfo() |
|
157 | + * |
|
158 | + * @return Puc_v4p4_Update|null An instance of Plugin_Update, or NULL when no updates are available. |
|
159 | + */ |
|
160 | + public function requestUpdate() { |
|
161 | + //For the sake of simplicity, this function just calls requestInfo() |
|
162 | + //and transforms the result accordingly. |
|
163 | + $pluginInfo = $this->requestInfo(array('checking_for_updates' => '1')); |
|
164 | + if ( $pluginInfo === null ){ |
|
165 | + return null; |
|
166 | + } |
|
167 | + $update = Puc_v4p4_Plugin_Update::fromPluginInfo($pluginInfo); |
|
168 | + |
|
169 | + $update = $this->filterUpdateResult($update); |
|
170 | + |
|
171 | + return $update; |
|
172 | + } |
|
173 | + |
|
174 | + /** |
|
175 | + * Get the currently installed version of the plugin. |
|
176 | + * |
|
177 | + * @return string Version number. |
|
178 | + */ |
|
179 | + public function getInstalledVersion(){ |
|
180 | + if ( isset($this->cachedInstalledVersion) ) { |
|
181 | + return $this->cachedInstalledVersion; |
|
182 | + } |
|
183 | + |
|
184 | + $pluginHeader = $this->getPluginHeader(); |
|
185 | + if ( isset($pluginHeader['Version']) ) { |
|
186 | + $this->cachedInstalledVersion = $pluginHeader['Version']; |
|
187 | + return $pluginHeader['Version']; |
|
188 | + } else { |
|
189 | + //This can happen if the filename points to something that is not a plugin. |
|
190 | + $this->triggerError( |
|
191 | + sprintf( |
|
192 | + "Can't to read the Version header for '%s'. The filename is incorrect or is not a plugin.", |
|
193 | + $this->pluginFile |
|
194 | + ), |
|
195 | + E_USER_WARNING |
|
196 | + ); |
|
197 | + return null; |
|
198 | + } |
|
199 | + } |
|
200 | + |
|
201 | + /** |
|
202 | + * Get plugin's metadata from its file header. |
|
203 | + * |
|
204 | + * @return array |
|
205 | + */ |
|
206 | + protected function getPluginHeader() { |
|
207 | + if ( !is_file($this->pluginAbsolutePath) ) { |
|
208 | + //This can happen if the plugin filename is wrong. |
|
209 | + $this->triggerError( |
|
210 | + sprintf( |
|
211 | + "Can't to read the plugin header for '%s'. The file does not exist.", |
|
212 | + $this->pluginFile |
|
213 | + ), |
|
214 | + E_USER_WARNING |
|
215 | + ); |
|
216 | + return array(); |
|
217 | + } |
|
218 | + |
|
219 | + if ( !function_exists('get_plugin_data') ){ |
|
220 | + /** @noinspection PhpIncludeInspection */ |
|
221 | + require_once( ABSPATH . '/wp-admin/includes/plugin.php' ); |
|
222 | + } |
|
223 | + return get_plugin_data($this->pluginAbsolutePath, false, false); |
|
224 | + } |
|
225 | + |
|
226 | + /** |
|
227 | + * @return array |
|
228 | + */ |
|
229 | + protected function getHeaderNames() { |
|
230 | + return array( |
|
231 | + 'Name' => 'Plugin Name', |
|
232 | + 'PluginURI' => 'Plugin URI', |
|
233 | + 'Version' => 'Version', |
|
234 | + 'Description' => 'Description', |
|
235 | + 'Author' => 'Author', |
|
236 | + 'AuthorURI' => 'Author URI', |
|
237 | + 'TextDomain' => 'Text Domain', |
|
238 | + 'DomainPath' => 'Domain Path', |
|
239 | + 'Network' => 'Network', |
|
240 | + |
|
241 | + //The newest WordPress version that this plugin requires or has been tested with. |
|
242 | + //We support several different formats for compatibility with other libraries. |
|
243 | + 'Tested WP' => 'Tested WP', |
|
244 | + 'Requires WP' => 'Requires WP', |
|
245 | + 'Tested up to' => 'Tested up to', |
|
246 | + 'Requires at least' => 'Requires at least', |
|
247 | + ); |
|
248 | + } |
|
249 | + |
|
250 | + |
|
251 | + /** |
|
252 | + * Intercept plugins_api() calls that request information about our plugin and |
|
253 | + * use the configured API endpoint to satisfy them. |
|
254 | + * |
|
255 | + * @see plugins_api() |
|
256 | + * |
|
257 | + * @param mixed $result |
|
258 | + * @param string $action |
|
259 | + * @param array|object $args |
|
260 | + * @return mixed |
|
261 | + */ |
|
262 | + public function injectInfo($result, $action = null, $args = null){ |
|
263 | + $relevant = ($action == 'plugin_information') && isset($args->slug) && ( |
|
264 | + ($args->slug == $this->slug) || ($args->slug == dirname($this->pluginFile)) |
|
265 | + ); |
|
266 | + if ( !$relevant ) { |
|
267 | + return $result; |
|
268 | + } |
|
269 | + |
|
270 | + $pluginInfo = $this->requestInfo(); |
|
271 | + $pluginInfo = apply_filters($this->getUniqueName('pre_inject_info'), $pluginInfo); |
|
272 | + if ( $pluginInfo ) { |
|
273 | + return $pluginInfo->toWpFormat(); |
|
274 | + } |
|
275 | + |
|
276 | + return $result; |
|
277 | + } |
|
278 | + |
|
279 | + protected function shouldShowUpdates() { |
|
280 | + //No update notifications for mu-plugins unless explicitly enabled. The MU plugin file |
|
281 | + //is usually different from the main plugin file so the update wouldn't show up properly anyway. |
|
282 | + return !$this->isUnknownMuPlugin(); |
|
283 | + } |
|
284 | + |
|
285 | + /** |
|
286 | + * @param stdClass|null $updates |
|
287 | + * @param stdClass $updateToAdd |
|
288 | + * @return stdClass |
|
289 | + */ |
|
290 | + protected function addUpdateToList($updates, $updateToAdd) { |
|
291 | + if ( $this->isMuPlugin() ) { |
|
292 | + //WP does not support automatic update installation for mu-plugins, but we can |
|
293 | + //still display a notice. |
|
294 | + $updateToAdd->package = null; |
|
295 | + } |
|
296 | + return parent::addUpdateToList($updates, $updateToAdd); |
|
297 | + } |
|
298 | + |
|
299 | + /** |
|
300 | + * @param stdClass|null $updates |
|
301 | + * @return stdClass|null |
|
302 | + */ |
|
303 | + protected function removeUpdateFromList($updates) { |
|
304 | + $updates = parent::removeUpdateFromList($updates); |
|
305 | + if ( !empty($this->muPluginFile) && isset($updates, $updates->response) ) { |
|
306 | + unset($updates->response[$this->muPluginFile]); |
|
307 | + } |
|
308 | + return $updates; |
|
309 | + } |
|
310 | + |
|
311 | + /** |
|
312 | + * For plugins, the update array is indexed by the plugin filename relative to the "plugins" |
|
313 | + * directory. Example: "plugin-name/plugin.php". |
|
314 | + * |
|
315 | + * @return string |
|
316 | + */ |
|
317 | + protected function getUpdateListKey() { |
|
318 | + if ( $this->isMuPlugin() ) { |
|
319 | + return $this->muPluginFile; |
|
320 | + } |
|
321 | + return $this->pluginFile; |
|
322 | + } |
|
323 | + |
|
324 | + /** |
|
325 | + * Alias for isBeingUpgraded(). |
|
326 | + * |
|
327 | + * @deprecated |
|
328 | + * @param WP_Upgrader|null $upgrader The upgrader that's performing the current update. |
|
329 | + * @return bool |
|
330 | + */ |
|
331 | + public function isPluginBeingUpgraded($upgrader = null) { |
|
332 | + return $this->isBeingUpgraded($upgrader); |
|
333 | + } |
|
334 | + |
|
335 | + /** |
|
336 | + * Is there an update being installed for this plugin, right now? |
|
337 | + * |
|
338 | + * @param WP_Upgrader|null $upgrader |
|
339 | + * @return bool |
|
340 | + */ |
|
341 | + public function isBeingUpgraded($upgrader = null) { |
|
342 | + return $this->upgraderStatus->isPluginBeingUpgraded($this->pluginFile, $upgrader); |
|
343 | + } |
|
344 | + |
|
345 | + /** |
|
346 | + * Get the details of the currently available update, if any. |
|
347 | + * |
|
348 | + * If no updates are available, or if the last known update version is below or equal |
|
349 | + * to the currently installed version, this method will return NULL. |
|
350 | + * |
|
351 | + * Uses cached update data. To retrieve update information straight from |
|
352 | + * the metadata URL, call requestUpdate() instead. |
|
353 | + * |
|
354 | + * @return Puc_v4p4_Plugin_Update|null |
|
355 | + */ |
|
356 | + public function getUpdate() { |
|
357 | + $update = parent::getUpdate(); |
|
358 | + if ( isset($update) ) { |
|
359 | + /** @var Puc_v4p4_Plugin_Update $update */ |
|
360 | + $update->filename = $this->pluginFile; |
|
361 | + } |
|
362 | + return $update; |
|
363 | + } |
|
364 | + |
|
365 | + /** |
|
366 | + * Add a "Check for updates" link to the plugin row in the "Plugins" page. By default, |
|
367 | + * the new link will appear after the "Visit plugin site" link if present, otherwise |
|
368 | + * after the "View plugin details" link. |
|
369 | + * |
|
370 | + * You can change the link text by using the "puc_manual_check_link-$slug" filter. |
|
371 | + * Returning an empty string from the filter will disable the link. |
|
372 | + * |
|
373 | + * @param array $pluginMeta Array of meta links. |
|
374 | + * @param string $pluginFile |
|
375 | + * @return array |
|
376 | + */ |
|
377 | + public function addCheckForUpdatesLink($pluginMeta, $pluginFile) { |
|
378 | + $isRelevant = ($pluginFile == $this->pluginFile) |
|
379 | + || (!empty($this->muPluginFile) && $pluginFile == $this->muPluginFile); |
|
380 | + |
|
381 | + if ( $isRelevant && $this->userCanInstallUpdates() ) { |
|
382 | + $linkUrl = wp_nonce_url( |
|
383 | + add_query_arg( |
|
384 | + array( |
|
385 | + 'puc_check_for_updates' => 1, |
|
386 | + 'puc_slug' => $this->slug, |
|
387 | + ), |
|
388 | + self_admin_url('plugins.php') |
|
389 | + ), |
|
390 | + 'puc_check_for_updates' |
|
391 | + ); |
|
392 | + |
|
393 | + $linkText = apply_filters( |
|
394 | + $this->getUniqueName('manual_check_link'), |
|
395 | + __('Check for updates', 'plugin-update-checker') |
|
396 | + ); |
|
397 | + if ( !empty($linkText) ) { |
|
398 | + /** @noinspection HtmlUnknownTarget */ |
|
399 | + $pluginMeta[] = sprintf('<a href="%s">%s</a>', esc_attr($linkUrl), $linkText); |
|
400 | + } |
|
401 | + } |
|
402 | + return $pluginMeta; |
|
403 | + } |
|
404 | + |
|
405 | + /** |
|
406 | + * Add a "View Details" link to the plugin row in the "Plugins" page. By default, |
|
407 | + * the new link will appear before the "Visit plugin site" link (if present). |
|
408 | + * |
|
409 | + * You can change the link text by using the "puc_view_details_link-$slug" filter. |
|
410 | + * Returning an empty string from the filter will disable the link. |
|
411 | + * |
|
412 | + * You can change the position of the link using the |
|
413 | + * "puc_view_details_link_position-$slug" filter. |
|
414 | + * Returning 'before' or 'after' will place the link immediately before/after the |
|
415 | + * "Visit plugin site" link |
|
416 | + * Returning 'append' places the link after any existing links at the time of the hook. |
|
417 | + * Returning 'replace' replaces the "Visit plugin site" link |
|
418 | + * Returning anything else disables the link when there is a "Visit plugin site" link. |
|
419 | + * |
|
420 | + * If there is no "Visit plugin site" link 'append' is always used! |
|
421 | + * |
|
422 | + * @param array $pluginMeta Array of meta links. |
|
423 | + * @param string $pluginFile |
|
424 | + * @param array $pluginData Array of plugin header data. |
|
425 | + * @return array |
|
426 | + */ |
|
427 | + public function addViewDetailsLink($pluginMeta, $pluginFile, $pluginData = array()) { |
|
428 | + $isRelevant = ($pluginFile == $this->pluginFile) |
|
429 | + || (!empty($this->muPluginFile) && $pluginFile == $this->muPluginFile); |
|
430 | + |
|
431 | + if ( $isRelevant && $this->userCanInstallUpdates() && !isset($pluginData['slug']) ) { |
|
432 | + $linkText = apply_filters($this->getUniqueName('view_details_link'), __('View details')); |
|
433 | + if ( !empty($linkText) ) { |
|
434 | + $viewDetailsLinkPosition = 'append'; |
|
435 | + |
|
436 | + //Find the "Visit plugin site" link (if present). |
|
437 | + $visitPluginSiteLinkIndex = count($pluginMeta) - 1; |
|
438 | + if ( $pluginData['PluginURI'] ) { |
|
439 | + $escapedPluginUri = esc_url($pluginData['PluginURI']); |
|
440 | + foreach ($pluginMeta as $linkIndex => $existingLink) { |
|
441 | + if ( strpos($existingLink, $escapedPluginUri) !== false ) { |
|
442 | + $visitPluginSiteLinkIndex = $linkIndex; |
|
443 | + $viewDetailsLinkPosition = apply_filters( |
|
444 | + $this->getUniqueName('view_details_link_position'), |
|
445 | + 'before' |
|
446 | + ); |
|
447 | + break; |
|
448 | + } |
|
449 | + } |
|
450 | + } |
|
451 | + |
|
452 | + $viewDetailsLink = sprintf('<a href="%s" class="thickbox open-plugin-details-modal" aria-label="%s" data-title="%s">%s</a>', |
|
453 | + esc_url(network_admin_url('plugin-install.php?tab=plugin-information&plugin=' . urlencode($this->slug) . |
|
454 | + '&TB_iframe=true&width=600&height=550')), |
|
455 | + esc_attr(sprintf(__('More information about %s'), $pluginData['Name'])), |
|
456 | + esc_attr($pluginData['Name']), |
|
457 | + $linkText |
|
458 | + ); |
|
459 | + switch ($viewDetailsLinkPosition) { |
|
460 | + case 'before': |
|
461 | + array_splice($pluginMeta, $visitPluginSiteLinkIndex, 0, $viewDetailsLink); |
|
462 | + break; |
|
463 | + case 'after': |
|
464 | + array_splice($pluginMeta, $visitPluginSiteLinkIndex + 1, 0, $viewDetailsLink); |
|
465 | + break; |
|
466 | + case 'replace': |
|
467 | + $pluginMeta[$visitPluginSiteLinkIndex] = $viewDetailsLink; |
|
468 | + break; |
|
469 | + case 'append': |
|
470 | + default: |
|
471 | + $pluginMeta[] = $viewDetailsLink; |
|
472 | + break; |
|
473 | + } |
|
474 | + } |
|
475 | + } |
|
476 | + return $pluginMeta; |
|
477 | + } |
|
478 | + |
|
479 | + |
|
480 | + /** |
|
481 | + * Check for updates when the user clicks the "Check for updates" link. |
|
482 | + * @see self::addCheckForUpdatesLink() |
|
483 | + * |
|
484 | + * @return void |
|
485 | + */ |
|
486 | + public function handleManualCheck() { |
|
487 | + $shouldCheck = |
|
488 | + isset($_GET['puc_check_for_updates'], $_GET['puc_slug']) |
|
489 | + && $_GET['puc_slug'] == $this->slug |
|
490 | + && $this->userCanInstallUpdates() |
|
491 | + && check_admin_referer('puc_check_for_updates'); |
|
492 | + |
|
493 | + if ( $shouldCheck ) { |
|
494 | + $update = $this->checkForUpdates(); |
|
495 | + $status = ($update === null) ? 'no_update' : 'update_available'; |
|
496 | + |
|
497 | + if ( ($update === null) && !empty($this->lastRequestApiErrors) ) { |
|
498 | + //Some errors are not critical. For example, if PUC tries to retrieve the readme.txt |
|
499 | + //file from GitHub and gets a 404, that's an API error, but it doesn't prevent updates |
|
500 | + //from working. Maybe the plugin simply doesn't have a readme. |
|
501 | + //Let's only show important errors. |
|
502 | + $foundCriticalErrors = false; |
|
503 | + $questionableErrorCodes = array( |
|
504 | + 'puc-github-http-error', |
|
505 | + 'puc-gitlab-http-error', |
|
506 | + 'puc-bitbucket-http-error', |
|
507 | + ); |
|
508 | + |
|
509 | + foreach ($this->lastRequestApiErrors as $item) { |
|
510 | + $wpError = $item['error']; |
|
511 | + /** @var WP_Error $wpError */ |
|
512 | + if ( !in_array($wpError->get_error_code(), $questionableErrorCodes) ) { |
|
513 | + $foundCriticalErrors = true; |
|
514 | + break; |
|
515 | + } |
|
516 | + } |
|
517 | + |
|
518 | + if ( $foundCriticalErrors ) { |
|
519 | + $status = 'error'; |
|
520 | + set_site_transient($this->manualCheckErrorTransient, $this->lastRequestApiErrors, 60); |
|
521 | + } |
|
522 | + } |
|
523 | + |
|
524 | + wp_redirect(add_query_arg( |
|
525 | + array( |
|
526 | + 'puc_update_check_result' => $status, |
|
527 | + 'puc_slug' => $this->slug, |
|
528 | + ), |
|
529 | + self_admin_url('plugins.php') |
|
530 | + )); |
|
531 | + } |
|
532 | + } |
|
533 | + |
|
534 | + /** |
|
535 | + * Display the results of a manual update check. |
|
536 | + * @see self::handleManualCheck() |
|
537 | + * |
|
538 | + * You can change the result message by using the "puc_manual_check_message-$slug" filter. |
|
539 | + */ |
|
540 | + public function displayManualCheckResult() { |
|
541 | + if ( isset($_GET['puc_update_check_result'], $_GET['puc_slug']) && ($_GET['puc_slug'] == $this->slug) ) { |
|
542 | + $status = strval($_GET['puc_update_check_result']); |
|
543 | + $title = $this->getPluginTitle(); |
|
544 | + $noticeClass = 'updated notice-success'; |
|
545 | + $details = ''; |
|
546 | + |
|
547 | + if ( $status == 'no_update' ) { |
|
548 | + $message = sprintf(_x('The %s plugin is up to date.', 'the plugin title', 'plugin-update-checker'), $title); |
|
549 | + } else if ( $status == 'update_available' ) { |
|
550 | + $message = sprintf(_x('A new version of the %s plugin is available.', 'the plugin title', 'plugin-update-checker'), $title); |
|
551 | + } else if ( $status === 'error' ) { |
|
552 | + $message = sprintf(_x('Could not determine if updates are available for %s.', 'the plugin title', 'plugin-update-checker'), $title); |
|
553 | + $noticeClass = 'error notice-error'; |
|
554 | + |
|
555 | + $details = $this->formatManualCheckErrors(get_site_transient($this->manualCheckErrorTransient)); |
|
556 | + delete_site_transient($this->manualCheckErrorTransient); |
|
557 | + } else { |
|
558 | + $message = sprintf(__('Unknown update checker status "%s"', 'plugin-update-checker'), htmlentities($status)); |
|
559 | + $noticeClass = 'error notice-error'; |
|
560 | + } |
|
561 | + printf( |
|
562 | + '<div class="notice %s is-dismissible"><p>%s</p>%s</div>', |
|
563 | + $noticeClass, |
|
564 | + apply_filters($this->getUniqueName('manual_check_message'), $message, $status), |
|
565 | + $details |
|
566 | + ); |
|
567 | + } |
|
568 | + } |
|
569 | + |
|
570 | + /** |
|
571 | + * Format the list of errors that were thrown during an update check. |
|
572 | + * |
|
573 | + * @param array $errors |
|
574 | + * @return string |
|
575 | + */ |
|
576 | + protected function formatManualCheckErrors($errors) { |
|
577 | + if ( empty($errors) ) { |
|
578 | + return ''; |
|
579 | + } |
|
580 | + $output = ''; |
|
581 | + |
|
582 | + $showAsList = count($errors) > 1; |
|
583 | + if ( $showAsList ) { |
|
584 | + $output .= '<ol>'; |
|
585 | + $formatString = '<li>%1$s <code>%2$s</code></li>'; |
|
586 | + } else { |
|
587 | + $formatString = '<p>%1$s <code>%2$s</code></p>'; |
|
588 | + } |
|
589 | + foreach ($errors as $item) { |
|
590 | + $wpError = $item['error']; |
|
591 | + /** @var WP_Error $wpError */ |
|
592 | + $output .= sprintf( |
|
593 | + $formatString, |
|
594 | + $wpError->get_error_message(), |
|
595 | + $wpError->get_error_code() |
|
596 | + ); |
|
597 | + } |
|
598 | + if ( $showAsList ) { |
|
599 | + $output .= '</ol>'; |
|
600 | + } |
|
601 | + |
|
602 | + return $output; |
|
603 | + } |
|
604 | + |
|
605 | + /** |
|
606 | + * Get the translated plugin title. |
|
607 | + * |
|
608 | + * @return string |
|
609 | + */ |
|
610 | + protected function getPluginTitle() { |
|
611 | + $title = ''; |
|
612 | + $header = $this->getPluginHeader(); |
|
613 | + if ( $header && !empty($header['Name']) && isset($header['TextDomain']) ) { |
|
614 | + $title = translate($header['Name'], $header['TextDomain']); |
|
615 | + } |
|
616 | + return $title; |
|
617 | + } |
|
618 | + |
|
619 | + /** |
|
620 | + * Check if the current user has the required permissions to install updates. |
|
621 | + * |
|
622 | + * @return bool |
|
623 | + */ |
|
624 | + public function userCanInstallUpdates() { |
|
625 | + return current_user_can('update_plugins'); |
|
626 | + } |
|
627 | + |
|
628 | + /** |
|
629 | + * Check if the plugin file is inside the mu-plugins directory. |
|
630 | + * |
|
631 | + * @return bool |
|
632 | + */ |
|
633 | + protected function isMuPlugin() { |
|
634 | + static $cachedResult = null; |
|
635 | + |
|
636 | + if ( $cachedResult === null ) { |
|
637 | + //Convert both paths to the canonical form before comparison. |
|
638 | + $muPluginDir = realpath(WPMU_PLUGIN_DIR); |
|
639 | + $pluginPath = realpath($this->pluginAbsolutePath); |
|
640 | + |
|
641 | + $cachedResult = (strpos($pluginPath, $muPluginDir) === 0); |
|
642 | + } |
|
643 | + |
|
644 | + return $cachedResult; |
|
645 | + } |
|
646 | + |
|
647 | + /** |
|
648 | + * MU plugins are partially supported, but only when we know which file in mu-plugins |
|
649 | + * corresponds to this plugin. |
|
650 | + * |
|
651 | + * @return bool |
|
652 | + */ |
|
653 | + protected function isUnknownMuPlugin() { |
|
654 | + return empty($this->muPluginFile) && $this->isMuPlugin(); |
|
655 | + } |
|
656 | + |
|
657 | + /** |
|
658 | + * Clear the cached plugin version. This method can be set up as a filter (hook) and will |
|
659 | + * return the filter argument unmodified. |
|
660 | + * |
|
661 | + * @param mixed $filterArgument |
|
662 | + * @return mixed |
|
663 | + */ |
|
664 | + public function clearCachedVersion($filterArgument = null) { |
|
665 | + $this->cachedInstalledVersion = null; |
|
666 | + return $filterArgument; |
|
667 | + } |
|
668 | + |
|
669 | + /** |
|
670 | + * Get absolute path to the main plugin file. |
|
671 | + * |
|
672 | + * @return string |
|
673 | + */ |
|
674 | + public function getAbsolutePath() { |
|
675 | + return $this->pluginAbsolutePath; |
|
676 | + } |
|
677 | + |
|
678 | + /** |
|
679 | + * @return string |
|
680 | + */ |
|
681 | + public function getAbsoluteDirectoryPath() { |
|
682 | + return dirname($this->pluginAbsolutePath); |
|
683 | + } |
|
684 | + |
|
685 | + /** |
|
686 | + * Register a callback for filtering query arguments. |
|
687 | + * |
|
688 | + * The callback function should take one argument - an associative array of query arguments. |
|
689 | + * It should return a modified array of query arguments. |
|
690 | + * |
|
691 | + * @uses add_filter() This method is a convenience wrapper for add_filter(). |
|
692 | + * |
|
693 | + * @param callable $callback |
|
694 | + * @return void |
|
695 | + */ |
|
696 | + public function addQueryArgFilter($callback){ |
|
697 | + $this->addFilter('request_info_query_args', $callback); |
|
698 | + } |
|
699 | + |
|
700 | + /** |
|
701 | + * Register a callback for filtering arguments passed to wp_remote_get(). |
|
702 | + * |
|
703 | + * The callback function should take one argument - an associative array of arguments - |
|
704 | + * and return a modified array or arguments. See the WP documentation on wp_remote_get() |
|
705 | + * for details on what arguments are available and how they work. |
|
706 | + * |
|
707 | + * @uses add_filter() This method is a convenience wrapper for add_filter(). |
|
708 | + * |
|
709 | + * @param callable $callback |
|
710 | + * @return void |
|
711 | + */ |
|
712 | + public function addHttpRequestArgFilter($callback) { |
|
713 | + $this->addFilter('request_info_options', $callback); |
|
714 | + } |
|
715 | + |
|
716 | + /** |
|
717 | + * Register a callback for filtering the plugin info retrieved from the external API. |
|
718 | + * |
|
719 | + * The callback function should take two arguments. If the plugin info was retrieved |
|
720 | + * successfully, the first argument passed will be an instance of PluginInfo. Otherwise, |
|
721 | + * it will be NULL. The second argument will be the corresponding return value of |
|
722 | + * wp_remote_get (see WP docs for details). |
|
723 | + * |
|
724 | + * The callback function should return a new or modified instance of PluginInfo or NULL. |
|
725 | + * |
|
726 | + * @uses add_filter() This method is a convenience wrapper for add_filter(). |
|
727 | + * |
|
728 | + * @param callable $callback |
|
729 | + * @return void |
|
730 | + */ |
|
731 | + public function addResultFilter($callback) { |
|
732 | + $this->addFilter('request_info_result', $callback, 10, 2); |
|
733 | + } |
|
734 | + |
|
735 | + protected function createDebugBarExtension() { |
|
736 | + return new Puc_v4p4_DebugBar_PluginExtension($this); |
|
737 | + } |
|
738 | + } |
|
739 | 739 | |
740 | 740 | endif; |
@@ -1,5 +1,5 @@ discard block |
||
1 | 1 | <?php |
2 | -if ( !class_exists('Puc_v4p4_Plugin_UpdateChecker', false) ): |
|
2 | +if (!class_exists('Puc_v4p4_Plugin_UpdateChecker', false)): |
|
3 | 3 | |
4 | 4 | /** |
5 | 5 | * A custom plugin update checker. |
@@ -13,7 +13,7 @@ discard block |
||
13 | 13 | protected $translationType = 'plugin'; |
14 | 14 | |
15 | 15 | public $pluginAbsolutePath = ''; //Full path of the main plugin file. |
16 | - public $pluginFile = ''; //Plugin filename relative to the plugins directory. Many WP APIs use this to identify plugins. |
|
16 | + public $pluginFile = ''; //Plugin filename relative to the plugins directory. Many WP APIs use this to identify plugins. |
|
17 | 17 | public $muPluginFile = ''; //For MU plugins, the plugin filename relative to the mu-plugins directory. |
18 | 18 | |
19 | 19 | private $cachedInstalledVersion = null; |
@@ -29,21 +29,21 @@ discard block |
||
29 | 29 | * @param string $optionName Where to store book-keeping info about update checks. Defaults to 'external_updates-$slug'. |
30 | 30 | * @param string $muPluginFile Optional. The plugin filename relative to the mu-plugins directory. |
31 | 31 | */ |
32 | - public function __construct($metadataUrl, $pluginFile, $slug = '', $checkPeriod = 12, $optionName = '', $muPluginFile = ''){ |
|
32 | + public function __construct($metadataUrl, $pluginFile, $slug = '', $checkPeriod = 12, $optionName = '', $muPluginFile = '') { |
|
33 | 33 | $this->pluginAbsolutePath = $pluginFile; |
34 | 34 | $this->pluginFile = plugin_basename($this->pluginAbsolutePath); |
35 | 35 | $this->muPluginFile = $muPluginFile; |
36 | 36 | |
37 | 37 | //If no slug is specified, use the name of the main plugin file as the slug. |
38 | 38 | //For example, 'my-cool-plugin/cool-plugin.php' becomes 'cool-plugin'. |
39 | - if ( empty($slug) ){ |
|
39 | + if (empty($slug)) { |
|
40 | 40 | $slug = basename($this->pluginFile, '.php'); |
41 | 41 | } |
42 | 42 | |
43 | 43 | //Plugin slugs must be unique. |
44 | - $slugCheckFilter = 'puc_is_slug_in_use-' . $this->slug; |
|
44 | + $slugCheckFilter = 'puc_is_slug_in_use-'.$this->slug; |
|
45 | 45 | $slugUsedBy = apply_filters($slugCheckFilter, false); |
46 | - if ( $slugUsedBy ) { |
|
46 | + if ($slugUsedBy) { |
|
47 | 47 | $this->triggerError(sprintf( |
48 | 48 | 'Plugin slug "%s" is already in use by %s. Slugs must be unique.', |
49 | 49 | htmlentities($this->slug), |
@@ -54,13 +54,13 @@ discard block |
||
54 | 54 | |
55 | 55 | //Backwards compatibility: If the plugin is a mu-plugin but no $muPluginFile is specified, assume |
56 | 56 | //it's the same as $pluginFile given that it's not in a subdirectory (WP only looks in the base dir). |
57 | - if ( (strpbrk($this->pluginFile, '/\\') === false) && $this->isUnknownMuPlugin() ) { |
|
57 | + if ((strpbrk($this->pluginFile, '/\\') === false) && $this->isUnknownMuPlugin()) { |
|
58 | 58 | $this->muPluginFile = $this->pluginFile; |
59 | 59 | } |
60 | 60 | |
61 | 61 | //To prevent a crash during plugin uninstallation, remove updater hooks when the user removes the plugin. |
62 | 62 | //Details: https://github.com/YahnisElsts/plugin-update-checker/issues/138#issuecomment-335590964 |
63 | - add_action('uninstall_' . $this->pluginFile, array($this, 'removeHooks')); |
|
63 | + add_action('uninstall_'.$this->pluginFile, array($this, 'removeHooks')); |
|
64 | 64 | |
65 | 65 | $this->manualCheckErrorTransient = $this->getUniqueName('manual_check_errors'); |
66 | 66 | |
@@ -85,7 +85,7 @@ discard block |
||
85 | 85 | * |
86 | 86 | * @return void |
87 | 87 | */ |
88 | - protected function installHooks(){ |
|
88 | + protected function installHooks() { |
|
89 | 89 | //Override requests for plugin information |
90 | 90 | add_filter('plugins_api', array($this, 'injectInfo'), 20, 3); |
91 | 91 | |
@@ -140,7 +140,7 @@ discard block |
||
140 | 140 | public function requestInfo($queryArgs = array()) { |
141 | 141 | list($pluginInfo, $result) = $this->requestMetadata('Puc_v4p4_Plugin_Info', 'request_info', $queryArgs); |
142 | 142 | |
143 | - if ( $pluginInfo !== null ) { |
|
143 | + if ($pluginInfo !== null) { |
|
144 | 144 | /** @var Puc_v4p4_Plugin_Info $pluginInfo */ |
145 | 145 | $pluginInfo->filename = $this->pluginFile; |
146 | 146 | $pluginInfo->slug = $this->slug; |
@@ -161,7 +161,7 @@ discard block |
||
161 | 161 | //For the sake of simplicity, this function just calls requestInfo() |
162 | 162 | //and transforms the result accordingly. |
163 | 163 | $pluginInfo = $this->requestInfo(array('checking_for_updates' => '1')); |
164 | - if ( $pluginInfo === null ){ |
|
164 | + if ($pluginInfo === null) { |
|
165 | 165 | return null; |
166 | 166 | } |
167 | 167 | $update = Puc_v4p4_Plugin_Update::fromPluginInfo($pluginInfo); |
@@ -176,13 +176,13 @@ discard block |
||
176 | 176 | * |
177 | 177 | * @return string Version number. |
178 | 178 | */ |
179 | - public function getInstalledVersion(){ |
|
180 | - if ( isset($this->cachedInstalledVersion) ) { |
|
179 | + public function getInstalledVersion() { |
|
180 | + if (isset($this->cachedInstalledVersion)) { |
|
181 | 181 | return $this->cachedInstalledVersion; |
182 | 182 | } |
183 | 183 | |
184 | 184 | $pluginHeader = $this->getPluginHeader(); |
185 | - if ( isset($pluginHeader['Version']) ) { |
|
185 | + if (isset($pluginHeader['Version'])) { |
|
186 | 186 | $this->cachedInstalledVersion = $pluginHeader['Version']; |
187 | 187 | return $pluginHeader['Version']; |
188 | 188 | } else { |
@@ -204,7 +204,7 @@ discard block |
||
204 | 204 | * @return array |
205 | 205 | */ |
206 | 206 | protected function getPluginHeader() { |
207 | - if ( !is_file($this->pluginAbsolutePath) ) { |
|
207 | + if (!is_file($this->pluginAbsolutePath)) { |
|
208 | 208 | //This can happen if the plugin filename is wrong. |
209 | 209 | $this->triggerError( |
210 | 210 | sprintf( |
@@ -216,9 +216,9 @@ discard block |
||
216 | 216 | return array(); |
217 | 217 | } |
218 | 218 | |
219 | - if ( !function_exists('get_plugin_data') ){ |
|
219 | + if (!function_exists('get_plugin_data')) { |
|
220 | 220 | /** @noinspection PhpIncludeInspection */ |
221 | - require_once( ABSPATH . '/wp-admin/includes/plugin.php' ); |
|
221 | + require_once(ABSPATH.'/wp-admin/includes/plugin.php'); |
|
222 | 222 | } |
223 | 223 | return get_plugin_data($this->pluginAbsolutePath, false, false); |
224 | 224 | } |
@@ -259,17 +259,17 @@ discard block |
||
259 | 259 | * @param array|object $args |
260 | 260 | * @return mixed |
261 | 261 | */ |
262 | - public function injectInfo($result, $action = null, $args = null){ |
|
262 | + public function injectInfo($result, $action = null, $args = null) { |
|
263 | 263 | $relevant = ($action == 'plugin_information') && isset($args->slug) && ( |
264 | 264 | ($args->slug == $this->slug) || ($args->slug == dirname($this->pluginFile)) |
265 | 265 | ); |
266 | - if ( !$relevant ) { |
|
266 | + if (!$relevant) { |
|
267 | 267 | return $result; |
268 | 268 | } |
269 | 269 | |
270 | 270 | $pluginInfo = $this->requestInfo(); |
271 | 271 | $pluginInfo = apply_filters($this->getUniqueName('pre_inject_info'), $pluginInfo); |
272 | - if ( $pluginInfo ) { |
|
272 | + if ($pluginInfo) { |
|
273 | 273 | return $pluginInfo->toWpFormat(); |
274 | 274 | } |
275 | 275 | |
@@ -288,7 +288,7 @@ discard block |
||
288 | 288 | * @return stdClass |
289 | 289 | */ |
290 | 290 | protected function addUpdateToList($updates, $updateToAdd) { |
291 | - if ( $this->isMuPlugin() ) { |
|
291 | + if ($this->isMuPlugin()) { |
|
292 | 292 | //WP does not support automatic update installation for mu-plugins, but we can |
293 | 293 | //still display a notice. |
294 | 294 | $updateToAdd->package = null; |
@@ -302,7 +302,7 @@ discard block |
||
302 | 302 | */ |
303 | 303 | protected function removeUpdateFromList($updates) { |
304 | 304 | $updates = parent::removeUpdateFromList($updates); |
305 | - if ( !empty($this->muPluginFile) && isset($updates, $updates->response) ) { |
|
305 | + if (!empty($this->muPluginFile) && isset($updates, $updates->response)) { |
|
306 | 306 | unset($updates->response[$this->muPluginFile]); |
307 | 307 | } |
308 | 308 | return $updates; |
@@ -315,7 +315,7 @@ discard block |
||
315 | 315 | * @return string |
316 | 316 | */ |
317 | 317 | protected function getUpdateListKey() { |
318 | - if ( $this->isMuPlugin() ) { |
|
318 | + if ($this->isMuPlugin()) { |
|
319 | 319 | return $this->muPluginFile; |
320 | 320 | } |
321 | 321 | return $this->pluginFile; |
@@ -355,7 +355,7 @@ discard block |
||
355 | 355 | */ |
356 | 356 | public function getUpdate() { |
357 | 357 | $update = parent::getUpdate(); |
358 | - if ( isset($update) ) { |
|
358 | + if (isset($update)) { |
|
359 | 359 | /** @var Puc_v4p4_Plugin_Update $update */ |
360 | 360 | $update->filename = $this->pluginFile; |
361 | 361 | } |
@@ -378,7 +378,7 @@ discard block |
||
378 | 378 | $isRelevant = ($pluginFile == $this->pluginFile) |
379 | 379 | || (!empty($this->muPluginFile) && $pluginFile == $this->muPluginFile); |
380 | 380 | |
381 | - if ( $isRelevant && $this->userCanInstallUpdates() ) { |
|
381 | + if ($isRelevant && $this->userCanInstallUpdates()) { |
|
382 | 382 | $linkUrl = wp_nonce_url( |
383 | 383 | add_query_arg( |
384 | 384 | array( |
@@ -394,7 +394,7 @@ discard block |
||
394 | 394 | $this->getUniqueName('manual_check_link'), |
395 | 395 | __('Check for updates', 'plugin-update-checker') |
396 | 396 | ); |
397 | - if ( !empty($linkText) ) { |
|
397 | + if (!empty($linkText)) { |
|
398 | 398 | /** @noinspection HtmlUnknownTarget */ |
399 | 399 | $pluginMeta[] = sprintf('<a href="%s">%s</a>', esc_attr($linkUrl), $linkText); |
400 | 400 | } |
@@ -428,17 +428,17 @@ discard block |
||
428 | 428 | $isRelevant = ($pluginFile == $this->pluginFile) |
429 | 429 | || (!empty($this->muPluginFile) && $pluginFile == $this->muPluginFile); |
430 | 430 | |
431 | - if ( $isRelevant && $this->userCanInstallUpdates() && !isset($pluginData['slug']) ) { |
|
431 | + if ($isRelevant && $this->userCanInstallUpdates() && !isset($pluginData['slug'])) { |
|
432 | 432 | $linkText = apply_filters($this->getUniqueName('view_details_link'), __('View details')); |
433 | - if ( !empty($linkText) ) { |
|
433 | + if (!empty($linkText)) { |
|
434 | 434 | $viewDetailsLinkPosition = 'append'; |
435 | 435 | |
436 | 436 | //Find the "Visit plugin site" link (if present). |
437 | 437 | $visitPluginSiteLinkIndex = count($pluginMeta) - 1; |
438 | - if ( $pluginData['PluginURI'] ) { |
|
438 | + if ($pluginData['PluginURI']) { |
|
439 | 439 | $escapedPluginUri = esc_url($pluginData['PluginURI']); |
440 | 440 | foreach ($pluginMeta as $linkIndex => $existingLink) { |
441 | - if ( strpos($existingLink, $escapedPluginUri) !== false ) { |
|
441 | + if (strpos($existingLink, $escapedPluginUri) !== false) { |
|
442 | 442 | $visitPluginSiteLinkIndex = $linkIndex; |
443 | 443 | $viewDetailsLinkPosition = apply_filters( |
444 | 444 | $this->getUniqueName('view_details_link_position'), |
@@ -450,7 +450,7 @@ discard block |
||
450 | 450 | } |
451 | 451 | |
452 | 452 | $viewDetailsLink = sprintf('<a href="%s" class="thickbox open-plugin-details-modal" aria-label="%s" data-title="%s">%s</a>', |
453 | - esc_url(network_admin_url('plugin-install.php?tab=plugin-information&plugin=' . urlencode($this->slug) . |
|
453 | + esc_url(network_admin_url('plugin-install.php?tab=plugin-information&plugin='.urlencode($this->slug). |
|
454 | 454 | '&TB_iframe=true&width=600&height=550')), |
455 | 455 | esc_attr(sprintf(__('More information about %s'), $pluginData['Name'])), |
456 | 456 | esc_attr($pluginData['Name']), |
@@ -490,11 +490,11 @@ discard block |
||
490 | 490 | && $this->userCanInstallUpdates() |
491 | 491 | && check_admin_referer('puc_check_for_updates'); |
492 | 492 | |
493 | - if ( $shouldCheck ) { |
|
493 | + if ($shouldCheck) { |
|
494 | 494 | $update = $this->checkForUpdates(); |
495 | 495 | $status = ($update === null) ? 'no_update' : 'update_available'; |
496 | 496 | |
497 | - if ( ($update === null) && !empty($this->lastRequestApiErrors) ) { |
|
497 | + if (($update === null) && !empty($this->lastRequestApiErrors)) { |
|
498 | 498 | //Some errors are not critical. For example, if PUC tries to retrieve the readme.txt |
499 | 499 | //file from GitHub and gets a 404, that's an API error, but it doesn't prevent updates |
500 | 500 | //from working. Maybe the plugin simply doesn't have a readme. |
@@ -509,13 +509,13 @@ discard block |
||
509 | 509 | foreach ($this->lastRequestApiErrors as $item) { |
510 | 510 | $wpError = $item['error']; |
511 | 511 | /** @var WP_Error $wpError */ |
512 | - if ( !in_array($wpError->get_error_code(), $questionableErrorCodes) ) { |
|
512 | + if (!in_array($wpError->get_error_code(), $questionableErrorCodes)) { |
|
513 | 513 | $foundCriticalErrors = true; |
514 | 514 | break; |
515 | 515 | } |
516 | 516 | } |
517 | 517 | |
518 | - if ( $foundCriticalErrors ) { |
|
518 | + if ($foundCriticalErrors) { |
|
519 | 519 | $status = 'error'; |
520 | 520 | set_site_transient($this->manualCheckErrorTransient, $this->lastRequestApiErrors, 60); |
521 | 521 | } |
@@ -538,17 +538,17 @@ discard block |
||
538 | 538 | * You can change the result message by using the "puc_manual_check_message-$slug" filter. |
539 | 539 | */ |
540 | 540 | public function displayManualCheckResult() { |
541 | - if ( isset($_GET['puc_update_check_result'], $_GET['puc_slug']) && ($_GET['puc_slug'] == $this->slug) ) { |
|
541 | + if (isset($_GET['puc_update_check_result'], $_GET['puc_slug']) && ($_GET['puc_slug'] == $this->slug)) { |
|
542 | 542 | $status = strval($_GET['puc_update_check_result']); |
543 | 543 | $title = $this->getPluginTitle(); |
544 | 544 | $noticeClass = 'updated notice-success'; |
545 | 545 | $details = ''; |
546 | 546 | |
547 | - if ( $status == 'no_update' ) { |
|
547 | + if ($status == 'no_update') { |
|
548 | 548 | $message = sprintf(_x('The %s plugin is up to date.', 'the plugin title', 'plugin-update-checker'), $title); |
549 | - } else if ( $status == 'update_available' ) { |
|
549 | + } else if ($status == 'update_available') { |
|
550 | 550 | $message = sprintf(_x('A new version of the %s plugin is available.', 'the plugin title', 'plugin-update-checker'), $title); |
551 | - } else if ( $status === 'error' ) { |
|
551 | + } else if ($status === 'error') { |
|
552 | 552 | $message = sprintf(_x('Could not determine if updates are available for %s.', 'the plugin title', 'plugin-update-checker'), $title); |
553 | 553 | $noticeClass = 'error notice-error'; |
554 | 554 | |
@@ -574,13 +574,13 @@ discard block |
||
574 | 574 | * @return string |
575 | 575 | */ |
576 | 576 | protected function formatManualCheckErrors($errors) { |
577 | - if ( empty($errors) ) { |
|
577 | + if (empty($errors)) { |
|
578 | 578 | return ''; |
579 | 579 | } |
580 | 580 | $output = ''; |
581 | 581 | |
582 | 582 | $showAsList = count($errors) > 1; |
583 | - if ( $showAsList ) { |
|
583 | + if ($showAsList) { |
|
584 | 584 | $output .= '<ol>'; |
585 | 585 | $formatString = '<li>%1$s <code>%2$s</code></li>'; |
586 | 586 | } else { |
@@ -595,7 +595,7 @@ discard block |
||
595 | 595 | $wpError->get_error_code() |
596 | 596 | ); |
597 | 597 | } |
598 | - if ( $showAsList ) { |
|
598 | + if ($showAsList) { |
|
599 | 599 | $output .= '</ol>'; |
600 | 600 | } |
601 | 601 | |
@@ -610,7 +610,7 @@ discard block |
||
610 | 610 | protected function getPluginTitle() { |
611 | 611 | $title = ''; |
612 | 612 | $header = $this->getPluginHeader(); |
613 | - if ( $header && !empty($header['Name']) && isset($header['TextDomain']) ) { |
|
613 | + if ($header && !empty($header['Name']) && isset($header['TextDomain'])) { |
|
614 | 614 | $title = translate($header['Name'], $header['TextDomain']); |
615 | 615 | } |
616 | 616 | return $title; |
@@ -633,7 +633,7 @@ discard block |
||
633 | 633 | protected function isMuPlugin() { |
634 | 634 | static $cachedResult = null; |
635 | 635 | |
636 | - if ( $cachedResult === null ) { |
|
636 | + if ($cachedResult === null) { |
|
637 | 637 | //Convert both paths to the canonical form before comparison. |
638 | 638 | $muPluginDir = realpath(WPMU_PLUGIN_DIR); |
639 | 639 | $pluginPath = realpath($this->pluginAbsolutePath); |
@@ -693,7 +693,7 @@ discard block |
||
693 | 693 | * @param callable $callback |
694 | 694 | * @return void |
695 | 695 | */ |
696 | - public function addQueryArgFilter($callback){ |
|
696 | + public function addQueryArgFilter($callback) { |
|
697 | 697 | $this->addFilter('request_info_query_args', $callback); |
698 | 698 | } |
699 | 699 |
@@ -50,7 +50,7 @@ |
||
50 | 50 | * |
51 | 51 | * @param string $type |
52 | 52 | * @param string $id |
53 | - * @param Plugin_Upgrader|WP_Upgrader|null $upgrader |
|
53 | + * @param WP_Upgrader|null $upgrader |
|
54 | 54 | * @return bool |
55 | 55 | */ |
56 | 56 | protected function isBeingUpgraded($type, $id, $upgrader = null) { |
@@ -1,199 +1,199 @@ |
||
1 | 1 | <?php |
2 | 2 | if ( !class_exists('Puc_v4p4_UpgraderStatus', false) ): |
3 | 3 | |
4 | - /** |
|
5 | - * A utility class that helps figure out which plugin or theme WordPress is upgrading. |
|
6 | - * |
|
7 | - * It may seem strange to have a separate class just for that, but the task is surprisingly complicated. |
|
8 | - * Core classes like Plugin_Upgrader don't expose the plugin file name during an in-progress update (AFAICT). |
|
9 | - * This class uses a few workarounds and heuristics to get the file name. |
|
10 | - */ |
|
11 | - class Puc_v4p4_UpgraderStatus { |
|
12 | - private $currentType = null; //"plugin" or "theme". |
|
13 | - private $currentId = null; //Plugin basename or theme directory name. |
|
14 | - |
|
15 | - public function __construct() { |
|
16 | - //Keep track of which plugin/theme WordPress is currently upgrading. |
|
17 | - add_filter('upgrader_pre_install', array($this, 'setUpgradedThing'), 10, 2); |
|
18 | - add_filter('upgrader_package_options', array($this, 'setUpgradedPluginFromOptions'), 10, 1); |
|
19 | - add_filter('upgrader_post_install', array($this, 'clearUpgradedThing'), 10, 1); |
|
20 | - add_action('upgrader_process_complete', array($this, 'clearUpgradedThing'), 10, 1); |
|
21 | - } |
|
22 | - |
|
23 | - /** |
|
24 | - * Is there and update being installed RIGHT NOW, for a specific plugin? |
|
25 | - * |
|
26 | - * Caution: This method is unreliable. WordPress doesn't make it easy to figure out what it is upgrading, |
|
27 | - * and upgrader implementations are liable to change without notice. |
|
28 | - * |
|
29 | - * @param string $pluginFile The plugin to check. |
|
30 | - * @param WP_Upgrader|null $upgrader The upgrader that's performing the current update. |
|
31 | - * @return bool True if the plugin identified by $pluginFile is being upgraded. |
|
32 | - */ |
|
33 | - public function isPluginBeingUpgraded($pluginFile, $upgrader = null) { |
|
34 | - return $this->isBeingUpgraded('plugin', $pluginFile, $upgrader); |
|
35 | - } |
|
36 | - |
|
37 | - /** |
|
38 | - * Is there an update being installed for a specific theme? |
|
39 | - * |
|
40 | - * @param string $stylesheet Theme directory name. |
|
41 | - * @param WP_Upgrader|null $upgrader The upgrader that's performing the current update. |
|
42 | - * @return bool |
|
43 | - */ |
|
44 | - public function isThemeBeingUpgraded($stylesheet, $upgrader = null) { |
|
45 | - return $this->isBeingUpgraded('theme', $stylesheet, $upgrader); |
|
46 | - } |
|
47 | - |
|
48 | - /** |
|
49 | - * Check if a specific theme or plugin is being upgraded. |
|
50 | - * |
|
51 | - * @param string $type |
|
52 | - * @param string $id |
|
53 | - * @param Plugin_Upgrader|WP_Upgrader|null $upgrader |
|
54 | - * @return bool |
|
55 | - */ |
|
56 | - protected function isBeingUpgraded($type, $id, $upgrader = null) { |
|
57 | - if ( isset($upgrader) ) { |
|
58 | - list($currentType, $currentId) = $this->getThingBeingUpgradedBy($upgrader); |
|
59 | - if ( $currentType !== null ) { |
|
60 | - $this->currentType = $currentType; |
|
61 | - $this->currentId = $currentId; |
|
62 | - } |
|
63 | - } |
|
64 | - return ($this->currentType === $type) && ($this->currentId === $id); |
|
65 | - } |
|
66 | - |
|
67 | - /** |
|
68 | - * Figure out which theme or plugin is being upgraded by a WP_Upgrader instance. |
|
69 | - * |
|
70 | - * Returns an array with two items. The first item is the type of the thing that's being |
|
71 | - * upgraded: "plugin" or "theme". The second item is either the plugin basename or |
|
72 | - * the theme directory name. If we can't determine what the upgrader is doing, both items |
|
73 | - * will be NULL. |
|
74 | - * |
|
75 | - * Examples: |
|
76 | - * ['plugin', 'plugin-dir-name/plugin.php'] |
|
77 | - * ['theme', 'theme-dir-name'] |
|
78 | - * |
|
79 | - * @param Plugin_Upgrader|WP_Upgrader $upgrader |
|
80 | - * @return array |
|
81 | - */ |
|
82 | - private function getThingBeingUpgradedBy($upgrader) { |
|
83 | - if ( !isset($upgrader, $upgrader->skin) ) { |
|
84 | - return array(null, null); |
|
85 | - } |
|
86 | - |
|
87 | - //Figure out which plugin or theme is being upgraded. |
|
88 | - $pluginFile = null; |
|
89 | - $themeDirectoryName = null; |
|
90 | - |
|
91 | - $skin = $upgrader->skin; |
|
92 | - if ( isset($skin->theme_info) && ($skin->theme_info instanceof WP_Theme) ) { |
|
93 | - $themeDirectoryName = $skin->theme_info->get_stylesheet(); |
|
94 | - } elseif ( $skin instanceof Plugin_Upgrader_Skin ) { |
|
95 | - if ( isset($skin->plugin) && is_string($skin->plugin) && ($skin->plugin !== '') ) { |
|
96 | - $pluginFile = $skin->plugin; |
|
97 | - } |
|
98 | - } elseif ( $skin instanceof Theme_Upgrader_Skin ) { |
|
99 | - if ( isset($skin->theme) && is_string($skin->theme) && ($skin->theme !== '') ) { |
|
100 | - $themeDirectoryName = $skin->theme; |
|
101 | - } |
|
102 | - } elseif ( isset($skin->plugin_info) && is_array($skin->plugin_info) ) { |
|
103 | - //This case is tricky because Bulk_Plugin_Upgrader_Skin (etc) doesn't actually store the plugin |
|
104 | - //filename anywhere. Instead, it has the plugin headers in $plugin_info. So the best we can |
|
105 | - //do is compare those headers to the headers of installed plugins. |
|
106 | - $pluginFile = $this->identifyPluginByHeaders($skin->plugin_info); |
|
107 | - } |
|
108 | - |
|
109 | - if ( $pluginFile !== null ) { |
|
110 | - return array('plugin', $pluginFile); |
|
111 | - } elseif ( $themeDirectoryName !== null ) { |
|
112 | - return array('theme', $themeDirectoryName); |
|
113 | - } |
|
114 | - return array(null, null); |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * Identify an installed plugin based on its headers. |
|
119 | - * |
|
120 | - * @param array $searchHeaders The plugin file header to look for. |
|
121 | - * @return string|null Plugin basename ("foo/bar.php"), or NULL if we can't identify the plugin. |
|
122 | - */ |
|
123 | - private function identifyPluginByHeaders($searchHeaders) { |
|
124 | - if ( !function_exists('get_plugins') ){ |
|
125 | - /** @noinspection PhpIncludeInspection */ |
|
126 | - require_once( ABSPATH . '/wp-admin/includes/plugin.php' ); |
|
127 | - } |
|
128 | - |
|
129 | - $installedPlugins = get_plugins(); |
|
130 | - $matches = array(); |
|
131 | - foreach($installedPlugins as $pluginBasename => $headers) { |
|
132 | - $diff1 = array_diff_assoc($headers, $searchHeaders); |
|
133 | - $diff2 = array_diff_assoc($searchHeaders, $headers); |
|
134 | - if ( empty($diff1) && empty($diff2) ) { |
|
135 | - $matches[] = $pluginBasename; |
|
136 | - } |
|
137 | - } |
|
138 | - |
|
139 | - //It's possible (though very unlikely) that there could be two plugins with identical |
|
140 | - //headers. In that case, we can't unambiguously identify the plugin that's being upgraded. |
|
141 | - if ( count($matches) !== 1 ) { |
|
142 | - return null; |
|
143 | - } |
|
144 | - |
|
145 | - return reset($matches); |
|
146 | - } |
|
147 | - |
|
148 | - /** |
|
149 | - * @access private |
|
150 | - * |
|
151 | - * @param mixed $input |
|
152 | - * @param array $hookExtra |
|
153 | - * @return mixed Returns $input unaltered. |
|
154 | - */ |
|
155 | - public function setUpgradedThing($input, $hookExtra) { |
|
156 | - if ( !empty($hookExtra['plugin']) && is_string($hookExtra['plugin']) ) { |
|
157 | - $this->currentId = $hookExtra['plugin']; |
|
158 | - $this->currentType = 'plugin'; |
|
159 | - } elseif ( !empty($hookExtra['theme']) && is_string($hookExtra['theme']) ) { |
|
160 | - $this->currentId = $hookExtra['theme']; |
|
161 | - $this->currentType = 'theme'; |
|
162 | - } else { |
|
163 | - $this->currentType = null; |
|
164 | - $this->currentId = null; |
|
165 | - } |
|
166 | - return $input; |
|
167 | - } |
|
168 | - |
|
169 | - /** |
|
170 | - * @access private |
|
171 | - * |
|
172 | - * @param array $options |
|
173 | - * @return array |
|
174 | - */ |
|
175 | - public function setUpgradedPluginFromOptions($options) { |
|
176 | - if ( isset($options['hook_extra']['plugin']) && is_string($options['hook_extra']['plugin']) ) { |
|
177 | - $this->currentType = 'plugin'; |
|
178 | - $this->currentId = $options['hook_extra']['plugin']; |
|
179 | - } else { |
|
180 | - $this->currentType = null; |
|
181 | - $this->currentId = null; |
|
182 | - } |
|
183 | - return $options; |
|
184 | - } |
|
185 | - |
|
186 | - /** |
|
187 | - * @access private |
|
188 | - * |
|
189 | - * @param mixed $input |
|
190 | - * @return mixed Returns $input unaltered. |
|
191 | - */ |
|
192 | - public function clearUpgradedThing($input = null) { |
|
193 | - $this->currentId = null; |
|
194 | - $this->currentType = null; |
|
195 | - return $input; |
|
196 | - } |
|
197 | - } |
|
4 | + /** |
|
5 | + * A utility class that helps figure out which plugin or theme WordPress is upgrading. |
|
6 | + * |
|
7 | + * It may seem strange to have a separate class just for that, but the task is surprisingly complicated. |
|
8 | + * Core classes like Plugin_Upgrader don't expose the plugin file name during an in-progress update (AFAICT). |
|
9 | + * This class uses a few workarounds and heuristics to get the file name. |
|
10 | + */ |
|
11 | + class Puc_v4p4_UpgraderStatus { |
|
12 | + private $currentType = null; //"plugin" or "theme". |
|
13 | + private $currentId = null; //Plugin basename or theme directory name. |
|
14 | + |
|
15 | + public function __construct() { |
|
16 | + //Keep track of which plugin/theme WordPress is currently upgrading. |
|
17 | + add_filter('upgrader_pre_install', array($this, 'setUpgradedThing'), 10, 2); |
|
18 | + add_filter('upgrader_package_options', array($this, 'setUpgradedPluginFromOptions'), 10, 1); |
|
19 | + add_filter('upgrader_post_install', array($this, 'clearUpgradedThing'), 10, 1); |
|
20 | + add_action('upgrader_process_complete', array($this, 'clearUpgradedThing'), 10, 1); |
|
21 | + } |
|
22 | + |
|
23 | + /** |
|
24 | + * Is there and update being installed RIGHT NOW, for a specific plugin? |
|
25 | + * |
|
26 | + * Caution: This method is unreliable. WordPress doesn't make it easy to figure out what it is upgrading, |
|
27 | + * and upgrader implementations are liable to change without notice. |
|
28 | + * |
|
29 | + * @param string $pluginFile The plugin to check. |
|
30 | + * @param WP_Upgrader|null $upgrader The upgrader that's performing the current update. |
|
31 | + * @return bool True if the plugin identified by $pluginFile is being upgraded. |
|
32 | + */ |
|
33 | + public function isPluginBeingUpgraded($pluginFile, $upgrader = null) { |
|
34 | + return $this->isBeingUpgraded('plugin', $pluginFile, $upgrader); |
|
35 | + } |
|
36 | + |
|
37 | + /** |
|
38 | + * Is there an update being installed for a specific theme? |
|
39 | + * |
|
40 | + * @param string $stylesheet Theme directory name. |
|
41 | + * @param WP_Upgrader|null $upgrader The upgrader that's performing the current update. |
|
42 | + * @return bool |
|
43 | + */ |
|
44 | + public function isThemeBeingUpgraded($stylesheet, $upgrader = null) { |
|
45 | + return $this->isBeingUpgraded('theme', $stylesheet, $upgrader); |
|
46 | + } |
|
47 | + |
|
48 | + /** |
|
49 | + * Check if a specific theme or plugin is being upgraded. |
|
50 | + * |
|
51 | + * @param string $type |
|
52 | + * @param string $id |
|
53 | + * @param Plugin_Upgrader|WP_Upgrader|null $upgrader |
|
54 | + * @return bool |
|
55 | + */ |
|
56 | + protected function isBeingUpgraded($type, $id, $upgrader = null) { |
|
57 | + if ( isset($upgrader) ) { |
|
58 | + list($currentType, $currentId) = $this->getThingBeingUpgradedBy($upgrader); |
|
59 | + if ( $currentType !== null ) { |
|
60 | + $this->currentType = $currentType; |
|
61 | + $this->currentId = $currentId; |
|
62 | + } |
|
63 | + } |
|
64 | + return ($this->currentType === $type) && ($this->currentId === $id); |
|
65 | + } |
|
66 | + |
|
67 | + /** |
|
68 | + * Figure out which theme or plugin is being upgraded by a WP_Upgrader instance. |
|
69 | + * |
|
70 | + * Returns an array with two items. The first item is the type of the thing that's being |
|
71 | + * upgraded: "plugin" or "theme". The second item is either the plugin basename or |
|
72 | + * the theme directory name. If we can't determine what the upgrader is doing, both items |
|
73 | + * will be NULL. |
|
74 | + * |
|
75 | + * Examples: |
|
76 | + * ['plugin', 'plugin-dir-name/plugin.php'] |
|
77 | + * ['theme', 'theme-dir-name'] |
|
78 | + * |
|
79 | + * @param Plugin_Upgrader|WP_Upgrader $upgrader |
|
80 | + * @return array |
|
81 | + */ |
|
82 | + private function getThingBeingUpgradedBy($upgrader) { |
|
83 | + if ( !isset($upgrader, $upgrader->skin) ) { |
|
84 | + return array(null, null); |
|
85 | + } |
|
86 | + |
|
87 | + //Figure out which plugin or theme is being upgraded. |
|
88 | + $pluginFile = null; |
|
89 | + $themeDirectoryName = null; |
|
90 | + |
|
91 | + $skin = $upgrader->skin; |
|
92 | + if ( isset($skin->theme_info) && ($skin->theme_info instanceof WP_Theme) ) { |
|
93 | + $themeDirectoryName = $skin->theme_info->get_stylesheet(); |
|
94 | + } elseif ( $skin instanceof Plugin_Upgrader_Skin ) { |
|
95 | + if ( isset($skin->plugin) && is_string($skin->plugin) && ($skin->plugin !== '') ) { |
|
96 | + $pluginFile = $skin->plugin; |
|
97 | + } |
|
98 | + } elseif ( $skin instanceof Theme_Upgrader_Skin ) { |
|
99 | + if ( isset($skin->theme) && is_string($skin->theme) && ($skin->theme !== '') ) { |
|
100 | + $themeDirectoryName = $skin->theme; |
|
101 | + } |
|
102 | + } elseif ( isset($skin->plugin_info) && is_array($skin->plugin_info) ) { |
|
103 | + //This case is tricky because Bulk_Plugin_Upgrader_Skin (etc) doesn't actually store the plugin |
|
104 | + //filename anywhere. Instead, it has the plugin headers in $plugin_info. So the best we can |
|
105 | + //do is compare those headers to the headers of installed plugins. |
|
106 | + $pluginFile = $this->identifyPluginByHeaders($skin->plugin_info); |
|
107 | + } |
|
108 | + |
|
109 | + if ( $pluginFile !== null ) { |
|
110 | + return array('plugin', $pluginFile); |
|
111 | + } elseif ( $themeDirectoryName !== null ) { |
|
112 | + return array('theme', $themeDirectoryName); |
|
113 | + } |
|
114 | + return array(null, null); |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * Identify an installed plugin based on its headers. |
|
119 | + * |
|
120 | + * @param array $searchHeaders The plugin file header to look for. |
|
121 | + * @return string|null Plugin basename ("foo/bar.php"), or NULL if we can't identify the plugin. |
|
122 | + */ |
|
123 | + private function identifyPluginByHeaders($searchHeaders) { |
|
124 | + if ( !function_exists('get_plugins') ){ |
|
125 | + /** @noinspection PhpIncludeInspection */ |
|
126 | + require_once( ABSPATH . '/wp-admin/includes/plugin.php' ); |
|
127 | + } |
|
128 | + |
|
129 | + $installedPlugins = get_plugins(); |
|
130 | + $matches = array(); |
|
131 | + foreach($installedPlugins as $pluginBasename => $headers) { |
|
132 | + $diff1 = array_diff_assoc($headers, $searchHeaders); |
|
133 | + $diff2 = array_diff_assoc($searchHeaders, $headers); |
|
134 | + if ( empty($diff1) && empty($diff2) ) { |
|
135 | + $matches[] = $pluginBasename; |
|
136 | + } |
|
137 | + } |
|
138 | + |
|
139 | + //It's possible (though very unlikely) that there could be two plugins with identical |
|
140 | + //headers. In that case, we can't unambiguously identify the plugin that's being upgraded. |
|
141 | + if ( count($matches) !== 1 ) { |
|
142 | + return null; |
|
143 | + } |
|
144 | + |
|
145 | + return reset($matches); |
|
146 | + } |
|
147 | + |
|
148 | + /** |
|
149 | + * @access private |
|
150 | + * |
|
151 | + * @param mixed $input |
|
152 | + * @param array $hookExtra |
|
153 | + * @return mixed Returns $input unaltered. |
|
154 | + */ |
|
155 | + public function setUpgradedThing($input, $hookExtra) { |
|
156 | + if ( !empty($hookExtra['plugin']) && is_string($hookExtra['plugin']) ) { |
|
157 | + $this->currentId = $hookExtra['plugin']; |
|
158 | + $this->currentType = 'plugin'; |
|
159 | + } elseif ( !empty($hookExtra['theme']) && is_string($hookExtra['theme']) ) { |
|
160 | + $this->currentId = $hookExtra['theme']; |
|
161 | + $this->currentType = 'theme'; |
|
162 | + } else { |
|
163 | + $this->currentType = null; |
|
164 | + $this->currentId = null; |
|
165 | + } |
|
166 | + return $input; |
|
167 | + } |
|
168 | + |
|
169 | + /** |
|
170 | + * @access private |
|
171 | + * |
|
172 | + * @param array $options |
|
173 | + * @return array |
|
174 | + */ |
|
175 | + public function setUpgradedPluginFromOptions($options) { |
|
176 | + if ( isset($options['hook_extra']['plugin']) && is_string($options['hook_extra']['plugin']) ) { |
|
177 | + $this->currentType = 'plugin'; |
|
178 | + $this->currentId = $options['hook_extra']['plugin']; |
|
179 | + } else { |
|
180 | + $this->currentType = null; |
|
181 | + $this->currentId = null; |
|
182 | + } |
|
183 | + return $options; |
|
184 | + } |
|
185 | + |
|
186 | + /** |
|
187 | + * @access private |
|
188 | + * |
|
189 | + * @param mixed $input |
|
190 | + * @return mixed Returns $input unaltered. |
|
191 | + */ |
|
192 | + public function clearUpgradedThing($input = null) { |
|
193 | + $this->currentId = null; |
|
194 | + $this->currentType = null; |
|
195 | + return $input; |
|
196 | + } |
|
197 | + } |
|
198 | 198 | |
199 | 199 | endif; |
200 | 200 | \ No newline at end of file |
@@ -1,5 +1,5 @@ discard block |
||
1 | 1 | <?php |
2 | -if ( !class_exists('Puc_v4p4_UpgraderStatus', false) ): |
|
2 | +if (!class_exists('Puc_v4p4_UpgraderStatus', false)): |
|
3 | 3 | |
4 | 4 | /** |
5 | 5 | * A utility class that helps figure out which plugin or theme WordPress is upgrading. |
@@ -10,7 +10,7 @@ discard block |
||
10 | 10 | */ |
11 | 11 | class Puc_v4p4_UpgraderStatus { |
12 | 12 | private $currentType = null; //"plugin" or "theme". |
13 | - private $currentId = null; //Plugin basename or theme directory name. |
|
13 | + private $currentId = null; //Plugin basename or theme directory name. |
|
14 | 14 | |
15 | 15 | public function __construct() { |
16 | 16 | //Keep track of which plugin/theme WordPress is currently upgrading. |
@@ -54,9 +54,9 @@ discard block |
||
54 | 54 | * @return bool |
55 | 55 | */ |
56 | 56 | protected function isBeingUpgraded($type, $id, $upgrader = null) { |
57 | - if ( isset($upgrader) ) { |
|
57 | + if (isset($upgrader)) { |
|
58 | 58 | list($currentType, $currentId) = $this->getThingBeingUpgradedBy($upgrader); |
59 | - if ( $currentType !== null ) { |
|
59 | + if ($currentType !== null) { |
|
60 | 60 | $this->currentType = $currentType; |
61 | 61 | $this->currentId = $currentId; |
62 | 62 | } |
@@ -80,7 +80,7 @@ discard block |
||
80 | 80 | * @return array |
81 | 81 | */ |
82 | 82 | private function getThingBeingUpgradedBy($upgrader) { |
83 | - if ( !isset($upgrader, $upgrader->skin) ) { |
|
83 | + if (!isset($upgrader, $upgrader->skin)) { |
|
84 | 84 | return array(null, null); |
85 | 85 | } |
86 | 86 | |
@@ -89,26 +89,26 @@ discard block |
||
89 | 89 | $themeDirectoryName = null; |
90 | 90 | |
91 | 91 | $skin = $upgrader->skin; |
92 | - if ( isset($skin->theme_info) && ($skin->theme_info instanceof WP_Theme) ) { |
|
92 | + if (isset($skin->theme_info) && ($skin->theme_info instanceof WP_Theme)) { |
|
93 | 93 | $themeDirectoryName = $skin->theme_info->get_stylesheet(); |
94 | - } elseif ( $skin instanceof Plugin_Upgrader_Skin ) { |
|
95 | - if ( isset($skin->plugin) && is_string($skin->plugin) && ($skin->plugin !== '') ) { |
|
94 | + } elseif ($skin instanceof Plugin_Upgrader_Skin) { |
|
95 | + if (isset($skin->plugin) && is_string($skin->plugin) && ($skin->plugin !== '')) { |
|
96 | 96 | $pluginFile = $skin->plugin; |
97 | 97 | } |
98 | - } elseif ( $skin instanceof Theme_Upgrader_Skin ) { |
|
99 | - if ( isset($skin->theme) && is_string($skin->theme) && ($skin->theme !== '') ) { |
|
98 | + } elseif ($skin instanceof Theme_Upgrader_Skin) { |
|
99 | + if (isset($skin->theme) && is_string($skin->theme) && ($skin->theme !== '')) { |
|
100 | 100 | $themeDirectoryName = $skin->theme; |
101 | 101 | } |
102 | - } elseif ( isset($skin->plugin_info) && is_array($skin->plugin_info) ) { |
|
102 | + } elseif (isset($skin->plugin_info) && is_array($skin->plugin_info)) { |
|
103 | 103 | //This case is tricky because Bulk_Plugin_Upgrader_Skin (etc) doesn't actually store the plugin |
104 | 104 | //filename anywhere. Instead, it has the plugin headers in $plugin_info. So the best we can |
105 | 105 | //do is compare those headers to the headers of installed plugins. |
106 | 106 | $pluginFile = $this->identifyPluginByHeaders($skin->plugin_info); |
107 | 107 | } |
108 | 108 | |
109 | - if ( $pluginFile !== null ) { |
|
109 | + if ($pluginFile !== null) { |
|
110 | 110 | return array('plugin', $pluginFile); |
111 | - } elseif ( $themeDirectoryName !== null ) { |
|
111 | + } elseif ($themeDirectoryName !== null) { |
|
112 | 112 | return array('theme', $themeDirectoryName); |
113 | 113 | } |
114 | 114 | return array(null, null); |
@@ -121,24 +121,24 @@ discard block |
||
121 | 121 | * @return string|null Plugin basename ("foo/bar.php"), or NULL if we can't identify the plugin. |
122 | 122 | */ |
123 | 123 | private function identifyPluginByHeaders($searchHeaders) { |
124 | - if ( !function_exists('get_plugins') ){ |
|
124 | + if (!function_exists('get_plugins')) { |
|
125 | 125 | /** @noinspection PhpIncludeInspection */ |
126 | - require_once( ABSPATH . '/wp-admin/includes/plugin.php' ); |
|
126 | + require_once(ABSPATH.'/wp-admin/includes/plugin.php'); |
|
127 | 127 | } |
128 | 128 | |
129 | 129 | $installedPlugins = get_plugins(); |
130 | 130 | $matches = array(); |
131 | - foreach($installedPlugins as $pluginBasename => $headers) { |
|
131 | + foreach ($installedPlugins as $pluginBasename => $headers) { |
|
132 | 132 | $diff1 = array_diff_assoc($headers, $searchHeaders); |
133 | 133 | $diff2 = array_diff_assoc($searchHeaders, $headers); |
134 | - if ( empty($diff1) && empty($diff2) ) { |
|
134 | + if (empty($diff1) && empty($diff2)) { |
|
135 | 135 | $matches[] = $pluginBasename; |
136 | 136 | } |
137 | 137 | } |
138 | 138 | |
139 | 139 | //It's possible (though very unlikely) that there could be two plugins with identical |
140 | 140 | //headers. In that case, we can't unambiguously identify the plugin that's being upgraded. |
141 | - if ( count($matches) !== 1 ) { |
|
141 | + if (count($matches) !== 1) { |
|
142 | 142 | return null; |
143 | 143 | } |
144 | 144 | |
@@ -153,10 +153,10 @@ discard block |
||
153 | 153 | * @return mixed Returns $input unaltered. |
154 | 154 | */ |
155 | 155 | public function setUpgradedThing($input, $hookExtra) { |
156 | - if ( !empty($hookExtra['plugin']) && is_string($hookExtra['plugin']) ) { |
|
156 | + if (!empty($hookExtra['plugin']) && is_string($hookExtra['plugin'])) { |
|
157 | 157 | $this->currentId = $hookExtra['plugin']; |
158 | 158 | $this->currentType = 'plugin'; |
159 | - } elseif ( !empty($hookExtra['theme']) && is_string($hookExtra['theme']) ) { |
|
159 | + } elseif (!empty($hookExtra['theme']) && is_string($hookExtra['theme'])) { |
|
160 | 160 | $this->currentId = $hookExtra['theme']; |
161 | 161 | $this->currentType = 'theme'; |
162 | 162 | } else { |
@@ -173,7 +173,7 @@ discard block |
||
173 | 173 | * @return array |
174 | 174 | */ |
175 | 175 | public function setUpgradedPluginFromOptions($options) { |
176 | - if ( isset($options['hook_extra']['plugin']) && is_string($options['hook_extra']['plugin']) ) { |
|
176 | + if (isset($options['hook_extra']['plugin']) && is_string($options['hook_extra']['plugin'])) { |
|
177 | 177 | $this->currentType = 'plugin'; |
178 | 178 | $this->currentId = $options['hook_extra']['plugin']; |
179 | 179 | } else { |
@@ -204,6 +204,9 @@ discard block |
||
204 | 204 | return $r; |
205 | 205 | } |
206 | 206 | |
207 | + /** |
|
208 | + * @param string $string |
|
209 | + */ |
|
207 | 210 | function chop_string( $string, $chop ) { // chop a "prefix" from a string: Agressive! uses strstr not 0 === strpos |
208 | 211 | if ( $_string = strstr($string, $chop) ) { |
209 | 212 | $_string = substr($_string, strlen($chop)); |
@@ -213,6 +216,9 @@ discard block |
||
213 | 216 | } |
214 | 217 | } |
215 | 218 | |
219 | + /** |
|
220 | + * @return string |
|
221 | + */ |
|
216 | 222 | function user_sanitize( $text, $strict = false ) { // whitelisted chars |
217 | 223 | if ( function_exists('user_sanitize') ) // bbPress native |
218 | 224 | return user_sanitize( $text, $strict ); |
@@ -9,325 +9,325 @@ |
||
9 | 9 | |
10 | 10 | class PucReadmeParser { |
11 | 11 | |
12 | - function __construct() { |
|
13 | - // This space intentionally blank |
|
14 | - } |
|
15 | - |
|
16 | - function parse_readme( $file ) { |
|
17 | - $file_contents = @implode('', @file($file)); |
|
18 | - return $this->parse_readme_contents( $file_contents ); |
|
19 | - } |
|
20 | - |
|
21 | - function parse_readme_contents( $file_contents ) { |
|
22 | - $file_contents = str_replace(array("\r\n", "\r"), "\n", $file_contents); |
|
23 | - $file_contents = trim($file_contents); |
|
24 | - if ( 0 === strpos( $file_contents, "\xEF\xBB\xBF" ) ) |
|
25 | - $file_contents = substr( $file_contents, 3 ); |
|
26 | - |
|
27 | - // Markdown transformations |
|
28 | - $file_contents = preg_replace( "|^###([^#]+)#*?\s*?\n|im", '=$1='."\n", $file_contents ); |
|
29 | - $file_contents = preg_replace( "|^##([^#]+)#*?\s*?\n|im", '==$1=='."\n", $file_contents ); |
|
30 | - $file_contents = preg_replace( "|^#([^#]+)#*?\s*?\n|im", '===$1==='."\n", $file_contents ); |
|
31 | - |
|
32 | - // === Plugin Name === |
|
33 | - // Must be the very first thing. |
|
34 | - if ( !preg_match('|^===(.*)===|', $file_contents, $_name) ) |
|
35 | - return array(); // require a name |
|
36 | - $name = trim($_name[1], '='); |
|
37 | - $name = $this->sanitize_text( $name ); |
|
38 | - |
|
39 | - $file_contents = $this->chop_string( $file_contents, $_name[0] ); |
|
40 | - |
|
41 | - |
|
42 | - // Requires at least: 1.5 |
|
43 | - if ( preg_match('|Requires at least:(.*)|i', $file_contents, $_requires_at_least) ) |
|
44 | - $requires_at_least = $this->sanitize_text($_requires_at_least[1]); |
|
45 | - else |
|
46 | - $requires_at_least = NULL; |
|
47 | - |
|
48 | - |
|
49 | - // Tested up to: 2.1 |
|
50 | - if ( preg_match('|Tested up to:(.*)|i', $file_contents, $_tested_up_to) ) |
|
51 | - $tested_up_to = $this->sanitize_text( $_tested_up_to[1] ); |
|
52 | - else |
|
53 | - $tested_up_to = NULL; |
|
54 | - |
|
55 | - |
|
56 | - // Stable tag: 10.4-ride-the-fire-eagle-danger-day |
|
57 | - if ( preg_match('|Stable tag:(.*)|i', $file_contents, $_stable_tag) ) |
|
58 | - $stable_tag = $this->sanitize_text( $_stable_tag[1] ); |
|
59 | - else |
|
60 | - $stable_tag = NULL; // we assume trunk, but don't set it here to tell the difference between specified trunk and default trunk |
|
61 | - |
|
62 | - |
|
63 | - // Tags: some tag, another tag, we like tags |
|
64 | - if ( preg_match('|Tags:(.*)|i', $file_contents, $_tags) ) { |
|
65 | - $tags = preg_split('|,[\s]*?|', trim($_tags[1])); |
|
66 | - foreach ( array_keys($tags) as $t ) |
|
67 | - $tags[$t] = $this->sanitize_text( $tags[$t] ); |
|
68 | - } else { |
|
69 | - $tags = array(); |
|
70 | - } |
|
71 | - |
|
72 | - |
|
73 | - // Contributors: markjaquith, mdawaffe, zefrank |
|
74 | - $contributors = array(); |
|
75 | - if ( preg_match('|Contributors:(.*)|i', $file_contents, $_contributors) ) { |
|
76 | - $temp_contributors = preg_split('|,[\s]*|', trim($_contributors[1])); |
|
77 | - foreach ( array_keys($temp_contributors) as $c ) { |
|
78 | - $tmp_sanitized = $this->user_sanitize( $temp_contributors[$c] ); |
|
79 | - if ( strlen(trim($tmp_sanitized)) > 0 ) |
|
80 | - $contributors[$c] = $tmp_sanitized; |
|
81 | - unset($tmp_sanitized); |
|
82 | - } |
|
83 | - } |
|
84 | - |
|
85 | - |
|
86 | - // Donate Link: URL |
|
87 | - if ( preg_match('|Donate link:(.*)|i', $file_contents, $_donate_link) ) |
|
88 | - $donate_link = esc_url( $_donate_link[1] ); |
|
89 | - else |
|
90 | - $donate_link = NULL; |
|
91 | - |
|
92 | - |
|
93 | - // togs, conts, etc are optional and order shouldn't matter. So we chop them only after we've grabbed their values. |
|
94 | - foreach ( array('tags', 'contributors', 'requires_at_least', 'tested_up_to', 'stable_tag', 'donate_link') as $chop ) { |
|
95 | - if ( $$chop ) { |
|
96 | - $_chop = '_' . $chop; |
|
97 | - $file_contents = $this->chop_string( $file_contents, ${$_chop}[0] ); |
|
98 | - } |
|
99 | - } |
|
100 | - |
|
101 | - $file_contents = trim($file_contents); |
|
102 | - |
|
103 | - |
|
104 | - // short-description fu |
|
105 | - if ( !preg_match('/(^(.*?))^[\s]*=+?[\s]*.+?[\s]*=+?/ms', $file_contents, $_short_description) ) |
|
106 | - $_short_description = array( 1 => &$file_contents, 2 => &$file_contents ); |
|
107 | - $short_desc_filtered = $this->sanitize_text( $_short_description[2] ); |
|
108 | - $short_desc_length = strlen($short_desc_filtered); |
|
109 | - $short_description = substr($short_desc_filtered, 0, 150); |
|
110 | - if ( $short_desc_length > strlen($short_description) ) |
|
111 | - $truncated = true; |
|
112 | - else |
|
113 | - $truncated = false; |
|
114 | - if ( $_short_description[1] ) |
|
115 | - $file_contents = $this->chop_string( $file_contents, $_short_description[1] ); // yes, the [1] is intentional |
|
116 | - |
|
117 | - // == Section == |
|
118 | - // Break into sections |
|
119 | - // $_sections[0] will be the title of the first section, $_sections[1] will be the content of the first section |
|
120 | - // the array alternates from there: title2, content2, title3, content3... and so forth |
|
121 | - $_sections = preg_split('/^[\s]*==[\s]*(.+?)[\s]*==/m', $file_contents, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY); |
|
122 | - |
|
123 | - $sections = array(); |
|
124 | - for ( $i=1; $i <= count($_sections); $i +=2 ) { |
|
125 | - $_sections[$i] = preg_replace('/(^[\s]*)=[\s]+(.+?)[\s]+=/m', '$1<h4>$2</h4>', $_sections[$i]); |
|
126 | - $_sections[$i] = $this->filter_text( $_sections[$i], true ); |
|
127 | - $title = $this->sanitize_text( $_sections[$i-1] ); |
|
128 | - $sections[str_replace(' ', '_', strtolower($title))] = array('title' => $title, 'content' => $_sections[$i]); |
|
129 | - } |
|
130 | - |
|
131 | - |
|
132 | - // Special sections |
|
133 | - // This is where we nab our special sections, so we can enforce their order and treat them differently, if needed |
|
134 | - // upgrade_notice is not a section, but parse it like it is for now |
|
135 | - $final_sections = array(); |
|
136 | - foreach ( array('description', 'installation', 'frequently_asked_questions', 'screenshots', 'changelog', 'change_log', 'upgrade_notice') as $special_section ) { |
|
137 | - if ( isset($sections[$special_section]) ) { |
|
138 | - $final_sections[$special_section] = $sections[$special_section]['content']; |
|
139 | - unset($sections[$special_section]); |
|
140 | - } |
|
141 | - } |
|
142 | - if ( isset($final_sections['change_log']) && empty($final_sections['changelog']) ) |
|
143 | - $final_sections['changelog'] = $final_sections['change_log']; |
|
144 | - |
|
145 | - |
|
146 | - $final_screenshots = array(); |
|
147 | - if ( isset($final_sections['screenshots']) ) { |
|
148 | - preg_match_all('|<li>(.*?)</li>|s', $final_sections['screenshots'], $screenshots, PREG_SET_ORDER); |
|
149 | - if ( $screenshots ) { |
|
150 | - foreach ( (array) $screenshots as $ss ) |
|
151 | - $final_screenshots[] = $ss[1]; |
|
152 | - } |
|
153 | - } |
|
154 | - |
|
155 | - // Parse the upgrade_notice section specially: |
|
156 | - // 1.0 => blah, 1.1 => fnord |
|
157 | - $upgrade_notice = array(); |
|
158 | - if ( isset($final_sections['upgrade_notice']) ) { |
|
159 | - $split = preg_split( '#<h4>(.*?)</h4>#', $final_sections['upgrade_notice'], -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY ); |
|
160 | - for ( $i = 0; $i < count( $split ); $i += 2 ) |
|
161 | - $upgrade_notice[$this->sanitize_text( $split[$i] )] = substr( $this->sanitize_text( $split[$i + 1] ), 0, 300 ); |
|
162 | - unset( $final_sections['upgrade_notice'] ); |
|
163 | - } |
|
164 | - |
|
165 | - // No description? |
|
166 | - // No problem... we'll just fall back to the old style of description |
|
167 | - // We'll even let you use markup this time! |
|
168 | - $excerpt = false; |
|
169 | - if ( !isset($final_sections['description']) ) { |
|
170 | - $final_sections = array_merge(array('description' => $this->filter_text( $_short_description[2], true )), $final_sections); |
|
171 | - $excerpt = true; |
|
172 | - } |
|
173 | - |
|
174 | - |
|
175 | - // dump the non-special sections into $remaining_content |
|
176 | - // their order will be determined by their original order in the readme.txt |
|
177 | - $remaining_content = ''; |
|
178 | - foreach ( $sections as $s_name => $s_data ) { |
|
179 | - $remaining_content .= "\n<h3>{$s_data['title']}</h3>\n{$s_data['content']}"; |
|
180 | - } |
|
181 | - $remaining_content = trim($remaining_content); |
|
182 | - |
|
183 | - |
|
184 | - // All done! |
|
185 | - // $r['tags'] and $r['contributors'] are simple arrays |
|
186 | - // $r['sections'] is an array with named elements |
|
187 | - $r = array( |
|
188 | - 'name' => $name, |
|
189 | - 'tags' => $tags, |
|
190 | - 'requires_at_least' => $requires_at_least, |
|
191 | - 'tested_up_to' => $tested_up_to, |
|
192 | - 'stable_tag' => $stable_tag, |
|
193 | - 'contributors' => $contributors, |
|
194 | - 'donate_link' => $donate_link, |
|
195 | - 'short_description' => $short_description, |
|
196 | - 'screenshots' => $final_screenshots, |
|
197 | - 'is_excerpt' => $excerpt, |
|
198 | - 'is_truncated' => $truncated, |
|
199 | - 'sections' => $final_sections, |
|
200 | - 'remaining_content' => $remaining_content, |
|
201 | - 'upgrade_notice' => $upgrade_notice |
|
202 | - ); |
|
203 | - |
|
204 | - return $r; |
|
205 | - } |
|
206 | - |
|
207 | - function chop_string( $string, $chop ) { // chop a "prefix" from a string: Agressive! uses strstr not 0 === strpos |
|
208 | - if ( $_string = strstr($string, $chop) ) { |
|
209 | - $_string = substr($_string, strlen($chop)); |
|
210 | - return trim($_string); |
|
211 | - } else { |
|
212 | - return trim($string); |
|
213 | - } |
|
214 | - } |
|
215 | - |
|
216 | - function user_sanitize( $text, $strict = false ) { // whitelisted chars |
|
217 | - if ( function_exists('user_sanitize') ) // bbPress native |
|
218 | - return user_sanitize( $text, $strict ); |
|
219 | - |
|
220 | - if ( $strict ) { |
|
221 | - $text = preg_replace('/[^a-z0-9-]/i', '', $text); |
|
222 | - $text = preg_replace('|-+|', '-', $text); |
|
223 | - } else { |
|
224 | - $text = preg_replace('/[^a-z0-9_-]/i', '', $text); |
|
225 | - } |
|
226 | - return $text; |
|
227 | - } |
|
228 | - |
|
229 | - function sanitize_text( $text ) { // not fancy |
|
230 | - $text = strip_tags($text); |
|
231 | - $text = esc_html($text); |
|
232 | - $text = trim($text); |
|
233 | - return $text; |
|
234 | - } |
|
235 | - |
|
236 | - function filter_text( $text, $markdown = false ) { // fancy, Markdown |
|
237 | - $text = trim($text); |
|
238 | - |
|
239 | - $text = call_user_func( array( __CLASS__, 'code_trick' ), $text, $markdown ); // A better parser than Markdown's for: backticks -> CODE |
|
240 | - |
|
241 | - if ( $markdown ) { // Parse markdown. |
|
242 | - if ( !class_exists('Parsedown', false) ) { |
|
243 | - /** @noinspection PhpIncludeInspection */ |
|
244 | - require_once(dirname(__FILE__) . '/Parsedown' . (version_compare(PHP_VERSION, '5.3.0', '>=') ? '' : 'Legacy') . '.php'); |
|
245 | - } |
|
246 | - $instance = Parsedown::instance(); |
|
247 | - $text = $instance->text($text); |
|
248 | - } |
|
249 | - |
|
250 | - $allowed = array( |
|
251 | - 'a' => array( |
|
252 | - 'href' => array(), |
|
253 | - 'title' => array(), |
|
254 | - 'rel' => array()), |
|
255 | - 'blockquote' => array('cite' => array()), |
|
256 | - 'br' => array(), |
|
257 | - 'p' => array(), |
|
258 | - 'code' => array(), |
|
259 | - 'pre' => array(), |
|
260 | - 'em' => array(), |
|
261 | - 'strong' => array(), |
|
262 | - 'ul' => array(), |
|
263 | - 'ol' => array(), |
|
264 | - 'li' => array(), |
|
265 | - 'h3' => array(), |
|
266 | - 'h4' => array() |
|
267 | - ); |
|
268 | - |
|
269 | - $text = balanceTags($text); |
|
12 | + function __construct() { |
|
13 | + // This space intentionally blank |
|
14 | + } |
|
15 | + |
|
16 | + function parse_readme( $file ) { |
|
17 | + $file_contents = @implode('', @file($file)); |
|
18 | + return $this->parse_readme_contents( $file_contents ); |
|
19 | + } |
|
20 | + |
|
21 | + function parse_readme_contents( $file_contents ) { |
|
22 | + $file_contents = str_replace(array("\r\n", "\r"), "\n", $file_contents); |
|
23 | + $file_contents = trim($file_contents); |
|
24 | + if ( 0 === strpos( $file_contents, "\xEF\xBB\xBF" ) ) |
|
25 | + $file_contents = substr( $file_contents, 3 ); |
|
26 | + |
|
27 | + // Markdown transformations |
|
28 | + $file_contents = preg_replace( "|^###([^#]+)#*?\s*?\n|im", '=$1='."\n", $file_contents ); |
|
29 | + $file_contents = preg_replace( "|^##([^#]+)#*?\s*?\n|im", '==$1=='."\n", $file_contents ); |
|
30 | + $file_contents = preg_replace( "|^#([^#]+)#*?\s*?\n|im", '===$1==='."\n", $file_contents ); |
|
31 | + |
|
32 | + // === Plugin Name === |
|
33 | + // Must be the very first thing. |
|
34 | + if ( !preg_match('|^===(.*)===|', $file_contents, $_name) ) |
|
35 | + return array(); // require a name |
|
36 | + $name = trim($_name[1], '='); |
|
37 | + $name = $this->sanitize_text( $name ); |
|
38 | + |
|
39 | + $file_contents = $this->chop_string( $file_contents, $_name[0] ); |
|
40 | + |
|
41 | + |
|
42 | + // Requires at least: 1.5 |
|
43 | + if ( preg_match('|Requires at least:(.*)|i', $file_contents, $_requires_at_least) ) |
|
44 | + $requires_at_least = $this->sanitize_text($_requires_at_least[1]); |
|
45 | + else |
|
46 | + $requires_at_least = NULL; |
|
47 | + |
|
48 | + |
|
49 | + // Tested up to: 2.1 |
|
50 | + if ( preg_match('|Tested up to:(.*)|i', $file_contents, $_tested_up_to) ) |
|
51 | + $tested_up_to = $this->sanitize_text( $_tested_up_to[1] ); |
|
52 | + else |
|
53 | + $tested_up_to = NULL; |
|
54 | + |
|
55 | + |
|
56 | + // Stable tag: 10.4-ride-the-fire-eagle-danger-day |
|
57 | + if ( preg_match('|Stable tag:(.*)|i', $file_contents, $_stable_tag) ) |
|
58 | + $stable_tag = $this->sanitize_text( $_stable_tag[1] ); |
|
59 | + else |
|
60 | + $stable_tag = NULL; // we assume trunk, but don't set it here to tell the difference between specified trunk and default trunk |
|
61 | + |
|
62 | + |
|
63 | + // Tags: some tag, another tag, we like tags |
|
64 | + if ( preg_match('|Tags:(.*)|i', $file_contents, $_tags) ) { |
|
65 | + $tags = preg_split('|,[\s]*?|', trim($_tags[1])); |
|
66 | + foreach ( array_keys($tags) as $t ) |
|
67 | + $tags[$t] = $this->sanitize_text( $tags[$t] ); |
|
68 | + } else { |
|
69 | + $tags = array(); |
|
70 | + } |
|
71 | + |
|
72 | + |
|
73 | + // Contributors: markjaquith, mdawaffe, zefrank |
|
74 | + $contributors = array(); |
|
75 | + if ( preg_match('|Contributors:(.*)|i', $file_contents, $_contributors) ) { |
|
76 | + $temp_contributors = preg_split('|,[\s]*|', trim($_contributors[1])); |
|
77 | + foreach ( array_keys($temp_contributors) as $c ) { |
|
78 | + $tmp_sanitized = $this->user_sanitize( $temp_contributors[$c] ); |
|
79 | + if ( strlen(trim($tmp_sanitized)) > 0 ) |
|
80 | + $contributors[$c] = $tmp_sanitized; |
|
81 | + unset($tmp_sanitized); |
|
82 | + } |
|
83 | + } |
|
84 | + |
|
85 | + |
|
86 | + // Donate Link: URL |
|
87 | + if ( preg_match('|Donate link:(.*)|i', $file_contents, $_donate_link) ) |
|
88 | + $donate_link = esc_url( $_donate_link[1] ); |
|
89 | + else |
|
90 | + $donate_link = NULL; |
|
91 | + |
|
92 | + |
|
93 | + // togs, conts, etc are optional and order shouldn't matter. So we chop them only after we've grabbed their values. |
|
94 | + foreach ( array('tags', 'contributors', 'requires_at_least', 'tested_up_to', 'stable_tag', 'donate_link') as $chop ) { |
|
95 | + if ( $$chop ) { |
|
96 | + $_chop = '_' . $chop; |
|
97 | + $file_contents = $this->chop_string( $file_contents, ${$_chop}[0] ); |
|
98 | + } |
|
99 | + } |
|
100 | + |
|
101 | + $file_contents = trim($file_contents); |
|
102 | + |
|
103 | + |
|
104 | + // short-description fu |
|
105 | + if ( !preg_match('/(^(.*?))^[\s]*=+?[\s]*.+?[\s]*=+?/ms', $file_contents, $_short_description) ) |
|
106 | + $_short_description = array( 1 => &$file_contents, 2 => &$file_contents ); |
|
107 | + $short_desc_filtered = $this->sanitize_text( $_short_description[2] ); |
|
108 | + $short_desc_length = strlen($short_desc_filtered); |
|
109 | + $short_description = substr($short_desc_filtered, 0, 150); |
|
110 | + if ( $short_desc_length > strlen($short_description) ) |
|
111 | + $truncated = true; |
|
112 | + else |
|
113 | + $truncated = false; |
|
114 | + if ( $_short_description[1] ) |
|
115 | + $file_contents = $this->chop_string( $file_contents, $_short_description[1] ); // yes, the [1] is intentional |
|
116 | + |
|
117 | + // == Section == |
|
118 | + // Break into sections |
|
119 | + // $_sections[0] will be the title of the first section, $_sections[1] will be the content of the first section |
|
120 | + // the array alternates from there: title2, content2, title3, content3... and so forth |
|
121 | + $_sections = preg_split('/^[\s]*==[\s]*(.+?)[\s]*==/m', $file_contents, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY); |
|
122 | + |
|
123 | + $sections = array(); |
|
124 | + for ( $i=1; $i <= count($_sections); $i +=2 ) { |
|
125 | + $_sections[$i] = preg_replace('/(^[\s]*)=[\s]+(.+?)[\s]+=/m', '$1<h4>$2</h4>', $_sections[$i]); |
|
126 | + $_sections[$i] = $this->filter_text( $_sections[$i], true ); |
|
127 | + $title = $this->sanitize_text( $_sections[$i-1] ); |
|
128 | + $sections[str_replace(' ', '_', strtolower($title))] = array('title' => $title, 'content' => $_sections[$i]); |
|
129 | + } |
|
130 | + |
|
131 | + |
|
132 | + // Special sections |
|
133 | + // This is where we nab our special sections, so we can enforce their order and treat them differently, if needed |
|
134 | + // upgrade_notice is not a section, but parse it like it is for now |
|
135 | + $final_sections = array(); |
|
136 | + foreach ( array('description', 'installation', 'frequently_asked_questions', 'screenshots', 'changelog', 'change_log', 'upgrade_notice') as $special_section ) { |
|
137 | + if ( isset($sections[$special_section]) ) { |
|
138 | + $final_sections[$special_section] = $sections[$special_section]['content']; |
|
139 | + unset($sections[$special_section]); |
|
140 | + } |
|
141 | + } |
|
142 | + if ( isset($final_sections['change_log']) && empty($final_sections['changelog']) ) |
|
143 | + $final_sections['changelog'] = $final_sections['change_log']; |
|
144 | + |
|
145 | + |
|
146 | + $final_screenshots = array(); |
|
147 | + if ( isset($final_sections['screenshots']) ) { |
|
148 | + preg_match_all('|<li>(.*?)</li>|s', $final_sections['screenshots'], $screenshots, PREG_SET_ORDER); |
|
149 | + if ( $screenshots ) { |
|
150 | + foreach ( (array) $screenshots as $ss ) |
|
151 | + $final_screenshots[] = $ss[1]; |
|
152 | + } |
|
153 | + } |
|
154 | + |
|
155 | + // Parse the upgrade_notice section specially: |
|
156 | + // 1.0 => blah, 1.1 => fnord |
|
157 | + $upgrade_notice = array(); |
|
158 | + if ( isset($final_sections['upgrade_notice']) ) { |
|
159 | + $split = preg_split( '#<h4>(.*?)</h4>#', $final_sections['upgrade_notice'], -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY ); |
|
160 | + for ( $i = 0; $i < count( $split ); $i += 2 ) |
|
161 | + $upgrade_notice[$this->sanitize_text( $split[$i] )] = substr( $this->sanitize_text( $split[$i + 1] ), 0, 300 ); |
|
162 | + unset( $final_sections['upgrade_notice'] ); |
|
163 | + } |
|
164 | + |
|
165 | + // No description? |
|
166 | + // No problem... we'll just fall back to the old style of description |
|
167 | + // We'll even let you use markup this time! |
|
168 | + $excerpt = false; |
|
169 | + if ( !isset($final_sections['description']) ) { |
|
170 | + $final_sections = array_merge(array('description' => $this->filter_text( $_short_description[2], true )), $final_sections); |
|
171 | + $excerpt = true; |
|
172 | + } |
|
173 | + |
|
174 | + |
|
175 | + // dump the non-special sections into $remaining_content |
|
176 | + // their order will be determined by their original order in the readme.txt |
|
177 | + $remaining_content = ''; |
|
178 | + foreach ( $sections as $s_name => $s_data ) { |
|
179 | + $remaining_content .= "\n<h3>{$s_data['title']}</h3>\n{$s_data['content']}"; |
|
180 | + } |
|
181 | + $remaining_content = trim($remaining_content); |
|
182 | + |
|
183 | + |
|
184 | + // All done! |
|
185 | + // $r['tags'] and $r['contributors'] are simple arrays |
|
186 | + // $r['sections'] is an array with named elements |
|
187 | + $r = array( |
|
188 | + 'name' => $name, |
|
189 | + 'tags' => $tags, |
|
190 | + 'requires_at_least' => $requires_at_least, |
|
191 | + 'tested_up_to' => $tested_up_to, |
|
192 | + 'stable_tag' => $stable_tag, |
|
193 | + 'contributors' => $contributors, |
|
194 | + 'donate_link' => $donate_link, |
|
195 | + 'short_description' => $short_description, |
|
196 | + 'screenshots' => $final_screenshots, |
|
197 | + 'is_excerpt' => $excerpt, |
|
198 | + 'is_truncated' => $truncated, |
|
199 | + 'sections' => $final_sections, |
|
200 | + 'remaining_content' => $remaining_content, |
|
201 | + 'upgrade_notice' => $upgrade_notice |
|
202 | + ); |
|
203 | + |
|
204 | + return $r; |
|
205 | + } |
|
206 | + |
|
207 | + function chop_string( $string, $chop ) { // chop a "prefix" from a string: Agressive! uses strstr not 0 === strpos |
|
208 | + if ( $_string = strstr($string, $chop) ) { |
|
209 | + $_string = substr($_string, strlen($chop)); |
|
210 | + return trim($_string); |
|
211 | + } else { |
|
212 | + return trim($string); |
|
213 | + } |
|
214 | + } |
|
215 | + |
|
216 | + function user_sanitize( $text, $strict = false ) { // whitelisted chars |
|
217 | + if ( function_exists('user_sanitize') ) // bbPress native |
|
218 | + return user_sanitize( $text, $strict ); |
|
219 | + |
|
220 | + if ( $strict ) { |
|
221 | + $text = preg_replace('/[^a-z0-9-]/i', '', $text); |
|
222 | + $text = preg_replace('|-+|', '-', $text); |
|
223 | + } else { |
|
224 | + $text = preg_replace('/[^a-z0-9_-]/i', '', $text); |
|
225 | + } |
|
226 | + return $text; |
|
227 | + } |
|
228 | + |
|
229 | + function sanitize_text( $text ) { // not fancy |
|
230 | + $text = strip_tags($text); |
|
231 | + $text = esc_html($text); |
|
232 | + $text = trim($text); |
|
233 | + return $text; |
|
234 | + } |
|
235 | + |
|
236 | + function filter_text( $text, $markdown = false ) { // fancy, Markdown |
|
237 | + $text = trim($text); |
|
238 | + |
|
239 | + $text = call_user_func( array( __CLASS__, 'code_trick' ), $text, $markdown ); // A better parser than Markdown's for: backticks -> CODE |
|
240 | + |
|
241 | + if ( $markdown ) { // Parse markdown. |
|
242 | + if ( !class_exists('Parsedown', false) ) { |
|
243 | + /** @noinspection PhpIncludeInspection */ |
|
244 | + require_once(dirname(__FILE__) . '/Parsedown' . (version_compare(PHP_VERSION, '5.3.0', '>=') ? '' : 'Legacy') . '.php'); |
|
245 | + } |
|
246 | + $instance = Parsedown::instance(); |
|
247 | + $text = $instance->text($text); |
|
248 | + } |
|
249 | + |
|
250 | + $allowed = array( |
|
251 | + 'a' => array( |
|
252 | + 'href' => array(), |
|
253 | + 'title' => array(), |
|
254 | + 'rel' => array()), |
|
255 | + 'blockquote' => array('cite' => array()), |
|
256 | + 'br' => array(), |
|
257 | + 'p' => array(), |
|
258 | + 'code' => array(), |
|
259 | + 'pre' => array(), |
|
260 | + 'em' => array(), |
|
261 | + 'strong' => array(), |
|
262 | + 'ul' => array(), |
|
263 | + 'ol' => array(), |
|
264 | + 'li' => array(), |
|
265 | + 'h3' => array(), |
|
266 | + 'h4' => array() |
|
267 | + ); |
|
268 | + |
|
269 | + $text = balanceTags($text); |
|
270 | 270 | |
271 | - $text = wp_kses( $text, $allowed ); |
|
272 | - $text = trim($text); |
|
273 | - return $text; |
|
274 | - } |
|
275 | - |
|
276 | - function code_trick( $text, $markdown ) { // Don't use bbPress native function - it's incompatible with Markdown |
|
277 | - // If doing markdown, first take any user formatted code blocks and turn them into backticks so that |
|
278 | - // markdown will preserve things like underscores in code blocks |
|
279 | - if ( $markdown ) |
|
280 | - $text = preg_replace_callback("!(<pre><code>|<code>)(.*?)(</code></pre>|</code>)!s", array( __CLASS__,'decodeit'), $text); |
|
281 | - |
|
282 | - $text = str_replace(array("\r\n", "\r"), "\n", $text); |
|
283 | - if ( !$markdown ) { |
|
284 | - // This gets the "inline" code blocks, but can't be used with Markdown. |
|
285 | - $text = preg_replace_callback("|(`)(.*?)`|", array( __CLASS__, 'encodeit'), $text); |
|
286 | - // This gets the "block level" code blocks and converts them to PRE CODE |
|
287 | - $text = preg_replace_callback("!(^|\n)`(.*?)`!s", array( __CLASS__, 'encodeit'), $text); |
|
288 | - } else { |
|
289 | - // Markdown can do inline code, we convert bbPress style block level code to Markdown style |
|
290 | - $text = preg_replace_callback("!(^|\n)([ \t]*?)`(.*?)`!s", array( __CLASS__, 'indent'), $text); |
|
291 | - } |
|
292 | - return $text; |
|
293 | - } |
|
294 | - |
|
295 | - function indent( $matches ) { |
|
296 | - $text = $matches[3]; |
|
297 | - $text = preg_replace('|^|m', $matches[2] . ' ', $text); |
|
298 | - return $matches[1] . $text; |
|
299 | - } |
|
300 | - |
|
301 | - function encodeit( $matches ) { |
|
302 | - if ( function_exists('encodeit') ) // bbPress native |
|
303 | - return encodeit( $matches ); |
|
304 | - |
|
305 | - $text = trim($matches[2]); |
|
306 | - $text = htmlspecialchars($text, ENT_QUOTES); |
|
307 | - $text = str_replace(array("\r\n", "\r"), "\n", $text); |
|
308 | - $text = preg_replace("|\n\n\n+|", "\n\n", $text); |
|
309 | - $text = str_replace('&lt;', '<', $text); |
|
310 | - $text = str_replace('&gt;', '>', $text); |
|
311 | - $text = "<code>$text</code>"; |
|
312 | - if ( "`" != $matches[1] ) |
|
313 | - $text = "<pre>$text</pre>"; |
|
314 | - return $text; |
|
315 | - } |
|
316 | - |
|
317 | - function decodeit( $matches ) { |
|
318 | - if ( function_exists('decodeit') ) // bbPress native |
|
319 | - return decodeit( $matches ); |
|
320 | - |
|
321 | - $text = $matches[2]; |
|
322 | - $trans_table = array_flip(get_html_translation_table(HTML_ENTITIES)); |
|
323 | - $text = strtr($text, $trans_table); |
|
324 | - $text = str_replace('<br />', '', $text); |
|
325 | - $text = str_replace('&', '&', $text); |
|
326 | - $text = str_replace(''', "'", $text); |
|
327 | - if ( '<pre><code>' == $matches[1] ) |
|
328 | - $text = "\n$text\n"; |
|
329 | - return "`$text`"; |
|
330 | - } |
|
271 | + $text = wp_kses( $text, $allowed ); |
|
272 | + $text = trim($text); |
|
273 | + return $text; |
|
274 | + } |
|
275 | + |
|
276 | + function code_trick( $text, $markdown ) { // Don't use bbPress native function - it's incompatible with Markdown |
|
277 | + // If doing markdown, first take any user formatted code blocks and turn them into backticks so that |
|
278 | + // markdown will preserve things like underscores in code blocks |
|
279 | + if ( $markdown ) |
|
280 | + $text = preg_replace_callback("!(<pre><code>|<code>)(.*?)(</code></pre>|</code>)!s", array( __CLASS__,'decodeit'), $text); |
|
281 | + |
|
282 | + $text = str_replace(array("\r\n", "\r"), "\n", $text); |
|
283 | + if ( !$markdown ) { |
|
284 | + // This gets the "inline" code blocks, but can't be used with Markdown. |
|
285 | + $text = preg_replace_callback("|(`)(.*?)`|", array( __CLASS__, 'encodeit'), $text); |
|
286 | + // This gets the "block level" code blocks and converts them to PRE CODE |
|
287 | + $text = preg_replace_callback("!(^|\n)`(.*?)`!s", array( __CLASS__, 'encodeit'), $text); |
|
288 | + } else { |
|
289 | + // Markdown can do inline code, we convert bbPress style block level code to Markdown style |
|
290 | + $text = preg_replace_callback("!(^|\n)([ \t]*?)`(.*?)`!s", array( __CLASS__, 'indent'), $text); |
|
291 | + } |
|
292 | + return $text; |
|
293 | + } |
|
294 | + |
|
295 | + function indent( $matches ) { |
|
296 | + $text = $matches[3]; |
|
297 | + $text = preg_replace('|^|m', $matches[2] . ' ', $text); |
|
298 | + return $matches[1] . $text; |
|
299 | + } |
|
300 | + |
|
301 | + function encodeit( $matches ) { |
|
302 | + if ( function_exists('encodeit') ) // bbPress native |
|
303 | + return encodeit( $matches ); |
|
304 | + |
|
305 | + $text = trim($matches[2]); |
|
306 | + $text = htmlspecialchars($text, ENT_QUOTES); |
|
307 | + $text = str_replace(array("\r\n", "\r"), "\n", $text); |
|
308 | + $text = preg_replace("|\n\n\n+|", "\n\n", $text); |
|
309 | + $text = str_replace('&lt;', '<', $text); |
|
310 | + $text = str_replace('&gt;', '>', $text); |
|
311 | + $text = "<code>$text</code>"; |
|
312 | + if ( "`" != $matches[1] ) |
|
313 | + $text = "<pre>$text</pre>"; |
|
314 | + return $text; |
|
315 | + } |
|
316 | + |
|
317 | + function decodeit( $matches ) { |
|
318 | + if ( function_exists('decodeit') ) // bbPress native |
|
319 | + return decodeit( $matches ); |
|
320 | + |
|
321 | + $text = $matches[2]; |
|
322 | + $trans_table = array_flip(get_html_translation_table(HTML_ENTITIES)); |
|
323 | + $text = strtr($text, $trans_table); |
|
324 | + $text = str_replace('<br />', '', $text); |
|
325 | + $text = str_replace('&', '&', $text); |
|
326 | + $text = str_replace(''', "'", $text); |
|
327 | + if ( '<pre><code>' == $matches[1] ) |
|
328 | + $text = "\n$text\n"; |
|
329 | + return "`$text`"; |
|
330 | + } |
|
331 | 331 | |
332 | 332 | } // end class |
333 | 333 |
@@ -1,6 +1,6 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | |
3 | -if ( !class_exists('PucReadmeParser', false) ): |
|
3 | +if (!class_exists('PucReadmeParser', false)): |
|
4 | 4 | |
5 | 5 | /** |
6 | 6 | * This is a slightly modified version of github.com/markjaquith/WordPress-Plugin-Readme-Parser |
@@ -13,58 +13,58 @@ discard block |
||
13 | 13 | // This space intentionally blank |
14 | 14 | } |
15 | 15 | |
16 | - function parse_readme( $file ) { |
|
16 | + function parse_readme($file) { |
|
17 | 17 | $file_contents = @implode('', @file($file)); |
18 | - return $this->parse_readme_contents( $file_contents ); |
|
18 | + return $this->parse_readme_contents($file_contents); |
|
19 | 19 | } |
20 | 20 | |
21 | - function parse_readme_contents( $file_contents ) { |
|
21 | + function parse_readme_contents($file_contents) { |
|
22 | 22 | $file_contents = str_replace(array("\r\n", "\r"), "\n", $file_contents); |
23 | 23 | $file_contents = trim($file_contents); |
24 | - if ( 0 === strpos( $file_contents, "\xEF\xBB\xBF" ) ) |
|
25 | - $file_contents = substr( $file_contents, 3 ); |
|
24 | + if (0 === strpos($file_contents, "\xEF\xBB\xBF")) |
|
25 | + $file_contents = substr($file_contents, 3); |
|
26 | 26 | |
27 | 27 | // Markdown transformations |
28 | - $file_contents = preg_replace( "|^###([^#]+)#*?\s*?\n|im", '=$1='."\n", $file_contents ); |
|
29 | - $file_contents = preg_replace( "|^##([^#]+)#*?\s*?\n|im", '==$1=='."\n", $file_contents ); |
|
30 | - $file_contents = preg_replace( "|^#([^#]+)#*?\s*?\n|im", '===$1==='."\n", $file_contents ); |
|
28 | + $file_contents = preg_replace("|^###([^#]+)#*?\s*?\n|im", '=$1='."\n", $file_contents); |
|
29 | + $file_contents = preg_replace("|^##([^#]+)#*?\s*?\n|im", '==$1=='."\n", $file_contents); |
|
30 | + $file_contents = preg_replace("|^#([^#]+)#*?\s*?\n|im", '===$1==='."\n", $file_contents); |
|
31 | 31 | |
32 | 32 | // === Plugin Name === |
33 | 33 | // Must be the very first thing. |
34 | - if ( !preg_match('|^===(.*)===|', $file_contents, $_name) ) |
|
34 | + if (!preg_match('|^===(.*)===|', $file_contents, $_name)) |
|
35 | 35 | return array(); // require a name |
36 | 36 | $name = trim($_name[1], '='); |
37 | - $name = $this->sanitize_text( $name ); |
|
37 | + $name = $this->sanitize_text($name); |
|
38 | 38 | |
39 | - $file_contents = $this->chop_string( $file_contents, $_name[0] ); |
|
39 | + $file_contents = $this->chop_string($file_contents, $_name[0]); |
|
40 | 40 | |
41 | 41 | |
42 | 42 | // Requires at least: 1.5 |
43 | - if ( preg_match('|Requires at least:(.*)|i', $file_contents, $_requires_at_least) ) |
|
43 | + if (preg_match('|Requires at least:(.*)|i', $file_contents, $_requires_at_least)) |
|
44 | 44 | $requires_at_least = $this->sanitize_text($_requires_at_least[1]); |
45 | 45 | else |
46 | 46 | $requires_at_least = NULL; |
47 | 47 | |
48 | 48 | |
49 | 49 | // Tested up to: 2.1 |
50 | - if ( preg_match('|Tested up to:(.*)|i', $file_contents, $_tested_up_to) ) |
|
51 | - $tested_up_to = $this->sanitize_text( $_tested_up_to[1] ); |
|
50 | + if (preg_match('|Tested up to:(.*)|i', $file_contents, $_tested_up_to)) |
|
51 | + $tested_up_to = $this->sanitize_text($_tested_up_to[1]); |
|
52 | 52 | else |
53 | 53 | $tested_up_to = NULL; |
54 | 54 | |
55 | 55 | |
56 | 56 | // Stable tag: 10.4-ride-the-fire-eagle-danger-day |
57 | - if ( preg_match('|Stable tag:(.*)|i', $file_contents, $_stable_tag) ) |
|
58 | - $stable_tag = $this->sanitize_text( $_stable_tag[1] ); |
|
57 | + if (preg_match('|Stable tag:(.*)|i', $file_contents, $_stable_tag)) |
|
58 | + $stable_tag = $this->sanitize_text($_stable_tag[1]); |
|
59 | 59 | else |
60 | 60 | $stable_tag = NULL; // we assume trunk, but don't set it here to tell the difference between specified trunk and default trunk |
61 | 61 | |
62 | 62 | |
63 | 63 | // Tags: some tag, another tag, we like tags |
64 | - if ( preg_match('|Tags:(.*)|i', $file_contents, $_tags) ) { |
|
64 | + if (preg_match('|Tags:(.*)|i', $file_contents, $_tags)) { |
|
65 | 65 | $tags = preg_split('|,[\s]*?|', trim($_tags[1])); |
66 | - foreach ( array_keys($tags) as $t ) |
|
67 | - $tags[$t] = $this->sanitize_text( $tags[$t] ); |
|
66 | + foreach (array_keys($tags) as $t) |
|
67 | + $tags[$t] = $this->sanitize_text($tags[$t]); |
|
68 | 68 | } else { |
69 | 69 | $tags = array(); |
70 | 70 | } |
@@ -72,11 +72,11 @@ discard block |
||
72 | 72 | |
73 | 73 | // Contributors: markjaquith, mdawaffe, zefrank |
74 | 74 | $contributors = array(); |
75 | - if ( preg_match('|Contributors:(.*)|i', $file_contents, $_contributors) ) { |
|
75 | + if (preg_match('|Contributors:(.*)|i', $file_contents, $_contributors)) { |
|
76 | 76 | $temp_contributors = preg_split('|,[\s]*|', trim($_contributors[1])); |
77 | - foreach ( array_keys($temp_contributors) as $c ) { |
|
78 | - $tmp_sanitized = $this->user_sanitize( $temp_contributors[$c] ); |
|
79 | - if ( strlen(trim($tmp_sanitized)) > 0 ) |
|
77 | + foreach (array_keys($temp_contributors) as $c) { |
|
78 | + $tmp_sanitized = $this->user_sanitize($temp_contributors[$c]); |
|
79 | + if (strlen(trim($tmp_sanitized)) > 0) |
|
80 | 80 | $contributors[$c] = $tmp_sanitized; |
81 | 81 | unset($tmp_sanitized); |
82 | 82 | } |
@@ -84,17 +84,17 @@ discard block |
||
84 | 84 | |
85 | 85 | |
86 | 86 | // Donate Link: URL |
87 | - if ( preg_match('|Donate link:(.*)|i', $file_contents, $_donate_link) ) |
|
88 | - $donate_link = esc_url( $_donate_link[1] ); |
|
87 | + if (preg_match('|Donate link:(.*)|i', $file_contents, $_donate_link)) |
|
88 | + $donate_link = esc_url($_donate_link[1]); |
|
89 | 89 | else |
90 | 90 | $donate_link = NULL; |
91 | 91 | |
92 | 92 | |
93 | 93 | // togs, conts, etc are optional and order shouldn't matter. So we chop them only after we've grabbed their values. |
94 | - foreach ( array('tags', 'contributors', 'requires_at_least', 'tested_up_to', 'stable_tag', 'donate_link') as $chop ) { |
|
95 | - if ( $$chop ) { |
|
96 | - $_chop = '_' . $chop; |
|
97 | - $file_contents = $this->chop_string( $file_contents, ${$_chop}[0] ); |
|
94 | + foreach (array('tags', 'contributors', 'requires_at_least', 'tested_up_to', 'stable_tag', 'donate_link') as $chop) { |
|
95 | + if ($$chop) { |
|
96 | + $_chop = '_'.$chop; |
|
97 | + $file_contents = $this->chop_string($file_contents, ${$_chop}[0]); |
|
98 | 98 | } |
99 | 99 | } |
100 | 100 | |
@@ -102,29 +102,29 @@ discard block |
||
102 | 102 | |
103 | 103 | |
104 | 104 | // short-description fu |
105 | - if ( !preg_match('/(^(.*?))^[\s]*=+?[\s]*.+?[\s]*=+?/ms', $file_contents, $_short_description) ) |
|
106 | - $_short_description = array( 1 => &$file_contents, 2 => &$file_contents ); |
|
107 | - $short_desc_filtered = $this->sanitize_text( $_short_description[2] ); |
|
105 | + if (!preg_match('/(^(.*?))^[\s]*=+?[\s]*.+?[\s]*=+?/ms', $file_contents, $_short_description)) |
|
106 | + $_short_description = array(1 => &$file_contents, 2 => &$file_contents); |
|
107 | + $short_desc_filtered = $this->sanitize_text($_short_description[2]); |
|
108 | 108 | $short_desc_length = strlen($short_desc_filtered); |
109 | 109 | $short_description = substr($short_desc_filtered, 0, 150); |
110 | - if ( $short_desc_length > strlen($short_description) ) |
|
110 | + if ($short_desc_length > strlen($short_description)) |
|
111 | 111 | $truncated = true; |
112 | 112 | else |
113 | 113 | $truncated = false; |
114 | - if ( $_short_description[1] ) |
|
115 | - $file_contents = $this->chop_string( $file_contents, $_short_description[1] ); // yes, the [1] is intentional |
|
114 | + if ($_short_description[1]) |
|
115 | + $file_contents = $this->chop_string($file_contents, $_short_description[1]); // yes, the [1] is intentional |
|
116 | 116 | |
117 | 117 | // == Section == |
118 | 118 | // Break into sections |
119 | 119 | // $_sections[0] will be the title of the first section, $_sections[1] will be the content of the first section |
120 | 120 | // the array alternates from there: title2, content2, title3, content3... and so forth |
121 | - $_sections = preg_split('/^[\s]*==[\s]*(.+?)[\s]*==/m', $file_contents, -1, PREG_SPLIT_DELIM_CAPTURE|PREG_SPLIT_NO_EMPTY); |
|
121 | + $_sections = preg_split('/^[\s]*==[\s]*(.+?)[\s]*==/m', $file_contents, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); |
|
122 | 122 | |
123 | 123 | $sections = array(); |
124 | - for ( $i=1; $i <= count($_sections); $i +=2 ) { |
|
124 | + for ($i = 1; $i <= count($_sections); $i += 2) { |
|
125 | 125 | $_sections[$i] = preg_replace('/(^[\s]*)=[\s]+(.+?)[\s]+=/m', '$1<h4>$2</h4>', $_sections[$i]); |
126 | - $_sections[$i] = $this->filter_text( $_sections[$i], true ); |
|
127 | - $title = $this->sanitize_text( $_sections[$i-1] ); |
|
126 | + $_sections[$i] = $this->filter_text($_sections[$i], true); |
|
127 | + $title = $this->sanitize_text($_sections[$i - 1]); |
|
128 | 128 | $sections[str_replace(' ', '_', strtolower($title))] = array('title' => $title, 'content' => $_sections[$i]); |
129 | 129 | } |
130 | 130 | |
@@ -133,21 +133,21 @@ discard block |
||
133 | 133 | // This is where we nab our special sections, so we can enforce their order and treat them differently, if needed |
134 | 134 | // upgrade_notice is not a section, but parse it like it is for now |
135 | 135 | $final_sections = array(); |
136 | - foreach ( array('description', 'installation', 'frequently_asked_questions', 'screenshots', 'changelog', 'change_log', 'upgrade_notice') as $special_section ) { |
|
137 | - if ( isset($sections[$special_section]) ) { |
|
136 | + foreach (array('description', 'installation', 'frequently_asked_questions', 'screenshots', 'changelog', 'change_log', 'upgrade_notice') as $special_section) { |
|
137 | + if (isset($sections[$special_section])) { |
|
138 | 138 | $final_sections[$special_section] = $sections[$special_section]['content']; |
139 | 139 | unset($sections[$special_section]); |
140 | 140 | } |
141 | 141 | } |
142 | - if ( isset($final_sections['change_log']) && empty($final_sections['changelog']) ) |
|
142 | + if (isset($final_sections['change_log']) && empty($final_sections['changelog'])) |
|
143 | 143 | $final_sections['changelog'] = $final_sections['change_log']; |
144 | 144 | |
145 | 145 | |
146 | 146 | $final_screenshots = array(); |
147 | - if ( isset($final_sections['screenshots']) ) { |
|
147 | + if (isset($final_sections['screenshots'])) { |
|
148 | 148 | preg_match_all('|<li>(.*?)</li>|s', $final_sections['screenshots'], $screenshots, PREG_SET_ORDER); |
149 | - if ( $screenshots ) { |
|
150 | - foreach ( (array) $screenshots as $ss ) |
|
149 | + if ($screenshots) { |
|
150 | + foreach ((array) $screenshots as $ss) |
|
151 | 151 | $final_screenshots[] = $ss[1]; |
152 | 152 | } |
153 | 153 | } |
@@ -155,19 +155,19 @@ discard block |
||
155 | 155 | // Parse the upgrade_notice section specially: |
156 | 156 | // 1.0 => blah, 1.1 => fnord |
157 | 157 | $upgrade_notice = array(); |
158 | - if ( isset($final_sections['upgrade_notice']) ) { |
|
159 | - $split = preg_split( '#<h4>(.*?)</h4>#', $final_sections['upgrade_notice'], -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY ); |
|
160 | - for ( $i = 0; $i < count( $split ); $i += 2 ) |
|
161 | - $upgrade_notice[$this->sanitize_text( $split[$i] )] = substr( $this->sanitize_text( $split[$i + 1] ), 0, 300 ); |
|
162 | - unset( $final_sections['upgrade_notice'] ); |
|
158 | + if (isset($final_sections['upgrade_notice'])) { |
|
159 | + $split = preg_split('#<h4>(.*?)</h4>#', $final_sections['upgrade_notice'], -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); |
|
160 | + for ($i = 0; $i < count($split); $i += 2) |
|
161 | + $upgrade_notice[$this->sanitize_text($split[$i])] = substr($this->sanitize_text($split[$i + 1]), 0, 300); |
|
162 | + unset($final_sections['upgrade_notice']); |
|
163 | 163 | } |
164 | 164 | |
165 | 165 | // No description? |
166 | 166 | // No problem... we'll just fall back to the old style of description |
167 | 167 | // We'll even let you use markup this time! |
168 | 168 | $excerpt = false; |
169 | - if ( !isset($final_sections['description']) ) { |
|
170 | - $final_sections = array_merge(array('description' => $this->filter_text( $_short_description[2], true )), $final_sections); |
|
169 | + if (!isset($final_sections['description'])) { |
|
170 | + $final_sections = array_merge(array('description' => $this->filter_text($_short_description[2], true)), $final_sections); |
|
171 | 171 | $excerpt = true; |
172 | 172 | } |
173 | 173 | |
@@ -175,7 +175,7 @@ discard block |
||
175 | 175 | // dump the non-special sections into $remaining_content |
176 | 176 | // their order will be determined by their original order in the readme.txt |
177 | 177 | $remaining_content = ''; |
178 | - foreach ( $sections as $s_name => $s_data ) { |
|
178 | + foreach ($sections as $s_name => $s_data) { |
|
179 | 179 | $remaining_content .= "\n<h3>{$s_data['title']}</h3>\n{$s_data['content']}"; |
180 | 180 | } |
181 | 181 | $remaining_content = trim($remaining_content); |
@@ -204,8 +204,8 @@ discard block |
||
204 | 204 | return $r; |
205 | 205 | } |
206 | 206 | |
207 | - function chop_string( $string, $chop ) { // chop a "prefix" from a string: Agressive! uses strstr not 0 === strpos |
|
208 | - if ( $_string = strstr($string, $chop) ) { |
|
207 | + function chop_string($string, $chop) { // chop a "prefix" from a string: Agressive! uses strstr not 0 === strpos |
|
208 | + if ($_string = strstr($string, $chop)) { |
|
209 | 209 | $_string = substr($_string, strlen($chop)); |
210 | 210 | return trim($_string); |
211 | 211 | } else { |
@@ -213,11 +213,11 @@ discard block |
||
213 | 213 | } |
214 | 214 | } |
215 | 215 | |
216 | - function user_sanitize( $text, $strict = false ) { // whitelisted chars |
|
217 | - if ( function_exists('user_sanitize') ) // bbPress native |
|
218 | - return user_sanitize( $text, $strict ); |
|
216 | + function user_sanitize($text, $strict = false) { // whitelisted chars |
|
217 | + if (function_exists('user_sanitize')) // bbPress native |
|
218 | + return user_sanitize($text, $strict); |
|
219 | 219 | |
220 | - if ( $strict ) { |
|
220 | + if ($strict) { |
|
221 | 221 | $text = preg_replace('/[^a-z0-9-]/i', '', $text); |
222 | 222 | $text = preg_replace('|-+|', '-', $text); |
223 | 223 | } else { |
@@ -226,22 +226,22 @@ discard block |
||
226 | 226 | return $text; |
227 | 227 | } |
228 | 228 | |
229 | - function sanitize_text( $text ) { // not fancy |
|
229 | + function sanitize_text($text) { // not fancy |
|
230 | 230 | $text = strip_tags($text); |
231 | 231 | $text = esc_html($text); |
232 | 232 | $text = trim($text); |
233 | 233 | return $text; |
234 | 234 | } |
235 | 235 | |
236 | - function filter_text( $text, $markdown = false ) { // fancy, Markdown |
|
236 | + function filter_text($text, $markdown = false) { // fancy, Markdown |
|
237 | 237 | $text = trim($text); |
238 | 238 | |
239 | - $text = call_user_func( array( __CLASS__, 'code_trick' ), $text, $markdown ); // A better parser than Markdown's for: backticks -> CODE |
|
239 | + $text = call_user_func(array(__CLASS__, 'code_trick'), $text, $markdown); // A better parser than Markdown's for: backticks -> CODE |
|
240 | 240 | |
241 | - if ( $markdown ) { // Parse markdown. |
|
242 | - if ( !class_exists('Parsedown', false) ) { |
|
241 | + if ($markdown) { // Parse markdown. |
|
242 | + if (!class_exists('Parsedown', false)) { |
|
243 | 243 | /** @noinspection PhpIncludeInspection */ |
244 | - require_once(dirname(__FILE__) . '/Parsedown' . (version_compare(PHP_VERSION, '5.3.0', '>=') ? '' : 'Legacy') . '.php'); |
|
244 | + require_once(dirname(__FILE__).'/Parsedown'.(version_compare(PHP_VERSION, '5.3.0', '>=') ? '' : 'Legacy').'.php'); |
|
245 | 245 | } |
246 | 246 | $instance = Parsedown::instance(); |
247 | 247 | $text = $instance->text($text); |
@@ -268,39 +268,39 @@ discard block |
||
268 | 268 | |
269 | 269 | $text = balanceTags($text); |
270 | 270 | |
271 | - $text = wp_kses( $text, $allowed ); |
|
271 | + $text = wp_kses($text, $allowed); |
|
272 | 272 | $text = trim($text); |
273 | 273 | return $text; |
274 | 274 | } |
275 | 275 | |
276 | - function code_trick( $text, $markdown ) { // Don't use bbPress native function - it's incompatible with Markdown |
|
276 | + function code_trick($text, $markdown) { // Don't use bbPress native function - it's incompatible with Markdown |
|
277 | 277 | // If doing markdown, first take any user formatted code blocks and turn them into backticks so that |
278 | 278 | // markdown will preserve things like underscores in code blocks |
279 | - if ( $markdown ) |
|
280 | - $text = preg_replace_callback("!(<pre><code>|<code>)(.*?)(</code></pre>|</code>)!s", array( __CLASS__,'decodeit'), $text); |
|
279 | + if ($markdown) |
|
280 | + $text = preg_replace_callback("!(<pre><code>|<code>)(.*?)(</code></pre>|</code>)!s", array(__CLASS__, 'decodeit'), $text); |
|
281 | 281 | |
282 | 282 | $text = str_replace(array("\r\n", "\r"), "\n", $text); |
283 | - if ( !$markdown ) { |
|
283 | + if (!$markdown) { |
|
284 | 284 | // This gets the "inline" code blocks, but can't be used with Markdown. |
285 | - $text = preg_replace_callback("|(`)(.*?)`|", array( __CLASS__, 'encodeit'), $text); |
|
285 | + $text = preg_replace_callback("|(`)(.*?)`|", array(__CLASS__, 'encodeit'), $text); |
|
286 | 286 | // This gets the "block level" code blocks and converts them to PRE CODE |
287 | - $text = preg_replace_callback("!(^|\n)`(.*?)`!s", array( __CLASS__, 'encodeit'), $text); |
|
287 | + $text = preg_replace_callback("!(^|\n)`(.*?)`!s", array(__CLASS__, 'encodeit'), $text); |
|
288 | 288 | } else { |
289 | 289 | // Markdown can do inline code, we convert bbPress style block level code to Markdown style |
290 | - $text = preg_replace_callback("!(^|\n)([ \t]*?)`(.*?)`!s", array( __CLASS__, 'indent'), $text); |
|
290 | + $text = preg_replace_callback("!(^|\n)([ \t]*?)`(.*?)`!s", array(__CLASS__, 'indent'), $text); |
|
291 | 291 | } |
292 | 292 | return $text; |
293 | 293 | } |
294 | 294 | |
295 | - function indent( $matches ) { |
|
295 | + function indent($matches) { |
|
296 | 296 | $text = $matches[3]; |
297 | - $text = preg_replace('|^|m', $matches[2] . ' ', $text); |
|
298 | - return $matches[1] . $text; |
|
297 | + $text = preg_replace('|^|m', $matches[2].' ', $text); |
|
298 | + return $matches[1].$text; |
|
299 | 299 | } |
300 | 300 | |
301 | - function encodeit( $matches ) { |
|
302 | - if ( function_exists('encodeit') ) // bbPress native |
|
303 | - return encodeit( $matches ); |
|
301 | + function encodeit($matches) { |
|
302 | + if (function_exists('encodeit')) // bbPress native |
|
303 | + return encodeit($matches); |
|
304 | 304 | |
305 | 305 | $text = trim($matches[2]); |
306 | 306 | $text = htmlspecialchars($text, ENT_QUOTES); |
@@ -309,14 +309,14 @@ discard block |
||
309 | 309 | $text = str_replace('&lt;', '<', $text); |
310 | 310 | $text = str_replace('&gt;', '>', $text); |
311 | 311 | $text = "<code>$text</code>"; |
312 | - if ( "`" != $matches[1] ) |
|
312 | + if ("`" != $matches[1]) |
|
313 | 313 | $text = "<pre>$text</pre>"; |
314 | 314 | return $text; |
315 | 315 | } |
316 | 316 | |
317 | - function decodeit( $matches ) { |
|
318 | - if ( function_exists('decodeit') ) // bbPress native |
|
319 | - return decodeit( $matches ); |
|
317 | + function decodeit($matches) { |
|
318 | + if (function_exists('decodeit')) // bbPress native |
|
319 | + return decodeit($matches); |
|
320 | 320 | |
321 | 321 | $text = $matches[2]; |
322 | 322 | $trans_table = array_flip(get_html_translation_table(HTML_ENTITIES)); |
@@ -324,7 +324,7 @@ discard block |
||
324 | 324 | $text = str_replace('<br />', '', $text); |
325 | 325 | $text = str_replace('&', '&', $text); |
326 | 326 | $text = str_replace(''', "'", $text); |
327 | - if ( '<pre><code>' == $matches[1] ) |
|
327 | + if ('<pre><code>' == $matches[1]) |
|
328 | 328 | $text = "\n$text\n"; |
329 | 329 | return "`$text`"; |
330 | 330 | } |
@@ -21,8 +21,9 @@ discard block |
||
21 | 21 | function parse_readme_contents( $file_contents ) { |
22 | 22 | $file_contents = str_replace(array("\r\n", "\r"), "\n", $file_contents); |
23 | 23 | $file_contents = trim($file_contents); |
24 | - if ( 0 === strpos( $file_contents, "\xEF\xBB\xBF" ) ) |
|
25 | - $file_contents = substr( $file_contents, 3 ); |
|
24 | + if ( 0 === strpos( $file_contents, "\xEF\xBB\xBF" ) ) { |
|
25 | + $file_contents = substr( $file_contents, 3 ); |
|
26 | + } |
|
26 | 27 | |
27 | 28 | // Markdown transformations |
28 | 29 | $file_contents = preg_replace( "|^###([^#]+)#*?\s*?\n|im", '=$1='."\n", $file_contents ); |
@@ -31,8 +32,10 @@ discard block |
||
31 | 32 | |
32 | 33 | // === Plugin Name === |
33 | 34 | // Must be the very first thing. |
34 | - if ( !preg_match('|^===(.*)===|', $file_contents, $_name) ) |
|
35 | - return array(); // require a name |
|
35 | + if ( !preg_match('|^===(.*)===|', $file_contents, $_name) ) { |
|
36 | + return array(); |
|
37 | + } |
|
38 | + // require a name |
|
36 | 39 | $name = trim($_name[1], '='); |
37 | 40 | $name = $this->sanitize_text( $name ); |
38 | 41 | |
@@ -40,31 +43,36 @@ discard block |
||
40 | 43 | |
41 | 44 | |
42 | 45 | // Requires at least: 1.5 |
43 | - if ( preg_match('|Requires at least:(.*)|i', $file_contents, $_requires_at_least) ) |
|
44 | - $requires_at_least = $this->sanitize_text($_requires_at_least[1]); |
|
45 | - else |
|
46 | - $requires_at_least = NULL; |
|
46 | + if ( preg_match('|Requires at least:(.*)|i', $file_contents, $_requires_at_least) ) { |
|
47 | + $requires_at_least = $this->sanitize_text($_requires_at_least[1]); |
|
48 | + } else { |
|
49 | + $requires_at_least = NULL; |
|
50 | + } |
|
47 | 51 | |
48 | 52 | |
49 | 53 | // Tested up to: 2.1 |
50 | - if ( preg_match('|Tested up to:(.*)|i', $file_contents, $_tested_up_to) ) |
|
51 | - $tested_up_to = $this->sanitize_text( $_tested_up_to[1] ); |
|
52 | - else |
|
53 | - $tested_up_to = NULL; |
|
54 | + if ( preg_match('|Tested up to:(.*)|i', $file_contents, $_tested_up_to) ) { |
|
55 | + $tested_up_to = $this->sanitize_text( $_tested_up_to[1] ); |
|
56 | + } else { |
|
57 | + $tested_up_to = NULL; |
|
58 | + } |
|
54 | 59 | |
55 | 60 | |
56 | 61 | // Stable tag: 10.4-ride-the-fire-eagle-danger-day |
57 | - if ( preg_match('|Stable tag:(.*)|i', $file_contents, $_stable_tag) ) |
|
58 | - $stable_tag = $this->sanitize_text( $_stable_tag[1] ); |
|
59 | - else |
|
60 | - $stable_tag = NULL; // we assume trunk, but don't set it here to tell the difference between specified trunk and default trunk |
|
62 | + if ( preg_match('|Stable tag:(.*)|i', $file_contents, $_stable_tag) ) { |
|
63 | + $stable_tag = $this->sanitize_text( $_stable_tag[1] ); |
|
64 | + } else { |
|
65 | + $stable_tag = NULL; |
|
66 | + } |
|
67 | + // we assume trunk, but don't set it here to tell the difference between specified trunk and default trunk |
|
61 | 68 | |
62 | 69 | |
63 | 70 | // Tags: some tag, another tag, we like tags |
64 | 71 | if ( preg_match('|Tags:(.*)|i', $file_contents, $_tags) ) { |
65 | 72 | $tags = preg_split('|,[\s]*?|', trim($_tags[1])); |
66 | - foreach ( array_keys($tags) as $t ) |
|
67 | - $tags[$t] = $this->sanitize_text( $tags[$t] ); |
|
73 | + foreach ( array_keys($tags) as $t ) { |
|
74 | + $tags[$t] = $this->sanitize_text( $tags[$t] ); |
|
75 | + } |
|
68 | 76 | } else { |
69 | 77 | $tags = array(); |
70 | 78 | } |
@@ -76,18 +84,20 @@ discard block |
||
76 | 84 | $temp_contributors = preg_split('|,[\s]*|', trim($_contributors[1])); |
77 | 85 | foreach ( array_keys($temp_contributors) as $c ) { |
78 | 86 | $tmp_sanitized = $this->user_sanitize( $temp_contributors[$c] ); |
79 | - if ( strlen(trim($tmp_sanitized)) > 0 ) |
|
80 | - $contributors[$c] = $tmp_sanitized; |
|
87 | + if ( strlen(trim($tmp_sanitized)) > 0 ) { |
|
88 | + $contributors[$c] = $tmp_sanitized; |
|
89 | + } |
|
81 | 90 | unset($tmp_sanitized); |
82 | 91 | } |
83 | 92 | } |
84 | 93 | |
85 | 94 | |
86 | 95 | // Donate Link: URL |
87 | - if ( preg_match('|Donate link:(.*)|i', $file_contents, $_donate_link) ) |
|
88 | - $donate_link = esc_url( $_donate_link[1] ); |
|
89 | - else |
|
90 | - $donate_link = NULL; |
|
96 | + if ( preg_match('|Donate link:(.*)|i', $file_contents, $_donate_link) ) { |
|
97 | + $donate_link = esc_url( $_donate_link[1] ); |
|
98 | + } else { |
|
99 | + $donate_link = NULL; |
|
100 | + } |
|
91 | 101 | |
92 | 102 | |
93 | 103 | // togs, conts, etc are optional and order shouldn't matter. So we chop them only after we've grabbed their values. |
@@ -102,17 +112,21 @@ discard block |
||
102 | 112 | |
103 | 113 | |
104 | 114 | // short-description fu |
105 | - if ( !preg_match('/(^(.*?))^[\s]*=+?[\s]*.+?[\s]*=+?/ms', $file_contents, $_short_description) ) |
|
106 | - $_short_description = array( 1 => &$file_contents, 2 => &$file_contents ); |
|
115 | + if ( !preg_match('/(^(.*?))^[\s]*=+?[\s]*.+?[\s]*=+?/ms', $file_contents, $_short_description) ) { |
|
116 | + $_short_description = array( 1 => &$file_contents, 2 => &$file_contents ); |
|
117 | + } |
|
107 | 118 | $short_desc_filtered = $this->sanitize_text( $_short_description[2] ); |
108 | 119 | $short_desc_length = strlen($short_desc_filtered); |
109 | 120 | $short_description = substr($short_desc_filtered, 0, 150); |
110 | - if ( $short_desc_length > strlen($short_description) ) |
|
111 | - $truncated = true; |
|
112 | - else |
|
113 | - $truncated = false; |
|
114 | - if ( $_short_description[1] ) |
|
115 | - $file_contents = $this->chop_string( $file_contents, $_short_description[1] ); // yes, the [1] is intentional |
|
121 | + if ( $short_desc_length > strlen($short_description) ) { |
|
122 | + $truncated = true; |
|
123 | + } else { |
|
124 | + $truncated = false; |
|
125 | + } |
|
126 | + if ( $_short_description[1] ) { |
|
127 | + $file_contents = $this->chop_string( $file_contents, $_short_description[1] ); |
|
128 | + } |
|
129 | + // yes, the [1] is intentional |
|
116 | 130 | |
117 | 131 | // == Section == |
118 | 132 | // Break into sections |
@@ -139,16 +153,18 @@ discard block |
||
139 | 153 | unset($sections[$special_section]); |
140 | 154 | } |
141 | 155 | } |
142 | - if ( isset($final_sections['change_log']) && empty($final_sections['changelog']) ) |
|
143 | - $final_sections['changelog'] = $final_sections['change_log']; |
|
156 | + if ( isset($final_sections['change_log']) && empty($final_sections['changelog']) ) { |
|
157 | + $final_sections['changelog'] = $final_sections['change_log']; |
|
158 | + } |
|
144 | 159 | |
145 | 160 | |
146 | 161 | $final_screenshots = array(); |
147 | 162 | if ( isset($final_sections['screenshots']) ) { |
148 | 163 | preg_match_all('|<li>(.*?)</li>|s', $final_sections['screenshots'], $screenshots, PREG_SET_ORDER); |
149 | 164 | if ( $screenshots ) { |
150 | - foreach ( (array) $screenshots as $ss ) |
|
151 | - $final_screenshots[] = $ss[1]; |
|
165 | + foreach ( (array) $screenshots as $ss ) { |
|
166 | + $final_screenshots[] = $ss[1]; |
|
167 | + } |
|
152 | 168 | } |
153 | 169 | } |
154 | 170 | |
@@ -157,8 +173,9 @@ discard block |
||
157 | 173 | $upgrade_notice = array(); |
158 | 174 | if ( isset($final_sections['upgrade_notice']) ) { |
159 | 175 | $split = preg_split( '#<h4>(.*?)</h4>#', $final_sections['upgrade_notice'], -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY ); |
160 | - for ( $i = 0; $i < count( $split ); $i += 2 ) |
|
161 | - $upgrade_notice[$this->sanitize_text( $split[$i] )] = substr( $this->sanitize_text( $split[$i + 1] ), 0, 300 ); |
|
176 | + for ( $i = 0; $i < count( $split ); $i += 2 ) { |
|
177 | + $upgrade_notice[$this->sanitize_text( $split[$i] )] = substr( $this->sanitize_text( $split[$i + 1] ), 0, 300 ); |
|
178 | + } |
|
162 | 179 | unset( $final_sections['upgrade_notice'] ); |
163 | 180 | } |
164 | 181 | |
@@ -214,8 +231,10 @@ discard block |
||
214 | 231 | } |
215 | 232 | |
216 | 233 | function user_sanitize( $text, $strict = false ) { // whitelisted chars |
217 | - if ( function_exists('user_sanitize') ) // bbPress native |
|
234 | + if ( function_exists('user_sanitize') ) { |
|
235 | + // bbPress native |
|
218 | 236 | return user_sanitize( $text, $strict ); |
237 | + } |
|
219 | 238 | |
220 | 239 | if ( $strict ) { |
221 | 240 | $text = preg_replace('/[^a-z0-9-]/i', '', $text); |
@@ -276,8 +295,9 @@ discard block |
||
276 | 295 | function code_trick( $text, $markdown ) { // Don't use bbPress native function - it's incompatible with Markdown |
277 | 296 | // If doing markdown, first take any user formatted code blocks and turn them into backticks so that |
278 | 297 | // markdown will preserve things like underscores in code blocks |
279 | - if ( $markdown ) |
|
280 | - $text = preg_replace_callback("!(<pre><code>|<code>)(.*?)(</code></pre>|</code>)!s", array( __CLASS__,'decodeit'), $text); |
|
298 | + if ( $markdown ) { |
|
299 | + $text = preg_replace_callback("!(<pre><code>|<code>)(.*?)(</code></pre>|</code>)!s", array( __CLASS__,'decodeit'), $text); |
|
300 | + } |
|
281 | 301 | |
282 | 302 | $text = str_replace(array("\r\n", "\r"), "\n", $text); |
283 | 303 | if ( !$markdown ) { |
@@ -299,8 +319,10 @@ discard block |
||
299 | 319 | } |
300 | 320 | |
301 | 321 | function encodeit( $matches ) { |
302 | - if ( function_exists('encodeit') ) // bbPress native |
|
322 | + if ( function_exists('encodeit') ) { |
|
323 | + // bbPress native |
|
303 | 324 | return encodeit( $matches ); |
325 | + } |
|
304 | 326 | |
305 | 327 | $text = trim($matches[2]); |
306 | 328 | $text = htmlspecialchars($text, ENT_QUOTES); |
@@ -309,14 +331,17 @@ discard block |
||
309 | 331 | $text = str_replace('&lt;', '<', $text); |
310 | 332 | $text = str_replace('&gt;', '>', $text); |
311 | 333 | $text = "<code>$text</code>"; |
312 | - if ( "`" != $matches[1] ) |
|
313 | - $text = "<pre>$text</pre>"; |
|
334 | + if ( "`" != $matches[1] ) { |
|
335 | + $text = "<pre>$text</pre>"; |
|
336 | + } |
|
314 | 337 | return $text; |
315 | 338 | } |
316 | 339 | |
317 | 340 | function decodeit( $matches ) { |
318 | - if ( function_exists('decodeit') ) // bbPress native |
|
341 | + if ( function_exists('decodeit') ) { |
|
342 | + // bbPress native |
|
319 | 343 | return decodeit( $matches ); |
344 | + } |
|
320 | 345 | |
321 | 346 | $text = $matches[2]; |
322 | 347 | $trans_table = array_flip(get_html_translation_table(HTML_ENTITIES)); |
@@ -324,8 +349,9 @@ discard block |
||
324 | 349 | $text = str_replace('<br />', '', $text); |
325 | 350 | $text = str_replace('&', '&', $text); |
326 | 351 | $text = str_replace(''', "'", $text); |
327 | - if ( '<pre><code>' == $matches[1] ) |
|
328 | - $text = "\n$text\n"; |
|
352 | + if ( '<pre><code>' == $matches[1] ) { |
|
353 | + $text = "\n$text\n"; |
|
354 | + } |
|
329 | 355 | return "`$text`"; |
330 | 356 | } |
331 | 357 |