Passed
Push — master ( f72747...fc6536 )
by Warwick
02:11
created

Output::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 7
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 14
rs 10
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
		// General Post Types.
33
		add_action( 'lsx_entry_after', array( $this, 'output_sharing' ) );
34
35
		// Tribe Events.
36
		add_filter( 'tribe_events_ical_single_event_links', array( $this, 'output_event_sharing' ), 10, 1 );
37
	}
38
39
	/**
40
	 * Return an instance of this class.
41
	 *
42
	 * @since 1.0.0
43
	 *
44
	 * @return    object \lsx\sharing\classes\frontend\Output()    A single instance of this class.
45
	 */
46
	public static function get_instance() {
47
		// If the single instance hasn't been set, set it now.
48
		if ( null == self::$instance ) {
49
			self::$instance = new self();
50
		}
51
		return self::$instance;
52
	}
53
54
	/**
55
	 * Enques the assets.
56
	 */
57
	public function assets() {
58
		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...
59
			$min = '';
60
		} else {
61
			$min = '.min';
62
		}
63
		/* Remove assets completely if all sharing options are off */
64
65
		if ( \lsx\sharing\includes\functions\is_disabled() ) {
66
			return '';
67
		}
68
69
		// Set our variables.
70
		$post_type = get_post_type();
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

70
		$post_type = /** @scrutinizer ignore-call */ get_post_type();
Loading history...
71
72
		/* Only show the assets if the post type sharing option is on */
73
		if ( ! \lsx\sharing\includes\functions\is_pt_disabled( $post_type ) ) {
74
75
			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

75
			/** @scrutinizer ignore-call */ 
76
   wp_enqueue_script( 'lsx-sharing', LSX_SHARING_URL . 'assets/js/lsx-sharing' . $min . '.js', array( 'jquery' ), LSX_SHARING_VER, true );
Loading history...
76
77
			$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

77
			$params = /** @scrutinizer ignore-call */ apply_filters( 'lsx_sharing_js_params', array(
Loading history...
78
				'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

78
				'ajax_url' => /** @scrutinizer ignore-call */ admin_url( 'admin-ajax.php' ),
Loading history...
79
			));
80
81
			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

81
			/** @scrutinizer ignore-call */ 
82
   wp_localize_script( 'lsx-sharing', 'lsx_sharing_params', $params );
Loading history...
82
83
			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

83
			/** @scrutinizer ignore-call */ 
84
   wp_enqueue_style( 'lsx-sharing', LSX_SHARING_URL . 'assets/css/lsx-sharing.css', array(), LSX_SHARING_VER );
Loading history...
84
			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

84
			/** @scrutinizer ignore-call */ 
85
   wp_style_add_data( 'lsx-sharing', 'rtl', 'replace' );
Loading history...
85
		}
86
	}
87
88
	/**
89
	 * Display/return sharing buttons.
90
	 */
91
	public function sharing_buttons( $buttons = array( 'facebook', 'twitter', 'pinterest' ), $echo = false, $post_id = false ) {
92
		$sharing_content = '';
93
94
		if ( ( is_preview() || is_admin() ) && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
0 ignored issues
show
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...
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

94
		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

94
		if ( ( /** @scrutinizer ignore-call */ is_preview() || is_admin() ) && ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
Loading history...
95
			return '';
96
		}
97
98
		//Set our variables
99
		global $post;
100
		$share_post = $post;
101
		if ( false !== $post_id ) {
102
			$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

102
			$share_post = /** @scrutinizer ignore-call */ get_post( $post_id );
Loading history...
103
			$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

103
			$post_type = /** @scrutinizer ignore-call */ get_post_type( $post_id );
Loading history...
104
		} else {
105
			$post_type = get_post_type();
106
		}
107
108
		if ( \lsx\sharing\includes\functions\is_disabled() || \lsx\sharing\includes\functions\is_pt_disabled( $post_type ) ) {
109
			return '';
110
		}
111
112
		if ( ( is_array( $buttons ) && count( $buttons ) > 0 ) ) {
113
			$sharing_content .= '<div class="lsx-sharing-content"><p>';
114
115
			$sharing_text = \lsx\sharing\includes\functions\get_sharing_text( $post_type );
116
			if ( '' !== $sharing_text ) {
117
				$sharing_content .= '<span class="lsx-sharing-label">' . $sharing_text . '</span>';
118
			}
119
120
			foreach ( $buttons as $id => $button ) {
121
				$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...
122
123
				if ( ! empty( $button_obj ) ) {
124
					$url = $button_obj->get_link( $share_post );
125
126
					if ( ! empty( $url ) ) {
127
						if ( 'email' === $button ) {
128
							if ( ! isset( $this->options['display'] ) || empty( $this->options['display']['sharing_email_form_id'] ) ) {
129
								continue;
130
							}
131
							$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

131
							$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

131
							$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...
132
						} else {
133
							$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>';
134
						}
135
					}
136
				}
137
			}
138
			$sharing_content .= '</p></div>';
139
		}
140
141
		if ( $echo ) {
142
			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

142
			echo /** @scrutinizer ignore-call */ wp_kses_post( $sharing_content );
Loading history...
143
		} else {
144
			return $sharing_content;
145
		}
146
	}
147
148
	/**
149
	 * Sharing buttons shortcode.
150
	 */
151
	public function sharing_buttons_shortcode( $atts ) {
152
		$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

152
		$atts = /** @scrutinizer ignore-call */ shortcode_atts( array(
Loading history...
153
			'buttons' => '',
154
		), $atts, 'lsx_sharing_buttons' );
155
156
		if ( empty( $atts['buttons'] ) ) {
157
			return '';
158
		}
159
160
		$no_whitespaces = preg_replace( '/\s*,\s*/', ',', filter_var( $atts['buttons'], FILTER_SANITIZE_STRING ) );
161
		$buttons        = explode( ',', $no_whitespaces );
162
163
		if ( is_array( $buttons ) && count( $buttons ) > 0 ) {
164
			return $this->sharing_buttons( $buttons );
165
		}
166
	}
167
168
	/**
169
	 * Display buttons (template hook).
170
	 */
171
	public function sharing_buttons_template() {
172
		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

172
		echo /** @scrutinizer ignore-call */ wp_kses_post( $this->sharing_buttons() );
Loading history...
173
	}
174
175
	/**
176
	 * Allow data params for Bootstrap modal.
177
	 */
178
	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

178
	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...
179
		$allowedtags['a']['data-toggle'] = true;
180
		$allowedtags['a']['data-link']   = true;
181
		return $allowedtags;
182
	}
183
184
	/**
185
	 * Outputs the sharing to the templates.
186
	 *
187
	 * @return void
188
	 */
189
	public function output_sharing() {
190
		if ( is_main_query() && is_single() && ! is_singular( array( 'post', 'page', 'product' ) ) ) {
0 ignored issues
show
Bug introduced by
The function is_singular 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

190
		if ( is_main_query() && is_single() && ! /** @scrutinizer ignore-call */ is_singular( array( 'post', 'page', 'product' ) ) ) {
Loading history...
Bug introduced by
The function is_main_query 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

190
		if ( /** @scrutinizer ignore-call */ is_main_query() && is_single() && ! is_singular( array( 'post', 'page', 'product' ) ) ) {
Loading history...
Bug introduced by
The function is_single 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

190
		if ( is_main_query() && /** @scrutinizer ignore-call */ is_single() && ! is_singular( array( 'post', 'page', 'product' ) ) ) {
Loading history...
191
			if ( \lsx\sharing\includes\functions\is_disabled() || \lsx\sharing\includes\functions\is_pt_disabled( get_post_type() ) ) {
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

191
			if ( \lsx\sharing\includes\functions\is_disabled() || \lsx\sharing\includes\functions\is_pt_disabled( /** @scrutinizer ignore-call */ get_post_type() ) ) {
Loading history...
192
				return '';
0 ignored issues
show
Bug Best Practice introduced by
The expression return '' returns the type string which is incompatible with the documented return type void.
Loading history...
193
			}
194
			?>
195
			<footer class="footer-meta clearfix">
196
				<div class="post-tags-wrapper">
197
					<?php $this->sharing_buttons_template(); ?>
198
				</div>
199
			</footer><!-- .footer-meta -->
200
			<?php
201
		}
202
	}
203
204
	/**
205
	 * Outputs the sharing below the events.
206
	 *
207
	 * @param string $ical_links
208
	 * @return string
209
	 */
210
	public function output_event_sharing( $ical_links = '' ) {
211
		if ( \lsx\sharing\includes\functions\is_disabled() || \lsx\sharing\includes\functions\is_pt_disabled( get_post_type() ) ) {
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

211
		if ( \lsx\sharing\includes\functions\is_disabled() || \lsx\sharing\includes\functions\is_pt_disabled( /** @scrutinizer ignore-call */ get_post_type() ) ) {
Loading history...
212
			return '';
213
		} else {
214
			$ical_links .= $this->sharing_buttons();
215
		}
216
		return $ical_links;
217
	}
218
}
219