self::convert_date(), 'option' => self::$criticalapi_filesmatch_option, ]; // translators: Critical CSS for "{key}" ({url}) generated awpp_exit_ajax( 'success', sprintf( __( 'Critical CSS for "%1$s" (%2$s) generated.', 'awpp' ), $key, $url ), $data ); } public function ajax_delete() { $key = sanitize_title( $_POST['critical_key'] ); $dir = self::get_critical_dir(); $file = $dir . $key . '.css'; unlink( $file ); awpp_exit_ajax( 'success', 'deleted' ); } /** * Helpers */ protected function apikey_set() { if ( ! defined( 'AWPP_CRITICALAPI' ) || ! AWPP_CRITICALAPI ) { return false; } $options = get_option( self::$awpp_settings_key ); if ( isset( $options[ self::$apikey_key ] ) && '' != $options[ self::$apikey_key ] ) { return true; } return false; } protected function do_request( $url, $data = [] ) { if ( empty( $data ) ) { return [ 'status' => 'error', 'message' => __( 'Invalid data', 'awpp' ), ]; } if ( ! function_exists( 'curl_version' ) ) { return [ 'status' => 'error', 'message' => __( 'curl is not enabled on your server', 'awpp' ), ]; } $data_string = json_encode( $data ); $data_string = htmlspecialchars_decode( $data_string ); $ch = curl_init( $url ); curl_setopt( $ch, CURLOPT_CUSTOMREQUEST, 'POST' ); curl_setopt( $ch, CURLOPT_POSTFIELDS, $data_string ); curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true ); curl_setopt( $ch, CURLOPT_SSL_VERIFYPEER, true ); curl_setopt( $ch, CURLOPT_HTTPHEADER, [ 'Content-Type: application/json', 'Content-Length: ' . strlen( $data_string ), ] ); $content = curl_exec( $ch ); $http_code = curl_getinfo( $ch, CURLINFO_HTTP_CODE ); curl_close( $ch ); return [ 'status' => $http_code, 'message' => $content, //'message' => $content . ' data: ' . $data_string, // debugging ]; } protected function get_dimensions() { $dimensions = [ 'desktop' => [ 'width' => 1200, 'height' => 800, ], 'mobile' => [ 'width' => 700, 'height' => 300, ], ]; $dimensions = apply_filters( 'awpp_criticalapi_dimensions', $dimensions ); $i = 0; $return = []; foreach ( $dimensions as $device => $vals ) { if ( ! array_key_exists( 'width', $vals ) || ! array_key_exists( 'height', $vals ) ) { continue; } if ( 0 == intval( $vals['width'] ) || 0 == intval( $vals['height'] ) ) { continue; } if ( $i >= 2 ) { continue; } $i ++; $return[ $device ] = [ 'width' => intval( $vals['width'] ), 'height' => intval( $vals['height'] ), ]; } return $return; } protected function get_critical_dir() { $dir = ABSPATH . 'wp-content/cache/awpp/criticalapi/'; if ( is_multisite() ) { $dir = $dir . get_current_blog_id() . '/'; } if ( ! is_dir( $dir ) ) { mkdir( $dir, 0777, true ); } return $dir; } protected function fetch_css( $url, $api_key = '' ) { if ( '' == $api_key ) { $api_key = awpp_get_setting( self::$apikey_key ); } $atts = [ 'apiKey' => $api_key, 'url' => $url, 'dimensions' => self::get_dimensions(), ]; return self::do_request( 'https://api.critical-css.io', $atts ); } protected function get_all_critical_elements() { $elements = []; $elements['front-page'] = [ 'name' => __( 'Front Page', 'awpp' ), 'url' => get_home_url(), ]; /** * All Singular */ foreach ( self::get_post_types() as $key => $name ) { $posts = get_posts( [ 'posts_per_page' => - 1, 'post_type' => $key, ] ); $elements[ 'singular-' . $key ] = [ 'name' => $name, 'elements' => [], ]; foreach ( $posts as $post ) { $elements[ 'singular-' . $key ]['elements'][ 'singular-' . $post->ID ] = [ 'name' => get_the_title( $post ), 'url' => get_permalink( $post->ID ), ]; } } /** * All Taxonomies */ foreach ( self::get_taxonomies() as $key => $name ) { $terms = get_terms( $key, [ 'hide_empty' => true, ] ); $elements[ 'archive-taxonomy-' . $key ] = [ 'name' => $name, 'elements' => [], ]; foreach ( $terms as $term ) { $elements[ 'archive-taxonomy-' . $key ]['elements'][ 'archive-taxonomy-' . $term->term_id ] = [ 'name' => apply_filters( 'the_title', $term->name ), 'url' => get_term_link( $term ), ]; } } /** * All Users */ $elements['archive-author'] = [ 'name' => __( 'Author Pages', 'awpp' ), 'elements' => [], ]; foreach ( get_users() as $user ) { $elements['archive-author']['elements'][ 'archive-author-' . $user->user_nicename ] = [ 'name' => $user->display_name, 'url' => get_author_posts_url( $user->ID ), ]; } return $elements; } protected function get_post_types() { $post_types = []; $post_types_objects = get_post_types( [ 'public' => true, ], 'objects' ); foreach ( $post_types_objects as $pt => $object ) { if ( 'attachment' == $pt ) { continue; } $post_types[ $pt ] = $object->labels->name; } return $post_types; } protected function get_taxonomies() { $taxonomies = []; foreach ( self::get_post_types() as $pt => $name ) { $post_taxonomies_objects = get_object_taxonomies( $pt, 'objects' ); foreach ( $post_taxonomies_objects as $tax => $tax_object ) { if ( ! $tax_object->show_ui ) { continue; } $taxonomies[ $tax ] = $tax_object->labels->name; } } return $taxonomies; } protected function convert_date( $timestamp = '', $type = 'datetime' ) { if ( '' == $timestamp ) { $timestamp = time(); } switch ( $type ) { case 'date': return date( get_option( 'date_format' ), $timestamp ); break; case 'time': return date( get_option( 'time_format' ), $timestamp ); break; default: return date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $timestamp ); break; } } /** * Render */ protected function render_criticalapi_generate_list( $critical_key, $title, $urls ) { $file = self::get_critical_dir() . $critical_key . '.css'; $has_file = file_exists( $file ); $saved_option = get_option( self::$criticalapi_filesmatch_option ); $saved_url = ''; if ( is_array( $saved_option ) && array_key_exists( $critical_key, $saved_option ) ) { $saved_url = $saved_option[ $critical_key ]; } $return = '
' . $title . '
'; if ( is_array( $urls ) ) { $return .= ''; $return .= ''; } elseif ( '' == $urls ) { $return .= ''; $return .= ''; } else { $return .= ''; $return .= ''; } // End if(). $return .= '