. * */ /** * Constants */ define( 'ASYNC_SOCIAL_SHARING_PLUGIN_PATH', plugin_dir_path( __FILE__ ) ); define( 'ASYNC_SOCIAL_SHARING_PLUGIN_URL', plugins_url( '', __FILE__ ) ); class Async_Social_Sharing { private static $instance; /** * Initializes Async_Social_Sharing Class * * @return Object new Async_Social_Sharing Class(); */ public static function get_instance() { if ( ! self::$instance ) { self::$instance = new self(); } return self::$instance; } private function __construct() { add_action( 'init', array( $this, 'init' ) ); add_action( 'init', array( $this, 'register_styles' ) ); } public function init() { add_action( 'admin_init', array( $this, 'register_options' ) ); add_action( 'admin_menu', array( $this, 'add_options_page' ) ); add_filter( 'plugin_action_links', array( $this, 'add_settings_link' ), 10, 2 ); add_action( 'wp_enqueue_scripts', array( $this, 'load_scripts' ) ); add_filter( 'the_content', array( $this, 'display_check' ) ); } public function register_options() { register_setting( 'async_share_plugin_options', 'async_share_options', array( $this, 'validate_options' ) ); } public function get_options() { return get_option( 'async_share_options' ); } public static function validate_options( $input ) { sanitize_text_field( $input ); return $input; } public function add_options_page() { $page = add_options_page( 'Async Social Sharing Options', 'Async Sharing Options', 'manage_options', __FILE__, array( $this, 'display_admin_view' ) ); add_action( 'admin_print_styles-' . $page, array( $this, 'load_admin_styles' ) ); } // Defining admin style public function load_admin_styles() { $cache_buster = filemtime( ASYNC_SOCIAL_SHARING_PLUGIN_PATH . 'assets/css/async-admin.css' ); wp_enqueue_style( 'async_admin', ASYNC_SOCIAL_SHARING_PLUGIN_URL . '/assets/css/async-admin.css', false, $cache_buster, 'all' ); } // Render the Plugin options form public function display_admin_view() { ob_start(); return include ASYNC_SOCIAL_SHARING_PLUGIN_PATH . 'views/admin-view.php'; } public function add_settings_link( $links, $file ) { $plugin_file_name = plugin_basename( __FILE__ ); if ( $file == $plugin_file_name ) { $links[] = '' . __( 'Settings' ) . ''; } return $links; } /** * Registers included CSS for front-end display of social widgets * * Initialized by init hook */ public function register_styles() { $cache_buster = filemtime( ASYNC_SOCIAL_SHARING_PLUGIN_PATH . 'assets/css/async-share.css' ); wp_register_style( 'async_css', ASYNC_SOCIAL_SHARING_PLUGIN_URL . '/assets/css/async-share.css', false, $cache_buster, 'all' ); } /** * Loads JavaScript and optionally CSS for front-end display of social widgets * * Initialized by wp_enqueue_scripts hook */ public function load_scripts() { $options = $this->get_options(); $cache_buster = filemtime( ASYNC_SOCIAL_SHARING_PLUGIN_PATH . 'assets/js/async-share.js' ); wp_enqueue_script( 'async_js', ASYNC_SOCIAL_SHARING_PLUGIN_URL . '/assets/js/async-share.js', array( 'jquery' ), $cache_buster, true ); if ( ! isset( $options['disable_css'] ) ) { wp_enqueue_style( 'async_css' ); } } public function display_output_view() { $options = $this->get_options(); if ( ! empty( $options ) ) { ob_start(); include( ASYNC_SOCIAL_SHARING_PLUGIN_PATH . 'views/output-view.php' ); return ob_get_clean(); } return; } /** * Controls which template files the social widgets are displayed upon */ public function display_check( $content ) { $async_share_output = $this->display_output_view(); $options = $this->get_options(); // return early instead of hooking into feed content. if ( is_feed() ) { return $content; } if ( is_page() ) { if ( is_array( $options['types'] ) && in_array( 'page', $options['types'] ) ) { return $content . $async_share_output; } return $content; } if ( is_home() || is_paged() ) { if ( is_array( $options ) && isset( $options['paged'] ) ) { return $content . $async_share_output; } return $content; } if ( is_single() ) { $cpt = get_post_type(); if ( is_array( $options['types'] ) && in_array( $cpt, $options['types'] ) ) { return $content . $async_share_output; } return $content; } return $content; } } Async_Social_Sharing::get_instance();