*/ class AdUnblocker_Public { /** * The ID of this plugin. * * @since 1.0.0 * @access private * @var string $plugin_name The ID of this plugin. */ private $plugin_name; /** * The version of this plugin. * * @since 1.0.0 * @access private * @var string $version The current version of this plugin. */ private $version; /** * The version of this plugin. * * @since 1.0.0 * @access private * @var string $version The current version of this plugin. */ private $file_name; private $settings; /** * Initialize the class and set its properties. * * @since 1.0.0 * @param string $plugin_name The name of the plugin. * @param string $version The version of this plugin. */ public function __construct( $plugin_name, $version ) { $this->plugin_name = $plugin_name; $this->version = $version; $this->settings = $this->get_options_data(); } /** * Register the stylesheets for the public-facing side of the site. * * @since 1.0.0 */ public function enqueue_styles() { /** * This function is provided for demonstration purposes only. * * An instance of this class should be passed to the run() function * defined in AdUnblocker_Loader as all of the hooks are defined * in that particular class. * * The AdUnblocker_Loader will then create the relationship * between the defined hooks and the functions defined in this * class. */ if( array_key_exists( $this->plugin_name . '-file-name', $this->settings ) && array_key_exists( $this->plugin_name . '-status', $this->settings )) { if ( $this->settings[$this->plugin_name . '-status'] == 'y' ) { wp_enqueue_style( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'css/' . $this->settings[$this->plugin_name . '-file-name'] . '.css', array(), $this->version, 'all' ); } } } /** * Register the JavaScript for the public-facing side of the site. * * @since 1.0.0 */ public function enqueue_scripts() { /** * This function is provided for demonstration purposes only. * * An instance of this class should be passed to the run() function * defined in AdUnblocker_Loader as all of the hooks are defined * in that particular class. * * The AdUnblocker_Loader will then create the relationship * between the defined hooks and the functions defined in this * class. */ if( array_key_exists( $this->plugin_name . '-file-name', $this->settings ) && array_key_exists( $this->plugin_name . '-status', $this->settings ) ) { if ( $this->settings[$this->plugin_name . '-status'] == 'y' ) { wp_enqueue_script( $this->plugin_name, plugin_dir_url( __FILE__ ) . 'js/' . $this->settings[$this->plugin_name . '-file-name'] . '.js', array( 'jquery' ), $this->version, false ); } } } /** * Register the JavaScript for the public-facing side of the site. * * @since 1.0.0 */ public function localize_script() { $nonces = apply_filters( 'daau_nonces', array( 'get_plugin_data' => wp_create_nonce( 'get-plugin-data' ) ) ); $data = apply_filters( 'daau_data', array( 'this_url' => esc_html( addslashes( home_url() ) ) . '/wp-admin/admin-ajax.php', 'nonces' => $nonces ) ); // wp_localize_script( $handle, $name, $data ); wp_localize_script( $this->plugin_name, 'daau_app', $data ); } /** * Register the JavaScript for the public-facing side of the site. * * @since 1.0.0 */ public function ajax_request_callback() { $this->check_ajax_referer( 'get-plugin-data' ); wp_send_json( $this->settings ); end_ajax(); } /** * @param mixed $return Value to be returned as response. * * @return null */ function end_ajax( $return = false ) { $return = apply_filters( 'daau_before_response', $return ); echo ( false === $return ) ? '' : $return; exit; } function check_ajax_referer( $action ) { $result = check_ajax_referer( $action, 'nonce', false ); if ( false === $result ) { $return = array( 'daau_error' => 1, 'body' => sprintf( __( 'Invalid nonce for: %s', 'adunblocker' ), $action ) ); $this->end_ajax( json_encode( $return ) ); } } public function get_options_data() { $settings = array(); $settings = get_option( $this->plugin_name . '-options' ); $settings[$this->plugin_name . '-content'] = wpautop($settings[$this->plugin_name . '-content']); return $settings; } /** * Returns the absolute path to the root of the website. * * @return string */ function get_absolute_root_file_path() { static $absolute_path; if ( ! empty( $absolute_path ) ) { return $absolute_path; } $absolute_path = rtrim( ABSPATH, '\\/' ); $site_url = rtrim( site_url( '', 'http' ), '\\/' ); $home_url = rtrim( home_url( '', 'http' ), '\\/' ); if ( $site_url != $home_url ) { $difference = str_replace( $home_url, '', $site_url ); if ( strpos( $absolute_path, $difference ) !== false ) { $absolute_path = rtrim( substr( $absolute_path, 0, - strlen( $difference ) ), '\\/' ); } } return $absolute_path; } /** * Get the domain for the current site. * * @return string */ function get_domain_current_site() { if ( ! is_multisite() ) { return ''; } $current_site = get_current_site(); return $current_site->domain; } }