Passed
Push — master ( 4f39d7...08b433 )
by Daryl
02:00
created

WP_Share_This::the_sharing_links()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
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
	 *
83
	 */
84
	public static function the_sharing_links( $post = null ) {
85
86
		array_walk( self::$_services, array( __CLASS__, '_render_sharing_link' ), $post );
87
88
	}
89
90
	/**
91
	 * @param array    $params
92
	 * @param string   $service
93
	 * @param \WP_Post $post
94
	 */
95
	public static function _render_sharing_link( $params, $service, $post ) {
96
97
		$classes = array();
98
99
		// set some defaults
100
		$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

100
		$args = /** @scrutinizer ignore-call */ wp_parse_args( $params, array(
Loading history...
101
			'url'         => null,
102
			'short-url'   => null,
103
			'title'       => null,
104
			'image'       => null,
105
			'description' => null,
106
			'username'    => null,
107
			'message'     => null,
108
			'share_count' => true,
109
		) );
110
111
		// if we have a post, we will use post values for the defaults
112
		if ( $post instanceof \WP_Post ) {
113
			$args = wp_parse_args( $params, array(
114
				'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

114
				'url'         => /** @scrutinizer ignore-call */ get_permalink( $post ),
Loading history...
115
				'short-url'   => null,
116
				'title'       => $post->post_title,
117
				'image'       => null,
118
				'description' => get_the_excerpt( $post ),
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

118
				'description' => /** @scrutinizer ignore-call */ get_the_excerpt( $post ),
Loading history...
119
				'username'    => null,
120
				'message'     => null,
121
				'share_count' => true,
122
			) );
123
124
			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

124
			if ( /** @scrutinizer ignore-call */ has_post_thumbnail( $post ) ) {
Loading history...
125
				$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

125
				$args['image'] = /** @scrutinizer ignore-call */ get_the_post_thumbnail_url( $post );
Loading history...
126
			}
127
		}
128
129
		printf(
130
			'<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>',
131
			$service,
132
			implode( ' ', apply_filters( 'wpst_link_classes', $classes, $service ) ),
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

132
			implode( ' ', /** @scrutinizer ignore-call */ apply_filters( 'wpst_link_classes', $classes, $service ) ),
Loading history...
133
			self::_item_sharing_property( 'url',         $args['url'] ),
134
			self::_item_sharing_property( 'short_url',   $args['short_url'] ),
135
			self::_item_sharing_property( 'title',       $args['title'] ),
136
			self::_item_sharing_property( 'image',       $args['image'] ),
137
			self::_item_sharing_property( 'description', $args['description'] ),
138
			self::_item_sharing_property( 'username',    $args['username'] ),
139
			self::_item_sharing_property( 'message',     $args['message'] ),
140
			apply_filters( 'wpst_link_text', ucfirst( $service ) ),
141
			self::_item_sharing_count( $args['share_count'] )
142
		);
143
144
	}
145
146
	/**
147
	 * @link https://developers.facebook.com/docs/sharing/webmasters#basic
148
	 */
149
	private static function _facebook_og() {
150
151
		printf( '<meta property="og:url" content="%1$s" />' . PHP_EOL, self::_og_url() );
152
		print '<meta property="og:type" content="article" />' . PHP_EOL;
153
		printf( '<meta property="og:title" content="%1$s" />' . PHP_EOL, self::_og_title() );
154
155
		if ( ! ( empty( $description = self::_og_description() ) ) ) {
156
			printf(	'<meta property="og:description" content="%1$s" />' . PHP_EOL, $description );
157
		}
158
159
		if ( $image_url = self::_og_image() ) {
160
			printf( '<meta property="og:image" content="%1$s" />' . PHP_EOL, $image_url );
161
		}
162
163
	}
164
165
	/**
166
	 * @return mixed|void
167
	 */
168
	private static function _og_url() {
169
170
		$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

170
		$url = /** @scrutinizer ignore-call */ get_permalink();
Loading history...
171
172
		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

172
		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

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

173
			$url = /** @scrutinizer ignore-call */ home_url();
Loading history...
174
		}
175
176
		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

176
		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

176
		return /** @scrutinizer ignore-call */ apply_filters( 'wpst_og_url', esc_url( $url ) );
Loading history...
177
178
	}
179
180
	/**
181
	 * @return mixed|void
182
	 */
183 View Code Duplication
	private static function _og_title() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
184
185
		$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

185
		$title = /** @scrutinizer ignore-call */ get_the_title();
Loading history...
186
187
		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

187
		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

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

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

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

191
		return apply_filters( 'wpst_og_title', /** @scrutinizer ignore-call */ wp_kses_post( $title ) );
Loading history...
192
193
	}
194
195
	/**
196
	 * @return mixed|void
197
	 */
198 View Code Duplication
	private static function _og_description() {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
199
200
		$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

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

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

202
		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

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

203
			$description = /** @scrutinizer ignore-call */ get_bloginfo( 'description' );
Loading history...
204
		}
205
206
		return apply_filters( 'wpst_og_description', wp_kses_post( $description ) );
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

206
		return apply_filters( 'wpst_og_description', /** @scrutinizer ignore-call */ wp_kses_post( $description ) );
Loading history...
207
208
	}
209
210
	/**
211
	 * @return mixed|void
212
	 */
213
	private static function _og_image() {
214
215
		$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

215
		$image_url = /** @scrutinizer ignore-call */ get_the_post_thumbnail_url();
Loading history...
216
217
		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

217
		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

217
		if ( /** @scrutinizer ignore-call */ is_home() || is_front_page() ) {
Loading history...
218
			$image_url = '';
219
		}
220
221
		return apply_filters( 'wpst_og_image', esc_url( $image_url ) );
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

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

221
		return apply_filters( 'wpst_og_image', /** @scrutinizer ignore-call */ esc_url( $image_url ) );
Loading history...
222
223
	}
224
225
	/**
226
	 * @param string $property
227
	 * @param string $value
228
	 *
229
	 * @return string
230
	 */
231
	private static function _item_sharing_property( $property, $value ) {
232
233
		$text  = '';
234
		$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

234
		$maybe = /** @scrutinizer ignore-call */ apply_filters( "wpst_item_{$property}", $value );
Loading history...
235
236
		if ( ! empty( $maybe ) ) {
237
			$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

237
			$text = sprintf( ' data-%1$s="%2$s" ', str_replace('_', '-', $property ), /** @scrutinizer ignore-call */ esc_attr( $maybe ) );
Loading history...
238
		}
239
240
		return $text;
241
242
	}
243
244
	/**
245
	 * @param  bool $show
246
	 *
247
	 * @return string
248
	 */
249
	private static function _item_sharing_count( $show ) {
250
251
		$text = '';
252
253
		if ( $show ) {
254
			$text = '<span class="count"></span>';
255
		}
256
257
		return $text;
258
259
	}
260
261
}
262