existing_options = $options; else $this->existing_options = array(); } /** * Reference the social plugin by name. * * @since 1.1 * * @return string social plugin name */ public static function social_plugin_name() { return __( 'Recommendations Bar', 'facebook' ); } /** * Evaluate the Facebook_Recommendations_Bar class file if it is not already loaded. * * @since 1.1 * * @return void */ public static function require_recommendations_bar_builder() { if ( ! class_exists( 'Facebook_Recommendations_Bar' ) ) require_once( dirname( dirname(__FILE__) ) . '/social-plugins/class-facebook-recommendations-bar.php' ); } /** * Navigate to the settings page through the Facebook top-level menu item * * @since 1.1 * * @uses add_submenu_page() * @param string $parent_slug Facebook top-level menu item slug * @return string submenu hook suffix */ public static function add_submenu_item( $parent_slug ) { $recommendations_bar_settings = new Facebook_Recommendations_Bar_Settings(); $hook_suffix = add_submenu_page( $parent_slug, self::social_plugin_name(), self::social_plugin_name(), 'manage_options', self::PAGE_SLUG, array( &$recommendations_bar_settings, 'settings_page' ) ); if ( $hook_suffix ) { $recommendations_bar_settings->hook_suffix = $hook_suffix; register_setting( $hook_suffix, self::OPTION_NAME, array( 'Facebook_Recommendations_Bar_Settings', 'sanitize_options' ) ); add_action( 'load-' . $hook_suffix, array( &$recommendations_bar_settings, 'onload' ) ); } return $hook_suffix; } /** * Load stored options and scripts on settings page view * * @since 1.1 * * @return void */ public function onload() { $options = get_option( self::OPTION_NAME ); if ( ! is_array( $options ) ) $options = array(); $this->existing_options = $options; $this->settings_api_init(); } /** * Load the page * * @since 1.1 * * @return void */ public function settings_page() { if ( ! isset( $this->hook_suffix ) ) return; Facebook_Settings::settings_page_template( $this->hook_suffix, sprintf( __( '%s Settings', 'facebook' ), self::social_plugin_name() ) ); } /** * Hook into the settings API * * @since 1.1 * * @uses add_settings_section() * @uses add_settings_field() * @param string $options_group target grouping * @return void */ private function settings_api_init() { if ( ! isset( $this->hook_suffix ) ) return; $section = 'facebook-recommendations-bar'; add_settings_section( $section, '', // no title for main section array( &$this, 'section_header' ), $this->hook_suffix ); // when, where add_settings_field( 'facebook-recommendations-bar-show-on', _x( 'Show on', 'Display the social plugin in specific areas of a website', 'facebook' ), array( &$this, 'display_show_on' ), $this->hook_suffix, $section ); add_settings_field( 'facebook-recommendations-bar-side', _x( 'Side', 'Form label asking the viewer to choose the left or right side of a webpage for widget display.', 'facebook' ), array( &$this, 'display_side' ), $this->hook_suffix, $section ); // social plugin fields add_settings_field( 'facebook-recommendations-bar-action', __( 'Action', 'facebook' ), array( &$this, 'display_action' ), $this->hook_suffix, $section ); add_settings_field( 'facebook-recommendations-bar-trigger', _x( 'Trigger', 'An event triggering another event on a webpage. Examples: scrolled to the end of the page; 30 seconds elapsed.', 'facebook' ), array( &$this, 'display_trigger' ), $this->hook_suffix, $section ); add_settings_field( 'facebook-recommendations-bar-read-time', _x( 'Read time', 'A number of elapsed seconds', 'facebook' ), array( &$this, 'display_read_time' ), $this->hook_suffix, $section, array( 'label_for' => 'facebook-recommendations-bar-read-time' ) ); add_settings_field( 'facebook-recommendations-bar-num-recommendations', __( 'Number of recommendations', 'facebook' ), array( &$this, 'display_num_recommendations' ), $this->hook_suffix, $section, array( 'label_for' => 'facebook-recommendations-bar-num-recommendations' ) ); add_settings_field( 'facebook-recommendations-bar-max-age', __( 'Maximum age', 'facebook' ), array( &$this, 'display_max_age' ), $this->hook_suffix, $section, array( 'label_for' => 'facebook-recommendations-bar-max-age' ) ); } /** * Introduce publishers to the Recommendations Bar social plugin * * @since 1.1 * * @return void */ public function section_header() { echo '

' . esc_html( __( 'Encourage additional pageviews with site recommendations based on social context.', 'facebook' ) ) . ' ' . esc_html( __( 'Adds a Like Button overlay to the bottom of your page with an expanded list of recommendations triggered by time or position on the page.', 'facebook' ) ) . '
' . esc_html( __( 'Read more...', 'facebook' ) ) . '

'; } /** * Where should the button appear? * * @since 1.1 * * @return void */ public function display_show_on() { echo '
' . self::show_on_choices( self::OPTION_NAME . '[show_on]', self::get_display_conditionals_by_feature( 'recommendations_bar', 'all' ) ) . '
'; echo '

' . esc_html( self::show_on_description( self::social_plugin_name() ) ) . '

'; } /** * Choose to display the recommendations bar on the left or right side * * @since 1.1 * * @return void */ public function display_side() { $key = 'side'; self::require_recommendations_bar_builder(); if ( isset( $this->existing_options[$key] ) && isset( Facebook_Recommendations_Bar::$side_choices[ $this->existing_options[$key] ] ) ) $existing_value = $this->existing_options[$key]; else $existing_value = 'right'; $side_choices = array_keys( Facebook_Recommendations_Bar::$side_choices ); $choices = array(); foreach ( $side_choices as $side ) { $choices[] = ''; } echo '
' . implode( ' ', $choices ) . '
'; echo '

' . esc_html( __( 'The side of the screen where the Recommendations Bar will be displayed.', 'facebook' ) ) . '

'; } /** * Choose action text. * * @since 1.1 * * @return void */ public function display_action() { $key = 'action'; $name = self::OPTION_NAME . '[' . $key . ']'; self::require_recommendations_bar_builder(); if ( isset( $this->existing_options[$key] ) && isset( Facebook_Recommendations_Bar::$action_choices[ $this->existing_options[$key] ] ) ) $existing_value = $this->existing_options[$key]; else $existing_value = 'like'; $action_choices = array_keys( Facebook_Recommendations_Bar::$action_choices ); $fields = array(); foreach( $action_choices as $action ) { $fields[] = ''; } echo '
' . implode( ' ', $fields ) . '
'; echo '

' . esc_html( __( 'Action verb displayed in the Like Button.', 'facebook' ) ) . '

'; } /** * What page progression should trigger the recommendations bar? * * @since 1.1 * * @return void */ public function display_trigger() { $key = 'trigger'; $name = self::OPTION_NAME . '[' . $key . ']'; if ( isset( $this->existing_options[ $key ] ) ) $existing_value = $this->existing_options[ $key ]; else $existing_value = 'onvisible'; if ( $existing_value && substr( $existing_value, -1 ) === '%' ) { $pct = absint( substr( $existing_value, 0, strlen( $existing_value ) - 1 ) ); if ( $pct > 0 && $pct < 101 ) // positive integer less than or equal to 100 $existing_value = $pct; unset( $pct ); } echo '
'; echo '
'; // select the X% option then provide a % number echo '
'; echo '
'; // advanced users: set manually echo '
'; echo '
'; } /** * Trigger the recommendations bar after a given number of seconds. * * @since 1.1 * * @return void */ public function display_read_time() { $key = 'read_time'; if ( isset( $this->existing_options[$key] ) ) $existing_value = absint( $this->existing_options[$key] ); // enforce minimum. reset to default if ( ! isset( $existing_value ) || $existing_value < 10 ) $existing_value = 30; echo ' ' . esc_html( _x( 'seconds', 'measurement of time', 'facebook' ) ) . ''; echo '

' . esc_html( __( 'Number of seconds before the plugin will expand', 'facebook' ) ) . '

'; } /** * Maximum number of recommendations displayed. * * @since 1.1 * * @return void */ public function display_num_recommendations() { $key = 'num_recommendations'; if ( isset( $this->existing_options[$key] ) ) $existing_value = absint( $this->existing_options[$key] ); // enforce minimum. reset to default if ( ! isset( $existing_value ) || $existing_value < 1 || $existing_value > 5 ) $existing_value = 2; echo ' ' . _n( 'maximum recommendation', 'maximum recommendations', $existing_value, 'facebook' ); } /** * Maximum age of a recommended article. * * @since 1.1 * * @return void */ public function display_max_age() { $key = 'max_age'; if ( isset( $this->existing_options[$key] ) ) $existing_value = absint( $this->existing_options[$key] ); // enforce minimum. reset to default if ( ! isset( $existing_value ) || $existing_value > 180 ) $existing_value = 0; echo ' ' . esc_html( _n( 'day old', 'days old', $existing_value, 'facebook' ) ); // days === 0 can be confusing. clarify if ( $existing_value === 0 ) echo ' ' . esc_html( __( '(no limit)', 'facebook' ) ); echo '

' . esc_html( __( 'Limit recommendations to articles authored within the last N days.', 'facebook' ) ) . ' ' . esc_html( sprintf( __( 'Reset this value to %s for no date-based limits.', 'facebook' ), '"0"' ) ) . '

'; } /** * Translate HTML data response returned from Facebook social plugin builder into underscored keys and PHP values before saving. * * @since 1.1 * * @param array $options data-* options returned from Facebook social plugin builder * @return array $options options to store in WordPress */ public static function html_data_to_options( $options ) { if ( ! is_array( $options ) ) return array(); foreach( array( 'max-age' => 'max_age', 'num-recommendations' => 'num_recommendations', 'read-time' => 'read_time' ) as $data => $option ) { if ( isset( $options[ $data ] ) ) { $options[ $option ] = absint( $options[ $data ] ); unset( $options[ $data ] ); } } return $options; } /** * Sanitize Recommendations Bar settings before they are saved to the database. * * @since 1.1 * * @param array $options recommendation bar options * @return array clean option sets. note: we remove Recommendation Button social plugin default options, storing only custom settings (e.g. recommend action preference value stored, like is not stored) */ public static function sanitize_options( $options ) { if ( ! is_array( $options ) || empty( $options ) ) return array(); if ( isset( $options['trigger'] ) && $options['trigger'] === 'pct' ) { $pct = 0; if ( isset( $options['trigger_pct'] ) ) { $pct = absint( $options['trigger_pct'] ); unset( $options['trigger_pct'] ); } if ( $pct > 0 ) $options['trigger'] = $pct . '%'; else $options['trigger'] = 'onvisible'; } foreach( array( 'read_time', 'num_recommendations', 'max_age' ) as $option ) { if ( isset( $options[ $option ] ) ) $options[ $option ] = absint( $options[ $option ] ); } self::require_recommendations_bar_builder(); // Handle like button display preferences first $clean_options = parent::sanitize_options( $options ); if ( isset( $clean_options['show_on'] ) ) { self::update_display_conditionals( 'recommendations_bar', $clean_options['show_on'], self::get_show_on_choices() ); unset( $clean_options['show_on'] ); } unset( $options['show_on'] ); $bar = Facebook_Recommendations_Bar::fromArray( $options ); if ( $bar ) return array_merge( $clean_options, self::html_data_to_options( $bar->toHTMLDataArray() ) ); return $clean_options; } } ?>