$widgets ) { $sidebar_options[ $i ] = array( 'sidebar' => $sidebar, 'use_sticky_position' => $use_sticky_position, 'margin_top' => $options['margin-top'], 'margin_bottom' => $options['margin-bottom'], 'stop_elements_selectors' => isset($options['stop-id']) && $options['stop-id'] != '' && (!isset($options['stop_elements_selectors']) || $options['stop_elements_selectors'] == '') ? $options['stop-id']: $options['stop_elements_selectors'], 'screen_max_width' => $options['screen-max-width'], 'screen_max_height' => $options['screen-max-height'], 'widgets' => array_values( $widgets ), ); $i++; } $sidebar_options = apply_filters( 'q2w3-fixed-widget-sidebar-options', $sidebar_options ); } else { $sidebar_options[ 0 ] = array( 'use_sticky_position' => $use_sticky_position, 'margin_top' => $options['margin-top'], 'margin_bottom' => $options['margin-bottom'], 'stop_elements_selectors' => isset($options['stop-id']) && $options['stop-id'] != '' && (!isset($options['stop_elements_selectors']) || $options['stop_elements_selectors'] == '') ? $options['stop-id']: $options['stop_elements_selectors'], 'screen_max_width' => $options['screen-max-width'], 'screen_max_height' => $options['screen-max-height'], 'widgets' => array(), ); } wp_localize_script( self::ID, 'q2w3_sidebar_options', $sidebar_options ); } /** * Gather fixed widgets */ protected static function fixed_wigets() { $sidebars = wp_get_sidebars_widgets(); if ( $sidebars && is_array( $sidebars ) ) { foreach ( $sidebars as $sidebar_id => $sidebar_widgets ) { if ( ! ( stristr( $sidebar_id, 'orphaned_widgets' ) !== false || $sidebar_id === 'wp_inactive_widgets' ) ) { if ( $sidebar_widgets && is_array( $sidebar_widgets ) ) { foreach ( $sidebar_widgets as $widget ) { $widget_id = substr( strrchr( $widget, '-' ), 1 ); $widget_type = stristr( $widget, '-' . $widget_id, true ); $widget_options = get_option( 'widget_' . $widget_type ); if ( isset( $widget_options[ $widget_id ]['q2w3_fixed_widget'] ) && $widget_options[ $widget_id ]['q2w3_fixed_widget'] ) { self::$fixed_widgets[ $sidebar_id ][ $widget ] = "#".$widget; } } } } } } } /** * Prepare fixed widget output * * @param array $instance widget instance. * @param WP_Widget $widget specific widget object. * @param array $args additional widget arguments. * * @return array */ public static function display_fixed_widget( $instance, $widget, $args ) { if ( self::is_amp() ) { return $instance; } if ( isset( $instance['q2w3_fixed_widget'] ) && $instance['q2w3_fixed_widget'] && isset( $args['id'] ) && ! isset( self::$fixed_widgets[ $args['id'] ][ $widget->id ] ) ) { self::$fixed_widgets[ $args['id'] ][ $widget->id ] = $widget->id; self::wp_localize_script(); } return $instance; } /** * Fix elements which IDs are given in the Custom ID option in the Fixed Widget settings * technically, they are not widgets, though FW can handle any element */ protected static function custom_ids() { $options = self::load_options(); if ( isset( $options['custom-ids'] ) && $options['custom-ids'] ) { $ids = explode( PHP_EOL, $options['custom-ids'] ); foreach ( $ids as $id ) { $id = trim( $id ); if ( $id ) { self::$fixed_widgets[ self::get_widget_sidebar( $id ) ][ $id ] = $id; } } } } /** * Return the sidebar that hosts a widget. * * @param string $widget_id Widget ID * * @return int|string */ protected static function get_widget_sidebar( $widget_id ) { if ( ! self::$sidebars_widgets ) { self::$sidebars_widgets = wp_get_sidebars_widgets(); unset( self::$sidebars_widgets['wp_inactive_widgets'] ); } if ( is_array( self::$sidebars_widgets ) ) { foreach ( self::$sidebars_widgets as $sidebar => $widgets ) { $key = array_search( $widget_id, $widgets ); if ( $key !== false ) { return $sidebar; } } } return 'q2w3-default-sidebar'; } /** * Render the option field displayed in the widget form * * @param WP_Widget $widget Widget object. * @param string $return unused. * @param array $instance Widget instance. */ public static function add_widget_option( $widget, $return, $instance ) { if ( isset( $instance['q2w3_fixed_widget'] ) ) { $iqfw = $instance['q2w3_fixed_widget']; } else { $iqfw = 0; } echo '

' . PHP_EOL; echo '' . PHP_EOL; echo '' . PHP_EOL; echo '

' . PHP_EOL; } /** * Save widget options * * @param array $instance Widget instance. * @param array $new_instance old widget properties. * @param array $old_instance new widget properties. * * @return array */ public static function update_widget_option( $instance, $new_instance, $old_instance ) { if ( isset( $new_instance['q2w3_fixed_widget'] ) && $new_instance['q2w3_fixed_widget'] ) { $instance['q2w3_fixed_widget'] = 1; } else { $instance['q2w3_fixed_widget'] = false; } return $instance; } /** * Load Fixed Widget settings page */ public static function admin_menu() { remove_action( 'admin_menu', array( 'q2w3_fixed_widget', 'admin_menu' ) ); // Remove free version plugin self::$settings_page_hook = add_submenu_page( 'themes.php', esc_html__( 'Fixed Widget', 'q2w3-fixed-widget' ), esc_html__( 'Fixed Widget', 'q2w3-fixed-widget' ), 'activate_plugins', self::ID, array( __CLASS__, 'settings_page' ) ); } /** * Return default settings values * * @return array */ protected static function defaults() { $d['use_sticky_position'] = false; $d['margin-top'] = 0; $d['margin-bottom'] = 0; $d['stop_elements_selectors'] = ''; $d['screen-max-width'] = 0; $d['screen-max-height'] = 0; $d['fix-widget-id'] = 'yes'; $d['logged_in_req'] = false; return $d; } /** * Load Fixed Widget settings * * @return array */ protected static function load_options() { $options = get_option( self::ID ); $options_old = get_option( 'q2w3_fixed_widget' ); return array_merge( self::defaults(), (array) $options_old, (array) $options ); } /** * Register Fixed Widget settings */ public static function register_settings() { register_setting( self::ID, self::ID, array( __CLASS__, 'save_options_filter' ) ); } /** * Callback to sanitize options * * @param array $input options. * * @return mixed */ public static function save_options_filter( $input ) { // Sanitize user input $input['margin-top'] = (int) $input['margin-top']; $input['margin-bottom'] = (int) $input['margin-bottom']; $input['screen-max-width'] = (int) $input['screen-max-width']; $input['screen-max-height'] = (int) $input['screen-max-height']; $input['custom-ids'] = trim( wp_strip_all_tags( $input['custom-ids'] ) ); $input['stop_elements_selectors'] = trim( wp_strip_all_tags( $input['stop_elements_selectors'] ) ); if ( ! isset( $input['fix-widget-id'] ) ) { $input['fix-widget-id'] = false; } if ( ! isset( $input['logged_in_req'] ) ) { $input['logged_in_req'] = false; } if ( ! isset( $input['use_sticky_position'] ) ) { $input['use_sticky_position'] = false; } return $input; } /** * Load JavaScript on the settings page * * @param string $hook settings page hook. */ public static function settings_page_js( $hook ) { if ( self::$settings_page_hook !== $hook ) { return; } wp_enqueue_script( 'postbox' ); } /** * Render content of the settings page */ public static function settings_page() { $screen = get_current_screen(); add_meta_box( self::ID . '-general', esc_html__( 'General Options', 'q2w3-fixed-widget' ), array( __CLASS__, 'settings_page_general_box' ), $screen, 'normal' ); add_meta_box( self::ID . '-stop_element', esc_html__( 'Stop Elements', 'q2w3-fixed-widget' ), array( __CLASS__, 'settings_page_stop_element' ), $screen, 'normal' ); add_meta_box( self::ID . '-custom-ids', esc_html__( 'Custom Fixed Elements', 'q2w3-fixed-widget' ), array( __CLASS__, 'settings_page_custom_ids_box' ), $screen, 'normal' ); add_meta_box( self::ID . '-recommend', esc_html__( 'Recommended Integration', 'q2w3-fixed-widget' ), array( __CLASS__, 'settings_page_recommend_box' ), $screen, 'side', 'high' ); add_meta_box( self::ID . '-help', esc_html__( 'Documentation and Support', 'q2w3-fixed-widget' ), array( __CLASS__, 'settings_page_help_box' ), $screen, 'side' ); $options = self::load_options(); echo '

' . esc_html__( 'Fixed Widget', 'q2w3-fixed-widget' ) . '

' . PHP_EOL; if ( isset( $_GET['settings-updated'] ) && $_GET['settings-updated'] == 'true' ) { echo '

' . esc_html__( 'Settings saved.', 'q2w3-fixed-widget' ) . '

' . PHP_EOL; } echo '
' . PHP_EOL; settings_fields( self::ID ); wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false ); wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false ); echo '
' . PHP_EOL; echo '
' . PHP_EOL; do_meta_boxes( $screen, 'side', $options ); echo '
' . PHP_EOL; echo '
' . PHP_EOL; do_meta_boxes( $screen, 'normal', $options ); echo '
' . PHP_EOL; echo '

' . esc_html__( ' Note for users of caching plugins. Please, don’t forget to clear the cache after changing options.', 'q2w3-fixed-widget' ) . '

' . PHP_EOL; echo '

' . PHP_EOL; echo '
' . PHP_EOL; echo '
' . PHP_EOL; echo '' . PHP_EOL; echo '
' . PHP_EOL; } /** * Render General settings * * @param array $options plugin settings. */ public static function settings_page_general_box( $options ) { echo '

' . esc_html__( 'Improved version', 'q2w3-fixed-widget' ) . '

' . PHP_EOL; echo '

'; esc_html_e( 'Enable this option to use the completely updated script. It takes care of common issues and layout shifts.', 'q2w3-fixed-widget' ); echo ' '; esc_html_e( 'The improved script works best with themes that support flexbox instead of float.', 'q2w3-fixed-widget' ); echo ' '; printf( wp_kses( // translators: %1$s is an opening a tag, %2ds is a closing one __( 'If you discover any issues then please report them %1$shere%2$s', 'q2w3-fixed-widget' ), array( 'a' => array( 'rel' => array(), 'target' => array(), ), ) ), '', '' ); echo '

'; echo '

' . esc_html__( 'Minimum Screen Width', 'q2w3-fixed-widget' ) . ' ' . esc_html__( 'px', 'q2w3-fixed-widget' ) . ' / ' . esc_html__( 'Disable the plugin on small devices. When the browser screen width is less than the specified value, Fixed Widget will not make any elements sticky.', 'q2w3-fixed-widget' ) . '

' . PHP_EOL; echo '

' . esc_html__( 'Minimum Screen Height', 'q2w3-fixed-widget' ) . ' ' . esc_html__( 'px', 'q2w3-fixed-widget' ) . ' / ' . esc_html__( ' Works like the Minimum Width option.', 'q2w3-fixed-widget' ) . '

' . PHP_EOL; } /** * Render Custom ID setting * * @param array $options plugin settings. */ public static function settings_page_custom_ids_box( $options ) { $custom_ids = isset( $options['custom-ids'] ) ? $options['custom-ids'] : ''; echo '

' . esc_html__( 'Add HTML element selectors that should be fixed.', 'q2w3-fixed-widget' ) . ' ' . esc_html__( 'Accepts IDs, Class, and Type selectors.', 'q2w3-fixed-widget' ) . ' ' . esc_html__( 'One entry per line.', 'q2w3-fixed-widget' ) . '

' . PHP_EOL; } /** * Render Stop Element settings * * @param array $options plugin settings. */ public static function settings_page_stop_element( $options ) { $stop_selectors = isset($options['stop-id']) && $options['stop-id'] != '' && (!isset($options['stop_elements_selectors']) || $options['stop_elements_selectors'] == '') ? '#' . $options['stop-id'] : $options['stop_elements_selectors']; echo '

' . esc_html__( 'Margin Top', 'q2w3-fixed-widget' ) . ' ' . esc_html__( 'px', 'q2w3-fixed-widget' ) . ' / ' . esc_html__( 'The distance fixed elements will keep from the top of the window.', 'q2w3-fixed-widget' ) . '

' . PHP_EOL; echo '

' . esc_html__( 'Margin Bottom', 'q2w3-fixed-widget' ) . ' ' . esc_html__( 'px', 'q2w3-fixed-widget' ) . ' / ' . esc_html__( 'The distance fixed elements will keep from the bottom of the window.', 'q2w3-fixed-widget' ) . '

' . PHP_EOL; echo '

' . esc_html__( 'Stop Elements', 'q2w3-fixed-widget' ) . '

' . esc_html__( 'The stop elements will push sticky elements up as soon as they reach them while scrolling.', 'q2w3-fixed-widget' ) . '
' . esc_html__( 'Accepts IDs, Class, and Type selectors.', 'q2w3-fixed-widget' ) . ' ' . esc_html__( 'One entry per line.', 'q2w3-fixed-widget' ) . '
' . PHP_EOL; } /** * Render Recommendation box. */ public static function settings_page_recommend_box() { echo '

'; echo 'Advanced Ads '; echo esc_html__( 'provides many features to manage and optimize your ads and to boost your conversions. It works perfectly with the Fixed Widget plugin.', 'q2w3-fixed-widget' ); echo '

'; echo '
'; echo '
"Perfect plugin"
'; echo '

"The plugin contains everything I need for the ads management and publishing. Fair price, stable and functional."

'; echo '

from David H. on wordpress.org

'; echo '
'; echo '' . PHP_EOL; if ( ! defined( 'ADVADS_VERSION' ) ) { // check whether is's installed $plugins = get_plugins(); if ( isset( $plugins['advanced-ads/advanced-ads.php'] ) ) { // advanced-ads is deactivated $link = '' . esc_html__( 'Activate Now', 'q2w3-fixed-widget' ) . ''; } else { // advanced-ads is not installed $link = '' . esc_html__( 'Install Now', 'q2w3-fixed-widget' ) . ''; } echo '
' . $link . '
'; } } /** * Render Documentation box. */ public static function settings_page_help_box() { echo ''; echo '' . PHP_EOL; } /** * Add relevant links to the plugin page * * @param array $links existing links. * * @return array */ public static function add_plugin_links( $links ) { if ( ! is_array( $links ) ) { return $links; } // add link to the settings $extend_link = '' . esc_html__( 'Settings', 'q2w3-fixed-widget' ) . ''; array_unshift( $links, $extend_link ); return $links; } /** * Prepare sidebar output in the frontend */ public static function registered_sidebars_filter() { global $wp_registered_sidebars; if ( ! is_array( $wp_registered_sidebars ) ) { return; } foreach ( $wp_registered_sidebars as $id => $sidebar ) { if ( strpos( $sidebar['before_widget'], 'id="%1$s"' ) === false && strpos( $sidebar['before_widget'], 'id=\'%1$s\'' ) === false ) { if ( $sidebar['before_widget'] == '' || $sidebar['before_widget'] == ' ' ) { $wp_registered_sidebars[ $id ]['before_widget'] = '
'; $wp_registered_sidebars[ $id ]['after_widget'] = '
'; } elseif ( strpos( $sidebar['before_widget'], 'id=' ) === false ) { $tag_end_pos = strpos( $sidebar['before_widget'], '>' ); if ( $tag_end_pos !== false ) { $wp_registered_sidebars[ $id ]['before_widget'] = substr_replace( $sidebar['before_widget'], ' id="%1$s"', $tag_end_pos, 0 ); } } else { $str_array = explode( ' ', $sidebar['before_widget'] ); if ( is_array( $str_array ) ) { foreach ( $str_array as $str_part_id => $str_part ) { if ( strpos( $str_part, 'id="' ) !== false ) { $p1 = strpos( $str_part, 'id="' ); $p2 = strpos( $str_part, '"', $p1 + 4 ); $str_array[ $str_part_id ] = substr_replace( $str_part, 'id="%1$s"', $p1, $p2 + 1 ); } elseif ( strpos( $str_part, 'id=\'' ) !== false ) { $p1 = strpos( $str_part, 'id=\'' ); $p2 = strpos( $str_part, "'", $p1 + 4 ); $str_array[ $str_part_id ] = substr_replace( $str_part, 'id=\'%1$s\'', $p1, $p2 ); } } $wp_registered_sidebars[ $id ]['before_widget'] = implode( ' ', $str_array ); } } } // if id is wrong } // foreach } // registered_sidebars_filter() /** * Check if the current page is on AMP * needs to run using the `wp` hook or later in order to get access to WP_Query * * @return bool */ private static function is_amp() { // bail if the site uses the AMP technology in version 2.0 and higher of https://wordpress.org/plugins/amp/ // or https://wordpress.org/plugins/ads-for-wp/ return ( function_exists( 'amp_is_request' ) && amp_is_request() ) || ( function_exists( 'ampforwp_is_amp_endpoint' ) && ampforwp_is_amp_endpoint() ); } } // q2w3_fixed_widget_pro class