Completed
Push — sync/json-endpoints-19apr2017 ( 4d1744 )
by
unknown
64:46 queued 53:25
created

Social_Links::check_links()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nc 2
nop 0
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
1
<?php
2
/**
3
 * Social Links.
4
 *
5
 * This feature will only be activated for themes that declare their support.
6
 * This can be done by adding code similar to the following during the
7
 * 'after_setup_theme' action:
8
 *
9
 * add_theme_support( 'social-links', array(
10
 *     'facebook', 'twitter', 'linkedin', 'tumblr', 'google_plus',
11
 * ) );
12
 */
13
14
function jetpack_theme_supports_social_links() {
15
	if ( current_theme_supports( 'social-links' ) && function_exists( 'publicize_init' ) ) {
16
		new Social_Links();
17
	}
18
}
19
add_action( 'init', 'jetpack_theme_supports_social_links', 30 );
20
21
if ( ! class_exists( 'Social_Links' ) ) {
22
23
class Social_Links {
24
25
	/**
26
	 * The links the user set for each service.
27
	 *
28
	 * @var array
29
	 */
30
	private $links;
31
32
	/**
33
	 * A Publicize object.
34
	 *
35
	 * @var Publicize
36
	 */
37
	private $publicize;
38
39
	/**
40
	 * An array with all services that are supported by both Publicize and the
41
	 * currently active theme.
42
	 *
43
	 * @var array
44
	 */
45
	private $services = array();
46
47
	/**
48
	 * An array of the services the theme supports
49
	 *
50
	 * @var array
51
	 */
52
	private $theme_supported_services = array();
53
54
	/**
55
	 * Constructor.
56
	 */
57
	public function __construct() {
58
		$theme_support = get_theme_support( 'social-links' );
59
60
		/* An array of named arguments must be passed as the second parameter
61
		 * of add_theme_support().
62
		 */
63
		if ( empty( $theme_support[0] ) )
64
			return;
65
66
		$this->theme_supported_services = $theme_support[0];
67
		$this->links = Jetpack_Options::get_option( 'social_links', array() );
0 ignored issues
show
Documentation Bug introduced by
It seems like \Jetpack_Options::get_op...social_links', array()) of type * is incompatible with the declared type array of property $links.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
68
69
		$this->admin_setup();
70
71
		add_filter( 'jetpack_has_social_links', array( $this, 'has_social_links' ) );
72
		add_filter( 'jetpack_get_social_links', array( $this, 'get_social_links' ) );
73
74
		foreach ( $theme_support[0] as $service ) {
75
			add_filter( "pre_option_jetpack-$service", array( $this, 'get_social_link_filter' ) ); // get_option( 'jetpack-service' );
76
			add_filter( "theme_mod_jetpack-$service",  array( $this, 'get_social_link_filter' ) ); // get_theme_mod( 'jetpack-service' );
77
		}
78
	}
79
80
	public function admin_setup() {
81
		if ( ! current_user_can( 'manage_options' ) ) {
82
			return;
83
		}
84
85
		if ( ! is_admin() && ! $this->is_customize_preview() ) {
86
			return;
87
		}
88
89
		$this->publicize = publicize_init();
90
		$publicize_services = $this->publicize->get_services( 'connected' );
91
		$this->services  = array_intersect( array_keys( $publicize_services ), $this->theme_supported_services );
92
93
		add_action( 'publicize_connected', array( $this, 'check_links' ), 20 );
94
		add_action( 'publicize_disconnected', array( $this, 'check_links' ), 20 );
95
		add_action( 'customize_register', array( $this, 'customize_register' ) );
96
		add_filter( 'sanitize_option_jetpack_options', array( $this, 'sanitize_link' ) );
97
	}
98
99
	/**
100
	 * Compares the currently saved links with the connected services and removes
101
	 * links from services that are no longer connected.
102
	 *
103
	 * @return void
104
	 */
105
	public function check_links() {
106
		$active_links = array_intersect_key( $this->links, array_flip( $this->services ) );
107
108
		if ( $active_links !== $this->links ) {
109
			$this->links = $active_links;
110
			Jetpack_Options::update_option( 'social_links', $active_links );
111
		}
112
	}
113
114
	/**
115
	 * Add social link dropdown to the Customizer.
116
	 *
117
	 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
118
	 */
119
	public function customize_register( $wp_customize ) {
120
		$wp_customize->add_section( 'jetpack_social_links', array(
121
			'title'    => __( 'Connect', 'jetpack' ),
122
			'priority' => 35,
123
		) );
124
125
		foreach ( array_keys( $this->publicize->get_services() ) as $service ) {
0 ignored issues
show
Bug introduced by
The call to get_services() misses a required argument $filter.

This check looks for function calls that miss required arguments.

Loading history...
126
			$choices = $this->get_customize_select( $service );
127
128
			if ( empty( $choices ) ) {
129
				continue;
130
			}
131
132
			$wp_customize->add_setting( "jetpack_options[social_links][$service]", array(
133
				'type'    => 'option',
134
				'default' => '',
135
			) );
136
137
			$wp_customize->add_control( "jetpack-$service", array(
138
				'label'    => $this->publicize->get_service_label( $service ),
139
				'section'  => 'jetpack_social_links',
140
				'settings' => "jetpack_options[social_links][$service]",
141
				'type'     => 'select',
142
				'choices'  => $choices,
143
			) );
144
		}
145
	}
146
147
	/**
148
	 * Sanitizes social links.
149
	 *
150
	 * @param array $option The incoming values to be sanitized.
151
	 * @returns array
152
	 */
153
	public function sanitize_link( $option ) {
154
		foreach ( $this->services as $service ) {
155
			if ( ! empty( $option['social_links'][ $service ] ) )
156
				$option['social_links'][ $service ] = esc_url_raw( $option['social_links'][ $service ] );
157
			else
158
				unset( $option['social_links'][ $service ] );
159
		}
160
161
		return $option;
162
	}
163
164
	/**
165
	 * Returns whether there are any social links set.
166
	 *
167
	 * @returns bool
168
	 */
169
	public function has_social_links() {
170
		return ! empty( $this->links );
171
	}
172
173
	/**
174
	 * Return available social links.
175
	 *
176
	 * @returns array
177
	 */
178
	public function get_social_links() {
179
		return $this->links;
180
	}
181
182
	/**
183
	 * Short-circuits get_option and get_theme_mod calls.
184
	 *
185
	 * @param string $link The incoming value to be replaced.
186
	 * @returns string $link The social link that we've got.
187
	 */
188
	public function get_social_link_filter( $link ) {
189
		if ( preg_match( '/_jetpack-(.+)$/i', current_filter(), $matches ) && ! empty( $this->links[ $matches[1] ] ) )
190
			return $this->links[ $matches[1] ];
191
192
		return $link;
193
	}
194
195
	/**
196
	 * Puts together an array of choices for a specific service.
197
	 *
198
	 * @param string $service The social service.
199
	 * @return array An associative array with profile links and display names.
200
	 */
201
	private function get_customize_select( $service ) {
202
		$choices = array(
203
			'' => __( '&mdash; Select &mdash;', 'jetpack' )
204
		);
205
206
		if ( isset( $this->links[ $service ] ) ) {
207
			$choices[ $this->links[ $service ] ] = $this->links[ $service ];
208
		}
209
210
		$connected_services = $this->publicize->get_services( 'connected' );
211
		if ( isset( $connected_services[ $service ] ) ) {
212
			foreach ( $connected_services[ $service ] as $c ) {
213
				$profile_link = $this->publicize->get_profile_link( $service, $c );
214
215
				if ( false === $profile_link ) {
216
					continue;
217
				}
218
219
				$choices[ $profile_link ] = $this->publicize->get_display_name( $service, $c );
220
			}
221
		}
222
223
		if ( 1 === count( $choices ) ) {
224
			return array();
225
		}
226
227
		return $choices;
228
	}
229
230
	/**
231
	 * Back-compat function for versions prior to 4.0.
232
	 */
233
	private function is_customize_preview() {
234
		global $wp_customize;
235
		return is_a( $wp_customize, 'WP_Customize_Manager' ) && $wp_customize->is_preview();
236
	}
237
}
238
239
} // end if ( ! class_exists( 'Social_Links' )
240