Completed
Branch add/settings-update (80bb6a)
by Warwick
03:50
created

Output   A

Complexity

Total Complexity 33

Size/Duplication

Total Lines 177
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 77
c 2
b 0
f 0
dl 0
loc 177
rs 9.76
wmc 33

7 Methods

Rating   Name   Duplication   Size   Complexity  
A get_instance() 0 6 2
B assets() 0 36 6
D sharing_buttons() 0 54 18
A sharing_buttons_template() 0 2 1
A __construct() 0 8 1
A wp_kses_allowed_html() 0 4 1
A sharing_buttons_shortcode() 0 14 4
1
<?php
2
namespace lsx\sharing\classes\frontend;
3
4
/**
5
 * Houses the functions for the Settings page.
6
 *
7
 * @package lsx-sharing
8
 */
9
class Output {
10
11
	/**
12
	 * Holds class instance
13
	 *
14
	 * @since 1.0.0
15
	 *
16
	 * @var      object \lsx\sharing\classes\frontend\Output()
17
	 */
18
	protected static $instance = null;
19
20
	/**
21
	 * Contructor
22
	 */
23
	public function __construct() {
24
		add_action( 'wp_enqueue_scripts', array( $this, 'assets' ), 5 );
0 ignored issues
show
Bug introduced by
The function add_action was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

24
		/** @scrutinizer ignore-call */ 
25
  add_action( 'wp_enqueue_scripts', array( $this, 'assets' ), 5 );
Loading history...
25
		add_filter( 'wp_kses_allowed_html', array( $this, 'wp_kses_allowed_html' ), 10, 2 );
0 ignored issues
show
Bug introduced by
The function add_filter was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

25
		/** @scrutinizer ignore-call */ 
26
  add_filter( 'wp_kses_allowed_html', array( $this, 'wp_kses_allowed_html' ), 10, 2 );
Loading history...
26
		add_shortcode( 'lsx_sharing_buttons', array( $this, 'sharing_buttons_shortcode' ) );
0 ignored issues
show
Bug introduced by
The function add_shortcode was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

26
		/** @scrutinizer ignore-call */ 
27
  add_shortcode( 'lsx_sharing_buttons', array( $this, 'sharing_buttons_shortcode' ) );
Loading history...
27
		// Storefront (storefront_loop_post, storefront_single_post).
28
		add_action( 'storefront_post_content_before', array( $this, 'sharing_buttons_template' ), 20 );
29
		// WooCommerce.
30
		add_action( 'woocommerce_share', array( $this, 'sharing_buttons_template' ) );
31
	}
32
33
	/**
34
	 * Return an instance of this class.
35
	 *
36
	 * @since 1.0.0
37
	 *
38
	 * @return    object \lsx\sharing\classes\frontend\Output()    A single instance of this class.
39
	 */
40
	public static function get_instance() {
41
		// If the single instance hasn't been set, set it now.
42
		if ( null == self::$instance ) {
43
			self::$instance = new self();
44
		}
45
		return self::$instance;
46
	}
47
48
	/**
49
	 * Enques the assets.
50
	 */
51
	public function assets() {
52
		if ( defined( 'WP_DEBUG' ) && true === WP_DEBUG ) {
0 ignored issues
show
Bug introduced by
The constant lsx\sharing\classes\frontend\WP_DEBUG was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
53
			$min = '';
54
		} else {
55
			$min = '.min';
56
		}
57
		/* Remove assets completely if all sharing options are off */
58
59
		if ( \lsx\sharing\includes\functions\is_disabled() ) {
60
			return '';
61
		}
62
63
		//Set our variables
64
		global $post;
65
		$post_id    = false;
66
		$share_post = $post;
0 ignored issues
show
Unused Code introduced by
The assignment to $share_post is dead and can be removed.
Loading history...
67
		if ( false !== $post_id ) {
0 ignored issues
show
introduced by
The condition false !== $post_id is always false.
Loading history...
68
			$share_post = get_post( $post_id );
0 ignored issues
show
Bug introduced by
The function get_post was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

68
			$share_post = /** @scrutinizer ignore-call */ get_post( $post_id );
Loading history...
69
			$post_type  = get_post_type( $post_id );
0 ignored issues
show
Bug introduced by
The function get_post_type was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

69
			$post_type  = /** @scrutinizer ignore-call */ get_post_type( $post_id );
Loading history...
70
		} else {
71
			$post_type = get_post_type();
72
		}
73
74
		/* Only show the assets if the post type sharing option is on */
75
		if ( ! \lsx\sharing\includes\functions\is_pt_disabled( $post_type ) ) {
76
77
			wp_enqueue_script( 'lsx-sharing', LSX_SHARING_URL . 'assets/js/lsx-sharing' . $min . '.js', array( 'jquery' ), LSX_SHARING_VER, true );
0 ignored issues
show
Bug introduced by
The function wp_enqueue_script was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

77
			/** @scrutinizer ignore-call */ 
78
   wp_enqueue_script( 'lsx-sharing', LSX_SHARING_URL . 'assets/js/lsx-sharing' . $min . '.js', array( 'jquery' ), LSX_SHARING_VER, true );
Loading history...
78
79
			$params = apply_filters( 'lsx_sharing_js_params', array(
0 ignored issues
show
Bug introduced by
The function apply_filters was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

79
			$params = /** @scrutinizer ignore-call */ apply_filters( 'lsx_sharing_js_params', array(
Loading history...
80
				'ajax_url' => admin_url( 'admin-ajax.php' ),
0 ignored issues
show
Bug introduced by
The function admin_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

80
				'ajax_url' => /** @scrutinizer ignore-call */ admin_url( 'admin-ajax.php' ),
Loading history...
81
			));
82
83
			wp_localize_script( 'lsx-sharing', 'lsx_sharing_params', $params );
0 ignored issues
show
Bug introduced by
The function wp_localize_script was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

83
			/** @scrutinizer ignore-call */ 
84
   wp_localize_script( 'lsx-sharing', 'lsx_sharing_params', $params );
Loading history...
84
85
			wp_enqueue_style( 'lsx-sharing', LSX_SHARING_URL . 'assets/css/lsx-sharing.css', array(), LSX_SHARING_VER );
0 ignored issues
show
Bug introduced by
The function wp_enqueue_style was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

85
			/** @scrutinizer ignore-call */ 
86
   wp_enqueue_style( 'lsx-sharing', LSX_SHARING_URL . 'assets/css/lsx-sharing.css', array(), LSX_SHARING_VER );
Loading history...
86
			wp_style_add_data( 'lsx-sharing', 'rtl', 'replace' );
0 ignored issues
show
Bug introduced by
The function wp_style_add_data was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

86
			/** @scrutinizer ignore-call */ 
87
   wp_style_add_data( 'lsx-sharing', 'rtl', 'replace' );
Loading history...
87
88
		}
89
90
	}
91
92
	/**
93
	 * Display/return sharing buttons.
94
	 */
95
	public function sharing_buttons( $buttons = array( 'facebook', 'twitter', 'pinterest' ), $echo = false, $post_id = false ) {
96
		$sharing_content = '';
97
98
		if ( ( is_preview() || is_admin() ) && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
0 ignored issues
show
Bug introduced by
The function is_admin was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

98
		if ( ( is_preview() || /** @scrutinizer ignore-call */ is_admin() ) && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
Loading history...
Bug introduced by
The function is_preview was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

98
		if ( ( /** @scrutinizer ignore-call */ is_preview() || is_admin() ) && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
Loading history...
Bug introduced by
The constant lsx\sharing\classes\frontend\DOING_AJAX was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
99
			return '';
100
		}
101
102
		//Set our variables
103
		global $post;
104
		$share_post = $post;
105
		if ( false !== $post_id ) {
106
			$share_post = get_post( $post_id );
0 ignored issues
show
Bug introduced by
The function get_post was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

106
			$share_post = /** @scrutinizer ignore-call */ get_post( $post_id );
Loading history...
107
			$post_type = get_post_type( $post_id );
0 ignored issues
show
Bug introduced by
The function get_post_type was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

107
			$post_type = /** @scrutinizer ignore-call */ get_post_type( $post_id );
Loading history...
108
		} else {
109
			$post_type = get_post_type();
110
		}
111
112
		if ( \lsx\sharing\includes\functions\is_disabled() || \lsx\sharing\includes\functions\is_pt_disabled( $post_type ) ) {
113
			return '';
114
		}
115
116
		if ( ( is_array( $buttons ) && count( $buttons ) > 0 ) ) {
117
			$sharing_content .= '<div class="lsx-sharing-content"><p>';
118
119
			$sharing_text = \lsx\sharing\includes\functions\get_sharing_text( $post_type );
120
			if ( '' !== $sharing_text ) {
121
				$sharing_content .= '<span class="lsx-sharing-label">' . $sharing_text . '</span>';
122
			}
123
124
			foreach ( $buttons as $id => $button ) {
125
				$button_obj = new \lsx\sharing\classes\frontend\Button( $button, $this->options, $post_type );
0 ignored issues
show
Bug Best Practice introduced by
The property options does not exist on lsx\sharing\classes\frontend\Output. Did you maybe forget to declare it?
Loading history...
126
127
				if ( ! empty( $button_obj ) ) {
128
					$url = $button_obj->get_link( $share_post );
129
130
					if ( ! empty( $url ) ) {
131
						if ( 'email' === $button ) {
132
							if ( ! isset( $this->options['display'] ) || empty( $this->options['display']['sharing_email_form_id'] ) ) {
133
								continue;
134
							}
135
							$sharing_content .= '<span class="lsx-sharing-button lsx-sharing-button-' . esc_attr( $button ) . '"><a href="#lsx-sharing-email" data-toggle="modal" data-link="' . esc_url( $url ) . '"><span class="fa" aria-hidden="true"></span></a></span>';
0 ignored issues
show
Bug introduced by
The function esc_attr was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

135
							$sharing_content .= '<span class="lsx-sharing-button lsx-sharing-button-' . /** @scrutinizer ignore-call */ esc_attr( $button ) . '"><a href="#lsx-sharing-email" data-toggle="modal" data-link="' . esc_url( $url ) . '"><span class="fa" aria-hidden="true"></span></a></span>';
Loading history...
Bug introduced by
The function esc_url was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

135
							$sharing_content .= '<span class="lsx-sharing-button lsx-sharing-button-' . esc_attr( $button ) . '"><a href="#lsx-sharing-email" data-toggle="modal" data-link="' . /** @scrutinizer ignore-call */ esc_url( $url ) . '"><span class="fa" aria-hidden="true"></span></a></span>';
Loading history...
136
						} else {
137
							$sharing_content .= '<span class="lsx-sharing-button lsx-sharing-button-' . esc_attr( $button ) . '"><a href="' . esc_url( $url ) . '" target="_blank" rel="noopener noreferrer"><span class="fa" aria-hidden="true"></span></a></span>';
138
						}
139
					}
140
				}
141
			}
142
			$sharing_content .= '</p></div>';
143
		}
144
145
		if ( $echo ) {
146
			echo wp_kses_post( $sharing_content );
0 ignored issues
show
Bug introduced by
The function wp_kses_post was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

146
			echo /** @scrutinizer ignore-call */ wp_kses_post( $sharing_content );
Loading history...
147
		} else {
148
			return $sharing_content;
149
		}
150
	}
151
152
	/**
153
	 * Sharing buttons shortcode.
154
	 */
155
	public function sharing_buttons_shortcode( $atts ) {
156
		$atts = shortcode_atts( array(
0 ignored issues
show
Bug introduced by
The function shortcode_atts was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

156
		$atts = /** @scrutinizer ignore-call */ shortcode_atts( array(
Loading history...
157
			'buttons' => '',
158
		), $atts, 'lsx_sharing_buttons' );
159
160
		if ( empty( $atts['buttons'] ) ) {
161
			return '';
162
		}
163
164
		$no_whitespaces = preg_replace( '/\s*,\s*/', ',', filter_var( $atts['buttons'], FILTER_SANITIZE_STRING ) );
165
		$buttons        = explode( ',', $no_whitespaces );
166
167
		if ( is_array( $buttons ) && count( $buttons ) > 0 ) {
168
			return $this->sharing_buttons( $buttons );
169
		}
170
	}
171
172
	/**
173
	 * Display buttons (template hook).
174
	 */
175
	public function sharing_buttons_template() {
176
		echo wp_kses_post( $this->sharing_buttons() );
0 ignored issues
show
Bug introduced by
The function wp_kses_post was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

176
		echo /** @scrutinizer ignore-call */ wp_kses_post( $this->sharing_buttons() );
Loading history...
177
	}
178
179
	/**
180
	 * Allow data params for Bootstrap modal.
181
	 */
182
	public function wp_kses_allowed_html( $allowedtags, $context ) {
0 ignored issues
show
Unused Code introduced by
The parameter $context is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

182
	public function wp_kses_allowed_html( $allowedtags, /** @scrutinizer ignore-unused */ $context ) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
183
		$allowedtags['a']['data-toggle'] = true;
184
		$allowedtags['a']['data-link']   = true;
185
		return $allowedtags;
186
	}
187
}
188