images
3 years ago
js
3 years ago
admin-bar-settings.php
2 years ago
class-cwvpb-newsletter.php
1 year ago
class-cwvpsb-admin-settings.php
1 year ago
deactivate-feedback.php
1 year ago
helper-function.php
1 year ago
make-better-admin.css
3 years ago
make-better-admin.js
4 years ago
make-better-admin.min.css
2 years ago
make-better-admin.min.js
2 years ago
script.js
1 year ago
script.min.js
1 year ago
style.css
1 year ago
style.min.css
1 year ago
helper-function.php
296 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Helper Functions |
| 5 | * |
| 6 | * @package cwvpb |
| 7 | * @subpackage Helper/Templates |
| 8 | * @copyright Copyright (c) 2016, René Hermenau |
| 9 | * @license http://opensource.org/licenses/gpl-2.0.php GNU Public License |
| 10 | * @since 1.4.0 |
| 11 | */ |
| 12 | // Exit if accessed directly |
| 13 | if ( ! defined( 'ABSPATH' ) ) { |
| 14 | exit; |
| 15 | } |
| 16 | |
| 17 | /** |
| 18 | * Helper method to check if user is in the plugins page. |
| 19 | * |
| 20 | * @author René Hermenau |
| 21 | * @since 1.4.0 |
| 22 | * |
| 23 | * @return bool |
| 24 | */ |
| 25 | function cwv_is_plugins_page() { |
| 26 | if ( function_exists( 'get_current_screen' ) ) { |
| 27 | $screen = get_current_screen(); |
| 28 | if ( is_object( $screen ) ) { |
| 29 | if ( $screen->id == 'plugins' || $screen->id == 'plugins-network' ) { |
| 30 | return true; |
| 31 | } |
| 32 | } |
| 33 | } |
| 34 | return false; |
| 35 | } |
| 36 | |
| 37 | /** |
| 38 | * display deactivation logic on plugins page |
| 39 | * |
| 40 | * @since 1.4.0 |
| 41 | */ |
| 42 | function cwv_add_deactivation_feedback_modal() { |
| 43 | |
| 44 | if ( ! is_admin() && ! cwv_is_plugins_page() ) { |
| 45 | return; |
| 46 | } |
| 47 | |
| 48 | $current_user = wp_get_current_user(); |
| 49 | if ( ! ( $current_user instanceof WP_User ) ) { |
| 50 | $email = ''; |
| 51 | } else { |
| 52 | $email = trim( $current_user->user_email ); |
| 53 | } |
| 54 | |
| 55 | require_once CWVPSB_PLUGIN_DIR . 'includes/admin/deactivate-feedback.php'; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * send feedback via email |
| 60 | * |
| 61 | * @since 1.4.0 |
| 62 | */ |
| 63 | function cwv_send_feedback() { |
| 64 | |
| 65 | if ( ! current_user_can( 'manage_options' ) ) { |
| 66 | wp_send_json( |
| 67 | array( |
| 68 | 'status' => 400, |
| 69 | 'msg' => esc_html__( 'Permission verification failed', 'cwvpsb' ), |
| 70 | ) |
| 71 | ); |
| 72 | } |
| 73 | |
| 74 | if ( isset( $_POST['data'] ) ) { //phpcs:ignore WordPress.Security.NonceVerification.Missing -- Reason: Nonce verification is not required |
| 75 | parse_str( wp_unslash( $_POST['data'] ) , $form ); //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.NonceVerification.Missing -- Reason: Content are sanitized later |
| 76 | } |
| 77 | |
| 78 | |
| 79 | |
| 80 | $text = ''; |
| 81 | if ( isset( $form['cwv_disable_text'] ) ) { |
| 82 | $text = implode( "\n\r", wp_unslash( $form['cwv_disable_text'] ) ); |
| 83 | } |
| 84 | $headers = array(); |
| 85 | |
| 86 | $from = isset( $form['cwv_disable_from'] ) ? $form['cwv_disable_from'] : ''; |
| 87 | if ( $from ) { |
| 88 | $headers[] = "From: $from"; |
| 89 | $headers[] = "Reply-To: $from"; |
| 90 | } |
| 91 | |
| 92 | $subject = isset( $form['cwv_disable_reason'] ) ? $form['cwv_disable_reason'] : '(no reason given)'; |
| 93 | |
| 94 | $subject = $subject . ' - Core Web Vitals & PageSpeed Booster'; |
| 95 | |
| 96 | if ( $subject == 'technical - Core Web Vitals & PageSpeed Booster' ) { |
| 97 | |
| 98 | $text = trim( $text ); |
| 99 | |
| 100 | if ( ! empty( $text ) ) { |
| 101 | |
| 102 | $text = 'technical issue description: ' . $text; |
| 103 | |
| 104 | } else { |
| 105 | |
| 106 | $text = 'no description: ' . $text; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | $success = wp_mail( 'makebetter@magazine3.in', $subject, $text, $headers ); |
| 111 | |
| 112 | wp_die(); |
| 113 | } |
| 114 | add_action( 'wp_ajax_cwv_send_feedback', 'cwv_send_feedback' ); |
| 115 | |
| 116 | |
| 117 | |
| 118 | add_action( 'admin_enqueue_scripts', 'cwv_enqueue_makebetter_email_js' ); |
| 119 | |
| 120 | function cwv_enqueue_makebetter_email_js() { |
| 121 | |
| 122 | if ( ! is_admin() && ! cwv_is_plugins_page() ) { |
| 123 | return; |
| 124 | } |
| 125 | $min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
| 126 | wp_enqueue_script( 'cwv-make-better-js', CWVPSB_PLUGIN_DIR_URI . "includes/admin/make-better-admin{$min}.js", array( 'jquery' ), CWVPSB_VERSION, true ); |
| 127 | wp_localize_script( |
| 128 | 'cwv-make-better-js', |
| 129 | 'cwvpsb_script_vars', |
| 130 | array( |
| 131 | 'nonce' => wp_create_nonce( 'cwvpsb-admin-nonce' ), |
| 132 | ) |
| 133 | ); |
| 134 | wp_enqueue_style( 'cwv-make-better-css', CWVPSB_PLUGIN_DIR_URI . "includes/admin/make-better-admin{$min}.css", false, CWVPSB_VERSION ); |
| 135 | } |
| 136 | |
| 137 | |
| 138 | add_filter( 'admin_footer', 'cwv_add_deactivation_feedback_modal' ); |
| 139 | |
| 140 | |
| 141 | function cwvpbs_get_total_urls() { |
| 142 | |
| 143 | global $wpdb; |
| 144 | $total_count = 0; |
| 145 | $settings = cwvpsb_defaults(); |
| 146 | $urls_to = array(); |
| 147 | if ( isset( $settings['critical_css_on_home'] ) && $settings['critical_css_on_home'] == 1 ) { |
| 148 | $urls_to[] = get_home_url(); |
| 149 | $urls_to[] = get_home_url() . '/'; |
| 150 | $urls_to[] = home_url( '/' ); |
| 151 | $urls_to[] = site_url( '/' ); |
| 152 | } |
| 153 | |
| 154 | $total_count += count( array_unique( $urls_to ) ); |
| 155 | |
| 156 | $post_types = array(); |
| 157 | if ( ! empty( $settings['critical_css_on_cp_type'] ) ) { |
| 158 | foreach ( $settings['critical_css_on_cp_type'] as $key => $value ) { |
| 159 | if ( $value ) { |
| 160 | $post_types[] = $key; |
| 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | if ( ! empty( $post_types ) ) { |
| 166 | $postimp = "'" . implode( "', '", $post_types ) . "'"; |
| 167 | $total_count += $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->posts Where post_status=%s AND post_type IN (%s);", 'publish', $postimp ) ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching |
| 168 | } |
| 169 | |
| 170 | $taxonomy_types = array(); |
| 171 | if ( ! empty( $settings['critical_css_on_tax_type'] ) ) { |
| 172 | foreach ( $settings['critical_css_on_tax_type'] as $key => $value ) { |
| 173 | if ( $value ) { |
| 174 | $taxonomy_types[] = $key; |
| 175 | } |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | if ( ! empty( $taxonomy_types ) ) { |
| 180 | $postimp = "'" . implode( "', '", $taxonomy_types ) . "'"; |
| 181 | |
| 182 | $total_count += $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->term_taxonomy Where taxonomy IN (%s);", $postimp ) ); //phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching |
| 183 | } |
| 184 | |
| 185 | return $total_count; |
| 186 | } |
| 187 | |
| 188 | function cwvpb_get_current_url() { |
| 189 | |
| 190 | $link = 'http'; |
| 191 | |
| 192 | if ( isset( $_SERVER['HTTPS'] ) && $_SERVER['HTTPS'] === 'on' ) { |
| 193 | $link = 'https'; |
| 194 | } |
| 195 | $link .= '://'; |
| 196 | |
| 197 | if ( isset( $_SERVER['HTTP_HOST'] ) ) { |
| 198 | $link .= wp_unslash( $_SERVER['HTTP_HOST'] ); //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason: Sanitization not required |
| 199 | } |
| 200 | |
| 201 | if ( isset( $_SERVER['REQUEST_URI'] ) ) { |
| 202 | $link .= wp_unslash( $_SERVER['REQUEST_URI'] ); //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason: Sanitization not required |
| 203 | } |
| 204 | return $link; |
| 205 | } |
| 206 | |
| 207 | add_action( 'wp_ajax_cwvpsb_send_query_message', 'cwvpsb_send_query_message' ); |
| 208 | |
| 209 | function cwvpsb_sanitize_textarea_field( $str ) { |
| 210 | |
| 211 | if ( is_object( $str ) || is_array( $str ) ) { |
| 212 | return ''; |
| 213 | } |
| 214 | |
| 215 | $str = (string) $str; |
| 216 | |
| 217 | $filtered = wp_check_invalid_utf8( $str ); |
| 218 | |
| 219 | if ( strpos( $filtered, '<' ) !== false ) { |
| 220 | $filtered = wp_pre_kses_less_than( $filtered ); |
| 221 | // This will strip extra whitespace for us. |
| 222 | $filtered = wp_strip_all_tags( $filtered, false ); |
| 223 | |
| 224 | // Use HTML entities in a special case to make sure no later |
| 225 | // newline stripping stage could lead to a functional tag. |
| 226 | $filtered = str_replace( "<\n", "<\n", $filtered ); |
| 227 | } |
| 228 | |
| 229 | $filtered = trim( $filtered ); |
| 230 | |
| 231 | $found = false; |
| 232 | while ( preg_match( '/%[a-f0-9]{2}/i', $filtered, $match ) ) { |
| 233 | $filtered = str_replace( $match[0], '', $filtered ); |
| 234 | $found = true; |
| 235 | } |
| 236 | |
| 237 | if ( $found ) { |
| 238 | // Strip out the whitespace that may now exist after removing the octets. |
| 239 | $filtered = trim( preg_replace( '/ +/', ' ', $filtered ) ); |
| 240 | } |
| 241 | |
| 242 | return $filtered; |
| 243 | } |
| 244 | |
| 245 | function cwvpsb_send_query_message() { |
| 246 | |
| 247 | if ( ! isset( $_POST['cwvpsb_wpnonce'] ) ) { |
| 248 | return; |
| 249 | } |
| 250 | if ( ! wp_verify_nonce( wp_unslash( $_POST['cwvpsb_wpnonce'] ), 'cwvpsb-admin-nonce' ) ) { //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason: using custom Nonce verification |
| 251 | return; |
| 252 | } |
| 253 | |
| 254 | if ( ! current_user_can( 'manage_options' ) ) { |
| 255 | return; |
| 256 | } |
| 257 | $message = isset( $_POST['message'] ) ? cwvpsb_sanitize_textarea_field( wp_unslash( $_POST['message'] ) ) : ''; //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Reason: Sanitization is done using cwvpsb_sanitize_textarea_field |
| 258 | $email = isset( $_POST['email'] ) ? sanitize_email( wp_unslash( $_POST['email'] ) ) : ''; |
| 259 | |
| 260 | if ( function_exists( 'wp_get_current_user' ) ) { |
| 261 | |
| 262 | $user = wp_get_current_user(); |
| 263 | |
| 264 | $message = '<p>' . $message . '</p><br><br>' . 'Query from Core Web Vitals & PageSpeed Booster plugin support tab'; |
| 265 | |
| 266 | $user_data = $user->data; |
| 267 | $user_email = $user_data->user_email; |
| 268 | |
| 269 | if ( $email ) { |
| 270 | $user_email = $email; |
| 271 | } |
| 272 | // php mailer variables |
| 273 | $sendto = 'team@magazine3.in'; |
| 274 | $subject = 'Core Web Vitals & PageSpeed Booster Query'; |
| 275 | |
| 276 | $headers[] = 'Content-Type: text/html; charset=UTF-8'; |
| 277 | $headers[] = 'From: ' . esc_attr( $user_email ); |
| 278 | $headers[] = 'Reply-To: ' . esc_attr( $user_email ); |
| 279 | // Load WP components, no themes. |
| 280 | |
| 281 | $sent = wp_mail( $sendto, $subject, $message, $headers ); |
| 282 | |
| 283 | if ( $sent ) { |
| 284 | |
| 285 | wp_send_json( array( 'status' => 't' ) ); |
| 286 | |
| 287 | } else { |
| 288 | |
| 289 | wp_send_json( array( 'status' => 'f' ) ); |
| 290 | |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | wp_die(); |
| 295 | } |
| 296 |