<?php
/**
 *
 */
class Jetpack_Sharing_Shortcode {
	/**
	 * Singleton!
	 */
	private static $__instance = null;

	public static function instance() {
		if ( ! is_a( self::$__instance, __CLASS__ ) ) {
			self::$__instance = new self;
		}

		return self::$__instance;
	}

	/**
	 * Class properties
	 */
	private $text                   = null;
	private $services               = array();
	private $all_shortcode_services = array();

	/**
	 *
	 */
	private function __construct() {
		add_shortcode( 'jpshare', array( $this, 'do_shortcode' ) );

		// add_action( 'wp_footer', array( $this, 'set_footer_scripts' ), -999 );
	}

	/**
	 *
	 */
	public function do_shortcode( $atts ) {
		if ( class_exists( 'Jetpack' ) && method_exists( 'Jetpack', 'get_active_modules' ) && in_array( 'sharedaddy', Jetpack::get_active_modules() ) ) {
			// Parse shortcode settings
			$this->parse_atts( $atts );

			// Render button(s)
			add_filter( 'sharing_show', '__return_true', 9999 );
			add_filter( 'sharing_services_enabled', array( $this, 'set_enabled_services' ) );
			add_filter( 'sharing_enabled', array( $this, 'set_enabled_services' ) );
			add_filter( 'sharing_title', array( $this, 'set_title' ) );
			$buttons = sharing_display( '', false );
			remove_filter( 'sharing_enabled', array( $this, 'set_enabled_services' ) );
			remove_filter( 'sharing_services_enabled', array( $this, 'set_enabled_services' ) );
			remove_filter( 'sharing_show', '__return_true', 9999 );

			// Reset shortcode settings
			$this->text = null;
			$this->services = array();

			// Return whatever was rendered
			return $buttons;
		} else {
			// Return an empty string when Jetpack's sharing module isn't
			return '';
		}
	}

	/**
	 *
	 */
	private function parse_atts( $atts ) {
		$atts = shortcode_atts( array(
			'text'     => '',
			'services' => array(),
		), $atts, 'lafc_sharing_shortcode' );

		$this->text = isset( $atts['text'] ) && ! empty( $atts['text'] ) ? sanitize_text_field( $atts['text'] ) : '';
		$this->parse_services( $atts['services'] );
	}

	/**
	 * Validate shortcode sharing services and store for later use
	 */
	private function parse_services( $requested_services ) {
		$all_services = new Sharing_Service();
		$all_services = $all_services->get_all_services( true );

		if ( is_string( $requested_services ) ) {
			$requested_services = explode( ',', $requested_services );
		} elseif ( ! is_array( $requested_services ) ) {
			$requested_services = array();
		}
		$requested_services = array_flip( $requested_services );

		$requested_services = array_intersect_key( $all_services, $requested_services );

		foreach ( $requested_services as $service => $class ) {
			$requested_services[ $service ] = new $class( $service, array(
				'button_style' => 'icon-text',
			) );
		}

		$this->services               = $requested_services;
		$this->all_shortcode_services = array_merge( $this->all_shortcode_services, $requested_services );

		unset( $requested_services );
	}

	/**
	 * Set enabled sharing services based on parsed shortcode attribute
	 */
	public function set_enabled_services( $services ) {
		if ( is_array( $this->services ) && ! empty( $this->services ) ) {
			$services['visible'] = $this->services;
			$services['all']     = array_flip( array_keys( $this->services ) );
		}

		return $services;
	}

	/**
	 * Override post title with custom sharing text
	 */
	public function set_title( $title ) {
		return $this->text;
	}

	/**
	 *
	 */
	public function set_footer_scripts() {
		if ( is_array( $this->all_shortcode_services ) && ! empty( $this->all_shortcode_services ) ) {
			add_filter( 'sharing_services_enabled', array( $this, 'append_enabled_services_for_shortcodes' ) );
		}
	}

	/**
	 *
	 */
	public function append_enabled_services_for_shortcodes( $services ) {
		$services['visible'] = array_merge( $services['visible'], $this->all_shortcode_services );

		return $services;
	}
}
Jetpack_Sharing_Shortcode::instance();