file = $file; $this->dir = dirname( $file ); $this->basename = plugin_basename( $file ); add_action( 'plugins_loaded', [ $this, 'plugins_loaded' ] ); add_action( 'admin_post_ic-move-assets', [ $this, 'move_assets' ] ); add_action( 'admin_post_ic-reset-assets-settings', [ $this, 'reset_settings' ] ); add_action( 'template_redirect', [ $this, 'init' ] ); register_activation_hook( $file, [ $this, 'activation' ] ); } public function plugins_loaded() { load_plugin_textdomain( 'assets-to-footer', false, dirname( $this->basename ) . '/languages' ); } public function activation() { global $wp_version; __( 'Assets to footer', 'assets-to-footer' ); __( 'Moves scripts and styles to the footer to decrease page load times. You can move all or specific styles and scripts to footer.', 'assets-to-footer' ); //Check WordPress Version if ( version_compare( $wp_version, '4.1', '<' ) ) { deactivate_plugins( $this->basename ); wp_die( __( 'This plugin requires WordPress in version at least 4.1', 'assets-to-footer' ) ); } //Check PHP Version if ( version_compare( phpversion(), '5.4', '<' ) ) { deactivate_plugins( $this->basename ); wp_die( __( 'This plugin requires PHP in version at least 5.4', 'assets-to-footer' ) ); } } /** * Save settings */ public function move_assets() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( __( 'Sorry, you are not allowed to do that.', 'assets-to-footer' ) ); } check_admin_referer( 'ic-move-assets' ); //Check type of asset if ( ! isset( $_GET['type'] ) || ! in_array( $_GET['type'], [ 'css', 'js' ] ) ) { wp_die( __( 'Invalid type', 'assets-to-footer' ) ); } //Check current position of handle if ( ! isset( $_GET['position'] ) || ! in_array( $_GET['position'], [ 'header', 'footer' ] ) ) { wp_die( __( 'Invalid position', 'assets-to-footer' ) ); } //Check exist handle if ( ! isset( $_GET['handle'] ) || empty( $_GET['handle'] ) ) { wp_die( __( 'Invalid handle', 'assets-to-footer' ) ); } $options = $this->get_options(); foreach ( explode( ',', $_GET['handle'] ) AS $handle ) { $options[ $_GET['type'] ][ $handle ]['all'] = $_GET['position'] === 'footer' ? 'header' : 'footer'; } update_option( 'ic_assets_positions', $options, 1 ); wp_redirect( wp_get_referer() ); die(); } /** * Reset settings */ public function reset_settings() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( __( 'Sorry, you are not allowed to do that.', 'assets-to-footer' ) ); } check_admin_referer( 'ic-reset-assets-settings' ); delete_option( 'ic_assets_positions' ); wp_redirect( wp_get_referer() ); die(); } /** * Register actions */ public function init() { if ( current_user_can( 'manage_options' ) ) { add_action( 'admin_bar_menu', [ $this, 'admin_bar_menu' ], 100 ); add_action( 'wp_enqueue_scripts', [ $this, 'wp_enqueue_scripts' ] ); } add_action( 'wp_footer', [ $this, 'footer' ], 0 ); add_filter( 'style_loader_tag', [ $this, 'script_loader_tag' ], 10000, 2 ); add_filter( 'script_loader_tag', [ $this, 'script_loader_tag' ], 10000, 2 ); } public function wp_enqueue_scripts() { $file = 'assets/dist/css/style.css'; wp_enqueue_style( 'ic-a2f', $this->get_plugin_url( $file ), [], $this->get_file_timestamp( $file ) ); $file = 'assets/dist/js/app.min.js'; wp_enqueue_script( 'ic-a2f', $this->get_plugin_url( $file ), [ 'jquery' ], $this->get_file_timestamp( $file ), 1 ); } /** * Filters the HTML script tag of an enqueued script. * * @param string $html The `