capability = 'administrator';
$this->settings_page = awpp_get_instance()->prefix . '-settings';
$this->settings_option = awpp_get_instance()->prefix . '-option';
$this->settings_group = $this->settings_key . '-group';
$this->settings_section = $this->settings_key . '-section';
$this->adminbar_id = awpp_get_instance()->prefix . '_adminbar';
$this->server_push_choices = [
'disabled' => __( 'Disabled', 'awpp' ),
'php' => __( 'PHP', 'awpp' ),
'htaccess' => __( '.htaccess', 'awpp' ),
];
$this->options = get_option( $this->settings_option );
}
public function run() {
add_action( 'admin_menu', [ $this, 'add_menu_page' ] );
add_action( 'admin_init', [ $this, 'register_settings' ] );
add_action( 'admin_bar_menu', [ $this, 'add_toolbar' ], 90 );
add_action( 'admin_init', [ $this, 'http2_check' ] );
}
public function add_menu_page() {
add_submenu_page( 'options-general.php', awpp_get_instance()->name, awpp_get_instance()->name, $this->capability, $this->settings_page, [ $this, 'register_settings_page' ] );
}
public function register_settings_page() {
?>
settings_section;
register_setting( $this->settings_group, $this->settings_option, [ $this, 'sanitize' ] );
register_setting( $this->settings_group, $this->settings_option, [ $this, 'update_file' ] );
add_settings_section( $section, __( 'Settings', 'awpp' ), [ $this, 'print_section_info' ], $this->settings_page );
add_settings_field( 'scripts_to_footer', __( 'Move all scripts to footer', 'awpp' ), [ $this, 'scripts_to_footer_callback' ], $this->settings_page, $section );
add_settings_field( 'minify', __( 'Minify CSS and JS Files', 'awpp' ), [ $this, 'minify_callback' ], $this->settings_page, $section );
add_settings_field( 'loadcss', __( 'Load CSS async', 'awpp' ), [ $this, 'loadcss_callback' ], $this->settings_page, $section );
add_settings_field( 'serverpush', __( 'HTTP/2 Server Push', 'awpp' ), [ $this, 'serverpush_callback' ], $this->settings_page, $section );
}
public function sanitize( $input ) {
$checkboxes = [ 'scripts_to_footer', 'defer_scripts', 'loadcss', 'minify' ];
foreach ( $checkboxes as $key ) {
if ( ! isset( $input[ $key ] ) ) {
$input[ $key ] = 'off';
}
}
if ( ! array_key_exists( $input['serverpush'], $this->server_push_choices ) ) {
$input['serverpush'] = array_keys( $input['serverpush'] )[0];
}
return $input;
}
public function update_file( $input ) {
if ( apply_filters( 'awpp_critical_dir', awpp_get_instance()->CriticalCSS->default_critical_path ) != awpp_get_instance()->CriticalCSS->default_critical_path ) {
return $input;
}
$css = $input['criticalcss'];
if ( ! isset( $input['criticalcss'] ) ) {
return $input;
}
$path = plugin_dir_path( awpp_get_instance()->file ) . 'Classes/Libs';
require_once $path . '/minify/autoload.php';
require_once $path . '/path-converter/autoload.php';
$minifier = new \MatthiasMullie\Minify\CSS( $css );
$minifier->minify( awpp_get_instance()->CriticalCSS->default_critical_path . 'index.css' );
return $input;
}
public function print_section_info() {
//echo '';
//print_r( $this->options );
//echo ' ';
}
public function scripts_to_footer_callback() {
$key = 'scripts_to_footer';
$val = $this->get_val( $key, 'on' );
printf( ' ', $this->settings_option, $key, ( 'on' == $val ? 'checked' : '' ) );
}
public function minify_callback() {
$key = 'minify';
$val = $this->get_val( $key, 'on' );
printf( ' ', $this->settings_option, $key, ( 'on' == $val ? 'checked' : '' ) );
}
public function loadcss_callback() {
$key = 'loadcss';
$val = $this->get_val( $key, 'on' );
printf( ' ', $this->settings_option, $key, ( 'on' == $val ? 'checked' : '' ) );
?>
CriticalCSS->default_critical_path );
if ( awpp_get_instance()->CriticalCSS->default_critical_path != $path ) {
echo '
' . __( 'Custom critical directory found:', 'awpp' ) . ' ' . $path . '
';
if ( ! is_dir( $path ) ) {
echo '
' . __( 'Folder does not exist!', 'awpp' ) . '
';
}
if ( ! is_file( $path . 'index.css' ) ) {
echo '
' . __( 'index.css does not exist!', 'awpp' ) . '
';
}
} else {
$file = awpp_get_instance()->CriticalCSS->default_critical_path . 'index.css';
if ( ! file_exists( $file ) ) {
fopen( $file, 'w' );
}
$key = 'criticalcss';
$file_url = str_replace( awpp_get_instance()->CriticalCSS->base_path, awpp_get_instance()->CriticalCSS->base_url, $file );
$val = file_get_contents( $file );
printf( '
', $this->settings_option, $key, $val );
echo "
File: $file_url
";
}
?>
get_val( $key, 'disabled' );
echo '';
foreach ( $this->server_push_choices as $choice => $name ) {
echo '' . $name . ' ';
}
echo ' ';
?>
Http2Push->serverpush_possfiles_option );
foreach ( [ 'styles', 'scripts' ] as $type ) {
echo '
' . ucfirst( $type ) . '
';
echo '
';
}
?>
$this->adminbar_id,
'title' => str_replace( 'Advanced ', '', awpp_get_instance()->name ),
'href' => admin_url( 'options-general.php?page=' . $this->settings_page ),
'meta' => [
'class' => awpp_get_instance()->prefix . '-adminbar',
],
];
$wp_admin_bar->add_node( $args );
}
public function http2_check() {
add_action( 'admin_notices', function () {
if ( get_current_screen()->id != 'settings_page_' . $this->settings_page || getenv( 'X_SPDY' ) != '' ) {
return;
}
// translators: To get the maximum out of Advanced WPPerformance you should upgrade to HTTP/2. Currenty your server supports HTTP/1
$message = sprintf( __( 'To get the maximum out of %1$1s you should upgrade to HTTP/2. Currently your server supports %2$2s', 'awpp' ), '' . awpp_get_instance()->name . ' ', $_SERVER['SERVER_PROTOCOL'] );
printf( '', $message );
} );
}
/**
* Helpers
*/
public function get_val( $key, $default = '' ) {
if ( isset( $this->options[ $key ] ) ) {
return $this->options[ $key ];
} else {
return $default;
}
}
}