Browse files

first ...

  • Loading branch information...
0 parents commit 16194a99ac1ea992cbb7604353e4b2c4bbb9acb6 @mfields committed Jun 2, 2011
Showing with 633 additions and 0 deletions.
  1. +573 −0 mfields-plus-one.php
  2. BIN preview-sprite.png
  3. +43 −0 style-settings-page.css
  4. +17 −0 style-settings-page.js
573 mfields-plus-one.php
@@ -0,0 +1,573 @@
+<?php
+/*
+Plugin Name: Mfields Plus One
+Plugin URI: null
+Description: null
+Version: 0
+Author: Michael Fields
+Author URI: http://wordpress.mfields.org/
+License: GPLv2
+*/
+
+Mfields_Plus_One::init();
+
+class Mfields_Plus_One {
+ static $domain = 'mfields_plus_one';
+ static $url = '';
+ static $version = '0.1';
+ static $settings_page = '';
+
+ static function init() {
+ add_action( 'init', array( __class__, 'register_enqueueables' ) );
+ add_action( 'admin_menu', array( __class__, 'settings_menu' ), 10 );
+ add_action( 'admin_menu', array( __class__, 'settings_enqueue' ), 11 );
+ add_action( 'admin_init', array( __class__, 'settings_register' ) );
+ add_action( 'template_redirect', array( __class__, 'integrate_singular' ) );
+ add_action( 'template_redirect', array( __class__, 'integrate_multiple' ) );
+ add_action( 'wp_print_scripts', array( __class__, 'script_public' ) );
+ }
+
+ static function register_enqueueables() {
+ wp_register_script( 'mfields-plus-one', 'https://apis.google.com/js/plusone.js', array(), self::$version, true );
+ wp_register_script( 'mfields-plus-one-settings', plugin_dir_url( __FILE__ ) . 'style-settings-page.js', array( 'jquery' ), self::$version, true );
+ wp_register_style( 'mfields-plus-one-settings', plugin_dir_url( __FILE__ ) . 'style-settings-page.css', array(), self::$version, 'screen' );
+ }
+
+ static function settings_enqueue() {
+ add_action( 'admin_print_styles-' . self::$settings_page, array( __class__, 'style_settings_page' ) );
+ add_action( 'admin_print_scripts-' . self::$settings_page, array( __class__, 'script_settings_page' ) );
+ }
+
+ static function script_public() {
+ if ( is_admin() ) {
+ return;
+ }
+ wp_enqueue_script( 'mfields-plus-one' );
+ }
+
+ static function style_settings_page() {
+ wp_enqueue_style( 'mfields-plus-one-settings' );
+ }
+
+ static function script_settings_page() {
+ wp_enqueue_script( 'mfields-plus-one-settings' );
+ }
+
+ static function integrate_singular() {
+ $settings = self::get_settings();
+
+ if ( empty( $settings['singular'] ) ) {
+ return;
+ }
+
+ if ( empty( $settings['post_types'] ) ) {
+ return;
+ }
+
+ if ( ! is_singular( $settings['post_types'] ) ) {
+ return;
+ }
+
+ foreach ( $settings['singular'] as $location ) {
+ switch ( $location ) {
+ case 'before_content' :
+ add_filter( 'the_content', array( __class__, 'prepend' ) );
+ break;
+ case 'after_content' :
+ add_filter( 'the_content', array( __class__, 'append' ) );
+ break;
+ default :
+ continue;
+ break;
+ }
+ }
+ }
+
+ static function integrate_multiple() {
+ $settings = self::get_settings();
+
+ if ( empty( $settings['multiple'] ) ) {
+ return;
+ }
+
+ foreach ( $settings['multiple'] as $location ) {
+ switch ( $location ) {
+ case 'before_content' :
+ add_filter( 'the_content', array( __class__, 'prepend_archive' ) );
+ break;
+ case 'after_content' :
+ add_filter( 'the_content', array( __class__, 'append_archive' ) );
+ break;
+ case 'before_excerpt' :
+ add_filter( 'the_excerpt', array( __class__, 'prepend_archive' ) );
+ break;
+ case 'after_excerpt' :
+ add_filter( 'the_excerpt', array( __class__, 'append_archive' ) );
+ break;
+ default :
+ continue;
+ break;
+ }
+ }
+ }
+
+ static function button() {
+ $settings = self::get_settings();
+ $markup = $settings['markup'];
+
+ $atts = array(
+ 'xhtml' => array(
+ 'data-href' => esc_url( get_permalink() ),
+ ),
+ 'html' => array(
+ 'class' => 'g-plusone',
+ 'data-href' => esc_url( get_permalink() ),
+ ),
+
+ );
+
+ if ( isset( $settings['size'] ) ) {
+ $atts['xhtml']['size'] = $settings['size'];
+ $atts['html']['data-size'] = $settings['size'];
+ }
+
+ if ( isset( $settings['show_count'] ) ) {
+ $atts['xhtml']['count'] = $settings['show_count'];
+ $atts['html']['count'] = $settings['show_count'];
+ /*
+ This should be the correct attribute but does not seem to be working
+ at the moment. Revist in the future.
+ $atts['html']['data-count'] = $settings['show_count'];
+ */
+ }
+
+ if ( in_array( $markup, array_keys( $atts ) ) ) {
+ $attributes = '';
+ foreach ( $atts[$markup] as $name => $value ) {
+ $attributes .= ' ' . $name . '="' . esc_attr( $value ). '"';
+ }
+ if ( 'html' == $markup ) {
+ return sprintf( '<div%s></div>', $attributes );
+ }
+ return sprintf( '<g:plusone%s></g:plusone>', $attributes );
+ }
+ }
+
+ static function button_print() {
+ print self::button();
+ }
+
+ static function prepend( $content ) {
+ if ( ! in_the_loop() ) {
+ return $content;
+ }
+ return self::button() . $content;
+ }
+
+ static function prepend_archive( $content ) {
+ $settings = self::get_settings();
+ if ( empty( $settings['post_types'] ) ) {
+ return $content;
+ }
+ if ( ! in_array( get_post_type(), $settings['post_types'] ) ) {
+ return $content;
+ }
+ return self::prepend( $content );
+ }
+
+ static function append( $content ) {
+ if ( ! in_the_loop() ) {
+ return $content;
+ }
+ return $content . self::button();
+ }
+
+ static function append_archive( $content ) {
+ $settings = self::get_settings();
+ if ( empty( $settings['post_types'] ) ) {
+ return $content;
+ }
+ if ( ! in_array( get_post_type(), $settings['post_types'] ) ) {
+ return $content;
+ }
+ return self::append( $content );
+ }
+
+ static function settings_menu() {
+ self::$settings_page = add_options_page(
+ __( 'Plus One', self::$domain ),
+ __( 'Plus One', self::$domain ),
+ 'manage_options',
+ 'mfields_plus_one',
+ array( __class__, 'settings_page' )
+ );
+ }
+
+ static function settings_register() {
+ register_setting(
+ 'mfields_plus_one',
+ 'mfields_plus_one',
+ array( __class__, 'settings_sanitize' )
+ );
+ add_settings_section(
+ 'mfields_plus_one_configuration',
+ __( 'Configuration', self::$domain ),
+ array( __class__, 'message_configuration' ),
+ 'mfields_plus_one'
+ );
+ add_settings_field(
+ 'mfields_plus_one_language',
+ __( 'Select Language', self::$domain ),
+ array( __class__, 'control_language' ),
+ 'mfields_plus_one',
+ 'mfields_plus_one_configuration'
+ );
+ add_settings_field(
+ 'mfields_plus_one_markup',
+ __( 'Markup Type', self::$domain ),
+ array( __class__, 'control_markup' ),
+ 'mfields_plus_one',
+ 'mfields_plus_one_configuration'
+ );
+ add_settings_field(
+ 'mfields_plus_one_count',
+ __( 'Display Count', self::$domain ),
+ array( __class__, 'control_count' ),
+ 'mfields_plus_one',
+ 'mfields_plus_one_configuration'
+ );
+ add_settings_field(
+ 'mfields_plus_one_size',
+ __( 'Button Size', self::$domain ),
+ array( __class__, 'control_size' ),
+ 'mfields_plus_one',
+ 'mfields_plus_one_configuration'
+ );
+ add_settings_section(
+ 'mfields_plus_one_theme_integration',
+ __( 'Theme Integration', self::$domain ),
+ array( __class__, 'message_theme_integration' ),
+ 'mfields_plus_one'
+ );
+ add_settings_field(
+ 'mfields_plus_one_singular',
+ __( 'Singular Templates', self::$domain ),
+ array( __class__, 'control_singular' ),
+ 'mfields_plus_one',
+ 'mfields_plus_one_theme_integration'
+ );
+ add_settings_field(
+ 'mfields_plus_one_multiple',
+ __( 'Archive Templates', self::$domain ),
+ array( __class__, 'control_multiple' ),
+ 'mfields_plus_one',
+ 'mfields_plus_one_theme_integration'
+ );
+ add_settings_field(
+ 'mfields_plus_one_post_types',
+ __( 'Post Types', self::$domain ),
+ array( __class__, 'control_post_types' ),
+ 'mfields_plus_one',
+ 'mfields_plus_one_theme_integration'
+ );
+ }
+ static function message_configuration() {
+ print sprintf( esc_html__( 'All settings in this section apply to all buttons.', self::$domain ), '<code>plus-one-button</code>' );
+ }
+ static function message_theme_integration() {
+ print sprintf( esc_html__( 'The following settings enable you to automatically add +1 buttons at different places in your theme. To disable automatic theme intgration, just leave these settings unchecked. You will need to use the %1$s action to display the button in your theme.', self::$domain ), '<code>plus-one-button</code>' );
+ }
+ static function settings_page() {
+ print "\n" . '<div class="wrap" id="' . esc_attr( self::$settings_page ) . '">';
+ screen_icon();
+ print "\n" . '<h2>' . __( 'Plus One Settings', self::$domain ) . '</h2>';
+ print "\n" . '<form action="options.php" method="post">';
+
+ settings_fields( 'mfields_plus_one' );
+ do_settings_sections( 'mfields_plus_one' );
+
+ print "\n" . '<div class="button-holder"><input name="submit" type="submit" value="' . esc_attr__( 'Save Changes', self::$domain ) . '" /></div>';
+ print "\n" . '</div></form>';
+ }
+
+ static function control_language() {
+ $settings = get_option( 'mfields_plus_one' );
+ $languages = self::get_languages();
+ print "\n" . '<select name="mfields_plus_one[language]">';
+ foreach( $languages as $key => $value ) {
+ print "\n" . '<option' . selected( $settings['language'], $key ) . ' value="' . esc_attr( $key ) . '">' . esc_html( $value ) . '</option>';
+ }
+ print "\n" . '</select>';
+ }
+
+ static function control_markup() {
+ $setting = self::get_settings();
+
+ $id = 'mfields_plus_one_markup_html';
+ print "\n" . '<label for="' . esc_attr( $id ) . '"><input' . checked( $setting['markup'], 'html', false ) . ' id="' . esc_attr( $id ) . '" type="radio" name="mfields_plus_one[markup]" value="html" /> ' . __( 'html', self::$domain ) . '</label>';
+
+ $id = 'mfields_plus_one_markup_xhtml';
+ print "\n" . '<label for="' . esc_attr( $id ) . '"><input' . checked( $setting['markup'], 'xhtml', false ) . ' id="' . esc_attr( $id ) . '" type="radio" name="mfields_plus_one[markup]" value="xhtml" /> ' . __( 'xhtml', self::$domain ) . '</label>';
+ }
+
+ static function control_count() {
+ $setting = self::get_settings();
+
+ $id = 'mfields_plus_one_count_true';
+ print "\n" . '<label for="' . esc_attr( $id ) . '"><input' . checked( $setting['show_count'], 'true', false ) . ' id="' . esc_attr( $id ) . '" type="radio" class="mfields_plus_one_count" name="mfields_plus_one[show_count]" value="true" /> ' . __( 'Yes', self::$domain ) . '</label>';
+
+ $id = 'mfields_plus_one_count_false';
+ print "\n" . '<label for="' . esc_attr( $id ) . '"><input' . checked( $setting['show_count'], 'false', false ) . ' id="' . esc_attr( $id ) . '" type="radio" class="mfields_plus_one_count" name="mfields_plus_one[show_count]" value="false" /> ' . __( 'No', self::$domain ) . '</label>';
+ }
+
+ static function control_size() {
+ $setting = self::get_settings();
+ $value = $setting['size'];
+ print '<div id="mfields_plus_one_size_wrap">';
+ foreach ( self::get_sizes() as $size => $label ) {
+ $id = 'mfields_plus_one_size_' . $size;
+ print "\n" . '<label for="' . esc_attr( $id ) . '"><input' . checked( $size, $value, false ) . ' id="' . esc_attr( $id ) . '" type="radio" class="mfields_plus_one_size" name="mfields_plus_one[size]" value="' . esc_attr( $size ) . '" /> ' . esc_html( $label ) . '</label>';
+ }
+ $count = ( 'true' == $setting['show_count'] ) ? ' count' : '';
+ print '<div id="' . esc_attr( self::$domain . '_preview' ) . '"><div class="' . esc_attr( $setting['size'] . $count ) . '"></div></div>';
+ print '</div>';
+ }
+
+ static function control_singular() {
+ $setting = self::get_settings();
+ $value = $setting['singular'];
+ foreach ( self::get_locations_singular() as $location => $label ) {
+ $id = 'mfields_plus_one_singular_' . $location;
+ $checked = ( in_array( $location, $value ) ) ? ' checked="checked"' : '';
+ print "\n" . '<label for="' . esc_attr( $id ) . '"><input' . $checked . ' id="' . esc_attr( $id ) . '" type="checkbox" name="mfields_plus_one[singular][]" value="' . esc_attr( $location ) . '" /> ' . esc_html( $label ) . '</label>';
+ }
+ }
+
+ static function control_multiple() {
+ $setting = self::get_settings();
+ $value = $setting['multiple'];
+ foreach ( self::get_locations_multiple() as $location => $label ) {
+ $id = 'mfields_plus_one_multiple_' . $location;
+ $checked = ( in_array( $location, $value ) ) ? ' checked="checked"' : '';
+ print "\n" . '<label for="' . esc_attr( $id ) . '"><input' . $checked . ' id="' . esc_attr( $id ) . '" type="checkbox" name="mfields_plus_one[multiple][]" value="' . esc_attr( $location ) . '" /> ' . esc_html( $label ) . '</label>';
+ }
+ }
+
+ static function control_post_types() {
+ $setting = self::get_settings();
+ $value = $setting['post_types'];
+ foreach ( self::get_post_types() as $post_type => $label ) {
+ $id = 'mfields_plus_one_post_types_' . $post_type;
+ $checked = ( in_array( $post_type, $value ) ) ? ' checked="checked"' : '';
+ print "\n" . '<label for="' . esc_attr( $id ) . '"><input' . $checked . ' id="' . esc_attr( $id ) . '" type="checkbox" name="mfields_plus_one[post_types][]" value="' . esc_attr( $post_type ) . '" /> ' . esc_html( $label ) . '</label>';
+ }
+ }
+
+ static function get_settings() {
+ return wp_parse_args( (array) get_option( 'mfields_plus_one' ), self::get_defaults() );
+ }
+
+ static function settings_sanitize( $dirty ) {
+ $clean = array(
+ 'language' => '',
+ 'show_count' => 'true',
+ 'size' => '',
+ 'singular' => array(),
+ 'markup' => 'html',
+ 'multiple' => array(),
+ 'post_types' => array(),
+ );
+
+ if ( isset( $dirty['show_count'] ) && 'false' == $dirty['show_count'] ) {
+ $clean['show_count'] = 'false';
+ }
+
+ if ( isset( $dirty['markup'] ) && 'xhtml' == $dirty['markup'] ) {
+ $clean['markup'] = 'xhtml';
+ }
+
+ if ( isset( $dirty['language'] ) && array_key_exists( $dirty['language'], self::get_languages() ) ) {
+ $clean['language'] = $dirty['language'];
+ }
+
+ if ( isset( $dirty['size'] ) && array_key_exists( $dirty['size'], self::get_sizes() ) ) {
+ $clean['size'] = $dirty['size'];
+ }
+
+ if ( isset( $dirty['singular'] ) ) {
+ $locations = self::get_locations_singular();
+ foreach ( (array) $dirty['singular'] as $location ) {
+ if ( array_key_exists( $location, $locations ) ) {
+ $clean['singular'][] = $location;
+ }
+ }
+ }
+
+ if ( isset( $dirty['multiple'] ) ) {
+ $locations = self::get_locations_multiple();
+ foreach ( (array) $dirty['multiple'] as $location ) {
+ if ( array_key_exists( $location, $locations ) ) {
+ $clean['multiple'][] = $location;
+ }
+ }
+ }
+
+ if ( isset( $dirty['post_types'] ) ) {
+ $post_types = self::get_post_types();
+ foreach ( (array) $dirty['post_types'] as $post_type ) {
+ if ( array_key_exists( $post_type, $post_types ) ) {
+ $clean['post_types'][] = $post_type;
+ }
+ }
+ }
+
+ return $clean;
+ }
+
+ /**
+ * Default Values.
+ *
+ * @return array Default settings.
+ *
+ * @since 2011-06-02
+ * @access private
+ */
+ static function get_defaults() {
+ return array(
+ 'language' => 'en-US',
+ 'markup' => 'html',
+ 'multiple' => array(),
+ 'post_types' => array( 'post', 'page' ),
+ 'show_count' => 'true',
+ 'singular' => array(),
+ 'size' => 'standard',
+ );
+ }
+
+ static function get_post_types() {
+ $public = array();
+ $post_types = get_post_types();
+ foreach ( (array) $post_types as $post_type ) {
+ $obj = get_post_type_object( $post_type );
+ if ( ! isset( $obj->public ) ) {
+ continue;
+ }
+ if ( empty( $obj->public ) ) {
+ continue;
+ }
+ if ( empty( $obj->labels->singular_name ) ) {
+ continue;
+ }
+ $public[$post_type] = $obj->labels->singular_name;
+ }
+ return $public;
+ }
+
+ /**
+ * Singular Locations.
+ *
+ * @return array List of supported theme locations.
+ *
+ * @since 2011-06-02
+ * @access private
+ */
+ static function get_locations_singular() {
+ return array(
+ 'before_content' => __( 'Before the Content', self::$domain ),
+ 'after_content' => __( 'After the Content', self::$domain ),
+ );
+ }
+
+ /**
+ * Multiple Locations.
+ *
+ * @return array List of supported theme locations.
+ *
+ * @since 2011-06-02
+ * @access private
+ */
+ static function get_locations_multiple() {
+ return array(
+ 'before_content' => __( 'Before the Content', self::$domain ),
+ 'after_content' => __( 'After the Content', self::$domain ),
+ 'before_excerpt' => __( 'Before the Excerpt', self::$domain ),
+ 'after_excerpt' => __( 'After the Excerpt', self::$domain ),
+ );
+ }
+
+ /**
+ * Sizes.
+ *
+ * @see http://code.google.com/apis/+1button/#button-sizes
+ * @return array List of supported sizes.
+ *
+ * @since 2011-06-02
+ * @access private
+ */
+ static function get_sizes() {
+ return array(
+ 'small' => __( 'Small', self::$domain ),
+ 'medium' => __( 'Medium', self::$domain ),
+ 'standard' => __( 'Standard', self::$domain ),
+ 'tall' => __( 'Tall', self::$domain ),
+ );
+ }
+
+ /**
+ * Languages.
+ *
+ * @see http://code.google.com/apis/+1button/#languages
+ * @return array List of supported languages.
+ *
+ * @since 2011-06-02
+ * @access private
+ */
+ static function get_languages() {
+ return array(
+ 'ar' => __( 'Arabic', self::$domain ),
+ 'bg' => __( 'Bulgarian', self::$domain ),
+ 'ca' => __( 'Catalan', self::$domain ),
+ 'zh-CN' => __( 'Chinese (Simplified)', self::$domain ),
+ 'zh-TW' => __( 'Chinese (Traditional)', self::$domain ),
+ 'hr' => __( 'Croatian', self::$domain ),
+ 'cs' => __( 'Czech', self::$domain ),
+ 'da' => __( 'Danish', self::$domain ),
+ 'nl' => __( 'Dutch', self::$domain ),
+ 'en-GB' => __( 'English (UK)', self::$domain ),
+ 'en-US' => __( 'English (US)', self::$domain ),
+ 'et' => __( 'Estonian', self::$domain ),
+ 'fil' => __( 'Filipino', self::$domain ),
+ 'fi' => __( 'Finnish', self::$domain ),
+ 'fr' => __( 'French', self::$domain ),
+ 'de' => __( 'German', self::$domain ),
+ 'el' => __( 'Greek', self::$domain ),
+ 'iw' => __( 'Hebrew', self::$domain ),
+ 'hi' => __( 'Hindi', self::$domain ),
+ 'hu' => __( 'Hungarian', self::$domain ),
+ 'id' => __( 'Indonesian', self::$domain ),
+ 'it' => __( 'Italian', self::$domain ),
+ 'ja' => __( 'Japanese', self::$domain ),
+ 'ko' => __( 'Korean', self::$domain ),
+ 'lv' => __( 'Latvian', self::$domain ),
+ 'lt' => __( 'Lithuanian', self::$domain ),
+ 'ms' => __( 'Malay', self::$domain ),
+ 'no' => __( 'Norwegian', self::$domain ),
+ 'fa' => __( 'Persian', self::$domain ),
+ 'pl' => __( 'Polish', self::$domain ),
+ 'pt-BR' => __( 'Portuguese (Brazil)', self::$domain ),
+ 'pt-PT' => __( 'Portuguese (Portugal)', self::$domain ),
+ 'ro' => __( 'Romanian', self::$domain ),
+ 'ru' => __( 'Russian', self::$domain ),
+ 'sr' => __( 'Serbian', self::$domain ),
+ 'sk' => __( 'Slovak', self::$domain ),
+ 'sl' => __( 'Slovenian', self::$domain ),
+ 'es' => __( 'Spanish', self::$domain ),
+ 'es-419' => __( 'Spanish (Latin America)', self::$domain ),
+ 'sv' => __( 'Swedish', self::$domain ),
+ 'th' => __( 'Thai', self::$domain ),
+ 'tr' => __( 'Turkish', self::$domain ),
+ 'uk' => __( 'Ukrainian', self::$domain ),
+ 'vi' => __( 'Vietnamese', self::$domain ),
+ );
+ }
+}
BIN preview-sprite.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
43 style-settings-page.css
@@ -0,0 +1,43 @@
+#settings_page_mfields_plus_one label {
+ display:block;
+}
+#mfields_plus_one_size_wrap {
+ position:relative;
+}
+#mfields_plus_one_preview {
+ width:80px;
+ height:60px;
+ background-color:#eee;
+ border-radius:5px;
+ border:1px solid #ddd;
+ padding:5px;
+ position:absolute;
+ top:0;
+ left:200px;
+}
+#mfields_plus_one_preview div {
+ width:80px;
+ height:60px;
+ background:transparent url( 'preview-sprite.png' ) 0 -60px no-repeat;
+}
+#mfields_plus_one_preview div.small {
+ background-position:-480px 0;
+}
+#mfields_plus_one_preview div.small.count {
+ background-position:-400px 0;
+}
+#mfields_plus_one_preview div.medium {
+ background-position:-320px 0;
+}
+#mfields_plus_one_preview div.medium.count {
+ background-position:-240px 0;
+}
+#mfields_plus_one_preview div.standard {
+ background-position:-160px 0;
+}
+#mfields_plus_one_preview div.standard.count {
+ background-position:-80px 0;
+}
+#mfields_plus_one_preview div.tall {
+ background-position:0 0;
+}
17 style-settings-page.js
@@ -0,0 +1,17 @@
+jQuery( document ).ready( function( $ ) {
+ var preview = $( '#mfields_plus_one_preview div' );
+ var start_count = $( '#mfields_plus_one_count_true' ).attr( 'checked' );
+
+ $( '.mfields_plus_one_size' ).click( function() {
+ var val = $( this ).val();
+ preview.removeClass( 'small medium standard tall' );
+ preview.addClass( val );
+ } );
+ $( '.mfields_plus_one_count' ).click( function() {
+ preview.removeClass( 'count' );
+ if ( 'true' == $( this ).val() ) {
+ preview.addClass( 'count' );
+ }
+ console.log( 'taco: ' + $( this ).val() );
+ } );
+} );

0 comments on commit 16194a9

Please sign in to comment.