Completed
Push — master ( 54663f...a7969f )
by Daryl
02:04
created

WP_Share_This::_wp_enqueue_scripts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Clubdeuce\WPShareThis;
4
5
/**
6
 * Class WP_Share_This
7
 * @package Clubdeuce\WPShareThis
8
 *
9
 * @link http://www.sharethis.com/support/customization/how-to-set-custom-buttons/
10
 */
11
class WP_Share_This {
12
13
	/**
14
	 * @var bool
15
	 */
16
	private static $_facebook_og = true;
17
18
	/**
19
	 * @var string
20
	 */
21
	private static $_id = null;
22
23
	/**
24
	 * @var array
25
	 */
26
	private static $_services = array();
27
28
	/**
29
	 *
30
	 */
31
	public static function initialize() {
32
33
		add_action( 'wp_enqueue_scripts', array( __CLASS__, '_wp_enqueue_scripts' ) );
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

33
		/** @scrutinizer ignore-call */ 
34
  add_action( 'wp_enqueue_scripts', array( __CLASS__, '_wp_enqueue_scripts' ) );
Loading history...
34
		add_action( 'wp_head', array( __CLASS__, '_wp_head' ) );
35
36
	}
37
38
	/**
39
	 *
40
	 */
41
	public static function _wp_enqueue_scripts() {
42
43
		$id = self::$_id;
44
45
		wp_enqueue_script('sharethis', "//platform-api.sharethis.com/js/sharethis.js#property={$id}&product=unknown", null, false, 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

45
		/** @scrutinizer ignore-call */ 
46
  wp_enqueue_script('sharethis', "//platform-api.sharethis.com/js/sharethis.js#property={$id}&product=unknown", null, false, true );
Loading history...
46
47
	}
48
49
	/**
50
	 *
51
	 */
52
	public static function _wp_head() {
53
54
		if ( self::$_facebook_og ) {
55
			self::_facebook_og();
56
		}
57
58
	}
59
60
	/**
61
	 * The ShareThis account id.
62
	 *
63
	 * @param string $id
64
	 */
65
	public static function register_id( $id ) {
66
67
		self::$_id = $id;
68
69
	}
70
71
	/**
72
	 * @param string $service
73
	 * @param array  $params
74
	 */
75
	public static function register_service( $service, $params = array() ) {
76
77
		self::$_services[ $service ] = $params;
78
79
	}
80
81
	/**
82
	 * @param array $args
83
	 */
84
	public static function the_sharing_links( $args = array() ) {
85
86
		$args = wp_parse_args( $args, array(
0 ignored issues
show
Bug introduced by
The function wp_parse_args 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
		$args = /** @scrutinizer ignore-call */ wp_parse_args( $args, array(
Loading history...
87
			'post' => get_post(),
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

87
			'post' => /** @scrutinizer ignore-call */ get_post(),
Loading history...
88
		) );
89
90
		foreach ( self::$_services as $service => $params ) {
91
			$args = array_merge( $params, $args );
92
			self::_render_sharing_link( $args, $service, $args['post'] );
93
		}
94
95
	}
96
97
	/**
98
	 * @param bool $use
99
	 *
100
	 * @since 0.0.2
101
	 */
102
	public static function use_og( $use = true ) {
103
104
		self::$_facebook_og = $use;
105
106
	}
107
108
	/**
109
	 * @param array    $params
110
	 * @param string   $service
111
	 * @param \WP_Post $post
112
	 */
113
	private static function _render_sharing_link( $params, $service, $post ) {
114
115
		$classes = apply_filters( "wpst_link_classes_{$service}", 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

115
		$classes = /** @scrutinizer ignore-call */ apply_filters( "wpst_link_classes_{$service}", array() );
Loading history...
116
117
		// set some defaults
118
		$args = wp_parse_args( $params, array(
0 ignored issues
show
Bug introduced by
The function wp_parse_args 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

118
		$args = /** @scrutinizer ignore-call */ wp_parse_args( $params, array(
Loading history...
119
			'url'         => get_the_permalink(),
0 ignored issues
show
Bug introduced by
The function get_the_permalink 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

119
			'url'         => /** @scrutinizer ignore-call */ get_the_permalink(),
Loading history...
120
			'short_url'   => null,
121
			'title'       => get_the_title(),
0 ignored issues
show
Bug introduced by
The function get_the_title 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

121
			'title'       => /** @scrutinizer ignore-call */ get_the_title(),
Loading history...
122
			'image'       => null,
123
			'description' => null,
124
			'username'    => null,
125
			'message'     => get_the_excerpt(),
0 ignored issues
show
Bug introduced by
The function get_the_excerpt 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

125
			'message'     => /** @scrutinizer ignore-call */ get_the_excerpt(),
Loading history...
126
			'share_count' => true,
127
		) );
128
129
		// if we have a post, we will use post values for the defaults
130
		if ( $post instanceof \WP_Post ) {
131
			$args = wp_parse_args( $params, array(
132
				'url'         => get_permalink( $post ),
0 ignored issues
show
Bug introduced by
The function get_permalink 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

132
				'url'         => /** @scrutinizer ignore-call */ get_permalink( $post ),
Loading history...
133
				'short_url'   => null,
134
				'title'       => get_the_title( $post ),
135
				'image'       => null,
136
				'description' => get_the_excerpt( $post ),
137
				'username'    => null,
138
				'message'     => get_the_excerpt( $post ),
139
				'share_count' => true,
140
			) );
141
142
			if ( has_post_thumbnail( $post ) ) {
0 ignored issues
show
Bug introduced by
The function has_post_thumbnail 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
			if ( /** @scrutinizer ignore-call */ has_post_thumbnail( $post ) ) {
Loading history...
143
				$args['image'] = get_the_post_thumbnail_url( $post );
0 ignored issues
show
Bug introduced by
The function get_the_post_thumbnail_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

143
				$args['image'] = /** @scrutinizer ignore-call */ get_the_post_thumbnail_url( $post );
Loading history...
144
			}
145
		}
146
147
		printf(
148
			'<div data-network="%1$s" class="st-custom-button %2$s"%3$s%4$s%5$s%6$s%7$s%8$s%9$s>%10$s%11$s</div>',
149
			$service,
150
			implode( ' ', apply_filters( 'wpst_link_classes', $classes, $service ) ),
151
			self::_item_sharing_property( 'url',         $args['url'] ),
152
			self::_item_sharing_property( 'short_url',   $args['short_url'] ),
153
			self::_item_sharing_property( 'title',       $args['title'] ),
154
			self::_item_sharing_property( 'image',       $args['image'] ),
155
			self::_item_sharing_property( 'description', $args['description'] ),
156
			self::_item_sharing_property( 'username',    $args['username'] ),
157
			self::_item_sharing_property( 'message',     $args['message']  . "\r\n\r\n" . $args['url'] ),
158
			apply_filters( 'wpst_link_text', ucfirst( $service ) ),
159
			self::_item_sharing_count( $args['share_count'] )
160
		);
161
162
	}
163
164
	/**
165
	 * @link https://developers.facebook.com/docs/sharing/webmasters#basic
166
	 */
167
	private static function _facebook_og() {
168
169
		printf( '<meta property="og:url" content="%1$s" />' . PHP_EOL, self::_og_url() );
170
		print '<meta property="og:type" content="article" />' . PHP_EOL;
171
		printf( '<meta property="og:title" content="%1$s" />' . PHP_EOL, self::_og_title() );
172
173
		if ( ! ( empty( $description = self::_og_description() ) ) ) {
174
			printf(	'<meta property="og:description" content="%1$s" />' . PHP_EOL, $description );
175
		}
176
177
		if ( $image_url = self::_og_image() ) {
178
			printf( '<meta property="og:image" content="%1$s" />' . PHP_EOL, $image_url );
179
		}
180
181
	}
182
183
	/**
184
	 * @return mixed|void
185
	 */
186
	private static function _og_url() {
187
188
		$url = get_permalink();
0 ignored issues
show
Bug introduced by
The function get_permalink 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

188
		$url = /** @scrutinizer ignore-call */ get_permalink();
Loading history...
189
190
		if ( is_home() || is_front_page() ) {
0 ignored issues
show
Bug introduced by
The function is_front_page 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_home() || /** @scrutinizer ignore-call */ is_front_page() ) {
Loading history...
Bug introduced by
The function is_home 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_home() || is_front_page() ) {
Loading history...
191
			$url = home_url();
0 ignored issues
show
Bug introduced by
The function home_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

191
			$url = /** @scrutinizer ignore-call */ home_url();
Loading history...
192
		}
193
194
		return apply_filters( 'wpst_og_url', esc_url( $url ) );
0 ignored issues
show
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

194
		return apply_filters( 'wpst_og_url', /** @scrutinizer ignore-call */ esc_url( $url ) );
Loading history...
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

194
		return /** @scrutinizer ignore-call */ apply_filters( 'wpst_og_url', esc_url( $url ) );
Loading history...
195
196
	}
197
198
	/**
199
	 * @return mixed|void
200
	 */
201
	private static function _og_title() {
202
203
		$title = get_the_title();
0 ignored issues
show
Bug introduced by
The function get_the_title 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

203
		$title = /** @scrutinizer ignore-call */ get_the_title();
Loading history...
204
205
		if ( is_home() || is_front_page() ) {
0 ignored issues
show
Bug introduced by
The function is_home 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

205
		if ( /** @scrutinizer ignore-call */ is_home() || is_front_page() ) {
Loading history...
Bug introduced by
The function is_front_page 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

205
		if ( is_home() || /** @scrutinizer ignore-call */ is_front_page() ) {
Loading history...
206
			$title = get_bloginfo( 'name' );
0 ignored issues
show
Bug introduced by
The function get_bloginfo 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

206
			$title = /** @scrutinizer ignore-call */ get_bloginfo( 'name' );
Loading history...
207
		}
208
209
		return apply_filters( 'wpst_og_title', wp_kses_post( $title ) );
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

209
		return apply_filters( 'wpst_og_title', /** @scrutinizer ignore-call */ wp_kses_post( $title ) );
Loading history...
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

209
		return /** @scrutinizer ignore-call */ apply_filters( 'wpst_og_title', wp_kses_post( $title ) );
Loading history...
210
211
	}
212
213
	/**
214
	 * @return mixed|void
215
	 */
216
	private static function _og_description() {
217
218
		$description = apply_filters( 'the_excerpt', get_the_excerpt() );
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

218
		$description = /** @scrutinizer ignore-call */ apply_filters( 'the_excerpt', get_the_excerpt() );
Loading history...
Bug introduced by
The function get_the_excerpt 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

218
		$description = apply_filters( 'the_excerpt', /** @scrutinizer ignore-call */ get_the_excerpt() );
Loading history...
219
220
		if ( is_home() || is_front_page() ) {
0 ignored issues
show
Bug introduced by
The function is_home 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

220
		if ( /** @scrutinizer ignore-call */ is_home() || is_front_page() ) {
Loading history...
Bug introduced by
The function is_front_page 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

220
		if ( is_home() || /** @scrutinizer ignore-call */ is_front_page() ) {
Loading history...
221
			$description = get_bloginfo( 'description' );
0 ignored issues
show
Bug introduced by
The function get_bloginfo 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

221
			$description = /** @scrutinizer ignore-call */ get_bloginfo( 'description' );
Loading history...
222
		}
223
224
		return apply_filters( 'wpst_og_description', esc_html( $description ) );
0 ignored issues
show
Bug introduced by
The function esc_html 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

224
		return apply_filters( 'wpst_og_description', /** @scrutinizer ignore-call */ esc_html( $description ) );
Loading history...
225
226
	}
227
228
	/**
229
	 * @return mixed|void
230
	 */
231
	private static function _og_image() {
232
233
		$image_url = get_the_post_thumbnail_url();
0 ignored issues
show
Bug introduced by
The function get_the_post_thumbnail_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

233
		$image_url = /** @scrutinizer ignore-call */ get_the_post_thumbnail_url();
Loading history...
234
235
		if ( is_home() || is_front_page() ) {
0 ignored issues
show
Bug introduced by
The function is_home 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

235
		if ( /** @scrutinizer ignore-call */ is_home() || is_front_page() ) {
Loading history...
Bug introduced by
The function is_front_page 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

235
		if ( is_home() || /** @scrutinizer ignore-call */ is_front_page() ) {
Loading history...
236
			$image_url = '';
237
		}
238
239
		return apply_filters( 'wpst_og_image', esc_url( $image_url ) );
0 ignored issues
show
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

239
		return apply_filters( 'wpst_og_image', /** @scrutinizer ignore-call */ esc_url( $image_url ) );
Loading history...
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

239
		return /** @scrutinizer ignore-call */ apply_filters( 'wpst_og_image', esc_url( $image_url ) );
Loading history...
240
241
	}
242
243
	/**
244
	 * @param string $property
245
	 * @param string $value
246
	 *
247
	 * @return string
248
	 */
249
	private static function _item_sharing_property( $property, $value ) {
250
251
		$text  = '';
252
		$maybe = apply_filters( "wpst_item_{$property}", $value );
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

252
		$maybe = /** @scrutinizer ignore-call */ apply_filters( "wpst_item_{$property}", $value );
Loading history...
253
254
		if ( ! empty( $maybe ) ) {
255
			$text = sprintf( ' data-%1$s="%2$s" ', str_replace('_', '-', $property ), esc_attr( $maybe ) );
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

255
			$text = sprintf( ' data-%1$s="%2$s" ', str_replace('_', '-', $property ), /** @scrutinizer ignore-call */ esc_attr( $maybe ) );
Loading history...
256
		}
257
258
		return $text;
259
260
	}
261
262
	/**
263
	 * @param  bool $show
264
	 *
265
	 * @return string
266
	 */
267
	private static function _item_sharing_count( $show ) {
268
269
		$text = '';
270
271
		if ( $show ) {
272
			$text = '<span class="count"></span>';
273
		}
274
275
		return $text;
276
277
	}
278
279
}
280