Passed
Push — master ( a7969f...6ee9e8 )
by Daryl
05:17
created

wp-share-this.php (39 issues)

Labels
Severity
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
	const VERSION = '0.0.3';
14
15
	/**
16
	 * @var bool
17
	 */
18
	private static $_facebook_og = true;
19
20
	/**
21
	 * @var string
22
	 */
23
	private static $_id = null;
24
25
	/**
26
	 * @var array
27
	 */
28
	private static $_services = array();
29
30
	/**
31
	 *
32
	 */
33
	public static function initialize() {
34
35
		add_action( 'wp_enqueue_scripts', array( __CLASS__, '_wp_enqueue_scripts' ) );
0 ignored issues
show
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

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

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

88
		$args = /** @scrutinizer ignore-call */ wp_parse_args( $args, array(
Loading history...
89
			'post' => get_post(),
0 ignored issues
show
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

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

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

120
		$args = /** @scrutinizer ignore-call */ wp_parse_args( $params, array(
Loading history...
121
			'url'         => get_the_permalink(),
0 ignored issues
show
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

121
			'url'         => /** @scrutinizer ignore-call */ get_the_permalink(),
Loading history...
122
			'short_url'   => null,
123
			'title'       => get_the_title(),
0 ignored issues
show
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

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

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

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

144
			if ( /** @scrutinizer ignore-call */ has_post_thumbnail( $post ) ) {
Loading history...
145
				$args['image'] = get_the_post_thumbnail_url( $post );
0 ignored issues
show
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

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

190
		$url = /** @scrutinizer ignore-call */ get_permalink();
Loading history...
191
192
		if ( is_home() || is_front_page() ) {
0 ignored issues
show
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

192
		if ( is_home() || /** @scrutinizer ignore-call */ is_front_page() ) {
Loading history...
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

192
		if ( /** @scrutinizer ignore-call */ is_home() || is_front_page() ) {
Loading history...
193
			$url = home_url();
0 ignored issues
show
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

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

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

196
		return /** @scrutinizer ignore-call */ apply_filters( 'wpst_og_url', esc_url( $url ) );
Loading history...
197
198
	}
199
200
	/**
201
	 * @return mixed|void
202
	 */
203
	private static function _og_type() {
204
205
		$type = 'website';
206
207
		if ( is_single() ) {
0 ignored issues
show
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

207
		if ( /** @scrutinizer ignore-call */ is_single() ) {
Loading history...
208
			$type = 'article';
209
		}
210
211
		return apply_filters( 'wpst_og_type', $type );
0 ignored issues
show
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

211
		return /** @scrutinizer ignore-call */ apply_filters( 'wpst_og_type', $type );
Loading history...
212
213
	}
214
215
216
	/**
217
	 * @return mixed|void
218
	 */
219
	private static function _og_title() {
220
221
		$title = get_the_title();
0 ignored issues
show
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

221
		$title = /** @scrutinizer ignore-call */ get_the_title();
Loading history...
222
223
		if ( is_home() || is_front_page() ) {
0 ignored issues
show
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

223
		if ( /** @scrutinizer ignore-call */ is_home() || is_front_page() ) {
Loading history...
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

223
		if ( is_home() || /** @scrutinizer ignore-call */ is_front_page() ) {
Loading history...
224
			$title = get_bloginfo( 'name' );
0 ignored issues
show
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

224
			$title = /** @scrutinizer ignore-call */ get_bloginfo( 'name' );
Loading history...
225
		}
226
227
		return apply_filters( 'wpst_og_title', wp_kses_post( $title ) );
0 ignored issues
show
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

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

227
		return /** @scrutinizer ignore-call */ apply_filters( 'wpst_og_title', wp_kses_post( $title ) );
Loading history...
228
229
	}
230
231
	/**
232
	 * @return mixed|void
233
	 */
234
	private static function _og_description() {
235
236
		$description = apply_filters( 'the_excerpt', get_the_excerpt() );
0 ignored issues
show
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

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

236
		$description = apply_filters( 'the_excerpt', /** @scrutinizer ignore-call */ get_the_excerpt() );
Loading history...
237
238
		if ( is_home() || is_front_page() ) {
0 ignored issues
show
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

238
		if ( is_home() || /** @scrutinizer ignore-call */ is_front_page() ) {
Loading history...
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

238
		if ( /** @scrutinizer ignore-call */ is_home() || is_front_page() ) {
Loading history...
239
			$description = get_bloginfo( 'description' );
0 ignored issues
show
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

239
			$description = /** @scrutinizer ignore-call */ get_bloginfo( 'description' );
Loading history...
240
		}
241
242
		return apply_filters( 'wpst_og_description', esc_html( $description ) );
0 ignored issues
show
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

242
		return apply_filters( 'wpst_og_description', /** @scrutinizer ignore-call */ esc_html( $description ) );
Loading history...
243
244
	}
245
246
	/**
247
	 * @return mixed|void
248
	 */
249
	private static function _og_image() {
250
251
		$image_url = get_the_post_thumbnail_url();
0 ignored issues
show
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

251
		$image_url = /** @scrutinizer ignore-call */ get_the_post_thumbnail_url();
Loading history...
252
253
		if ( is_home() || is_front_page() ) {
0 ignored issues
show
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

253
		if ( /** @scrutinizer ignore-call */ is_home() || is_front_page() ) {
Loading history...
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

253
		if ( is_home() || /** @scrutinizer ignore-call */ is_front_page() ) {
Loading history...
254
			$image_url = '';
255
		}
256
257
		return apply_filters( 'wpst_og_image', esc_url( $image_url ) );
0 ignored issues
show
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

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

257
		return apply_filters( 'wpst_og_image', /** @scrutinizer ignore-call */ esc_url( $image_url ) );
Loading history...
258
259
	}
260
261
	/**
262
	 * @param string $property
263
	 * @param string $value
264
	 *
265
	 * @return string
266
	 */
267
	private static function _item_sharing_property( $property, $value ) {
268
269
		$text  = '';
270
		$maybe = apply_filters( "wpst_item_{$property}", $value );
0 ignored issues
show
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

270
		$maybe = /** @scrutinizer ignore-call */ apply_filters( "wpst_item_{$property}", $value );
Loading history...
271
272
		if ( ! empty( $maybe ) ) {
273
			$text = sprintf( ' data-%1$s="%2$s" ', str_replace('_', '-', $property ), esc_attr( $maybe ) );
0 ignored issues
show
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

273
			$text = sprintf( ' data-%1$s="%2$s" ', str_replace('_', '-', $property ), /** @scrutinizer ignore-call */ esc_attr( $maybe ) );
Loading history...
274
		}
275
276
		return $text;
277
278
	}
279
280
	/**
281
	 * @param  bool $show
282
	 *
283
	 * @return string
284
	 */
285
	private static function _item_sharing_count( $show ) {
286
287
		$text = '';
288
289
		if ( $show ) {
290
			$text = '<span class="count"></span>';
291
		}
292
293
		return $text;
294
295
	}
296
297
}
298