1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Temporary options page for AO26, will integrate CCSS functionality in next release. |
4
|
|
|
*/ |
5
|
|
|
|
6
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
7
|
|
|
exit; |
8
|
|
|
} |
9
|
|
|
|
10
|
|
|
class autoptimizeCriticalCSSSettings { |
11
|
|
|
/** |
12
|
|
|
* Options. |
13
|
|
|
* |
14
|
|
|
* @var bool |
15
|
|
|
*/ |
16
|
|
|
private $settings_screen_do_remote_http = true; |
17
|
|
|
|
18
|
|
|
public function __construct() |
19
|
|
|
{ |
20
|
|
|
$this->settings_screen_do_remote_http = apply_filters( 'autoptimize_settingsscreen_remotehttp', $this->settings_screen_do_remote_http ); |
21
|
|
|
$this->run(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
protected function enabled() |
25
|
|
|
{ |
26
|
|
|
return apply_filters( 'autoptimize_filter_show_criticalcsss_tabs', true ); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
View Code Duplication |
public function run() |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
if ( $this->enabled() ) { |
32
|
|
|
add_filter( 'autoptimize_filter_settingsscreen_tabs', array( $this, 'add_critcss_tabs' ), 10, 1 ); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
if ( is_multisite() && is_network_admin() && autoptimizeOptionWrapper::is_ao_active_for_network() ) { |
36
|
|
|
add_action( 'network_admin_menu', array( $this, 'add_critcss_admin_menu' ) ); |
37
|
|
|
} else { |
38
|
|
|
add_action( 'admin_menu', array( $this, 'add_critcss_admin_menu' ) ); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function add_critcss_tabs( $in ) |
43
|
|
|
{ |
44
|
|
|
$in = array_merge( $in, array( 'ao_critcss' => '⚡ ' . __( 'Critical CSS', 'autoptimize' ) ) ); |
45
|
|
|
|
46
|
|
|
return $in; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function add_critcss_admin_menu() |
50
|
|
|
{ |
51
|
|
|
if ( $this->enabled() ) { |
52
|
|
|
add_submenu_page( null, 'Critical CSS', 'Critical CSS', 'manage_options', 'ao_critcss', array( $this, 'ao_criticalcsssettings_page' ) ); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function ao_criticalcsssettings_page() |
57
|
|
|
{ |
58
|
|
|
?> |
59
|
|
|
<style> |
60
|
|
|
.ao_settings_div {background: white;border: 1px solid #ccc;padding: 1px 15px;margin: 15px 10px 10px 0;} |
61
|
|
|
.ao_settings_div .form-table th {font-weight: normal;} |
62
|
|
|
</style> |
63
|
|
|
<script>document.title = "Autoptimize: <?php _e( 'Critical CSS', 'autoptimize' ); ?> " + document.title;</script> |
64
|
|
|
<div class="wrap"> |
65
|
|
|
<h1><?php _e( 'Autoptimize Settings', 'autoptimize' ); ?></h1> |
66
|
|
|
<?php echo autoptimizeConfig::ao_admin_tabs(); ?> |
67
|
|
|
<div class="ao_settings_div"> |
68
|
|
|
<?php |
69
|
|
|
$ccss_explanation = ''; |
70
|
|
|
|
71
|
|
|
// get the HTML with the explanation of what critical CSS is. |
72
|
|
View Code Duplication |
if ( $this->settings_screen_do_remote_http ) { |
|
|
|
|
73
|
|
|
$ccss_explanation = get_transient( 'ccss_explain_ao26' ); |
74
|
|
|
if ( empty( $ccss_explanation ) ) { |
75
|
|
|
$ccss_expl_resp = wp_remote_get( 'https://misc.optimizingmatters.com/autoptimize_ccss_explain_ao26.html?ao_ver=' . AUTOPTIMIZE_PLUGIN_VERSION ); |
76
|
|
|
if ( ! is_wp_error( $ccss_expl_resp ) ) { |
77
|
|
|
if ( '200' == wp_remote_retrieve_response_code( $ccss_expl_resp ) ) { |
78
|
|
|
$ccss_explanation = wp_kses_post( wp_remote_retrieve_body( $ccss_expl_resp ) ); |
79
|
|
|
set_transient( 'ccss_explain_ao26', $ccss_explanation, WEEK_IN_SECONDS ); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// placeholder text in case HTML is empty. |
86
|
|
|
if ( empty( $ccss_explanation ) ) { |
87
|
|
|
$ccss_explanation = '<h2>Fix render-blocking CSS!</h2><p>Significantly improve your first-paint times by making CSS non-render-blocking.</p><br /><a href="./plugin-install.php?s=autoptimize+criticalcss&tab=search&type=term" class="button">Install the "Autoptimize Critical CSS Power-Up"!</a>'; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// and echo it. |
91
|
|
|
echo $ccss_explanation . '<p> </p>'; |
92
|
|
|
?> |
93
|
|
|
</div> |
94
|
|
|
</div> |
95
|
|
|
<?php |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.