summaryrefslogtreecommitdiff
blob: 44b2cbc8a0f354ff22186741820d91a5c7e73848 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
<?php
/**
 * Social Links.
 *
 * This feature will only be activated for themes that declare their support.
 * This can be done by adding code similar to the following during the
 * 'after_setup_theme' action:
 *
 * add_theme_support( 'social-links', array(
 *     'facebook', 'twitter', 'linkedin', 'tumblr', 'google_plus',
 * ) );
 */

function jetpack_theme_supports_social_links() {
	if ( current_theme_supports( 'social-links' ) && function_exists( 'publicize_init' ) ) {
		new Social_Links();
	}
}
add_action( 'init', 'jetpack_theme_supports_social_links', 30 );

if ( ! class_exists( 'Social_Links' ) ) {

	class Social_Links {

		/**
		 * The links the user set for each service.
		 *
		 * @var array
		 */
		private $links;

		/**
		 * A Publicize object.
		 *
		 * @var Publicize
		 */
		private $publicize;

		/**
		 * An array with all services that are supported by both Publicize and the
		 * currently active theme.
		 *
		 * @var array
		 */
		private $services = array();

		/**
		 * An array of the services the theme supports
		 *
		 * @var array
		 */
		private $theme_supported_services = array();

		/**
		 * Constructor.
		 */
		public function __construct() {
			$theme_support = get_theme_support( 'social-links' );

			/*
			An array of named arguments must be passed as the second parameter
			 * of add_theme_support().
			 */
			if ( empty( $theme_support[0] ) ) {
				return;
			}

			$this->theme_supported_services = $theme_support[0];
			$this->links                    = Jetpack_Options::get_option( 'social_links', array() );

			$this->admin_setup();

			add_filter( 'jetpack_has_social_links', array( $this, 'has_social_links' ) );
			add_filter( 'jetpack_get_social_links', array( $this, 'get_social_links' ) );

			foreach ( $theme_support[0] as $service ) {
				add_filter( "pre_option_jetpack-$service", array( $this, 'get_social_link_filter' ) ); // get_option( 'jetpack-service' );
				add_filter( "theme_mod_jetpack-$service", array( $this, 'get_social_link_filter' ) ); // get_theme_mod( 'jetpack-service' );
			}
		}

		public function admin_setup() {
			if ( ! current_user_can( 'manage_options' ) ) {
				return;
			}

			if ( ! is_admin() && ! $this->is_customize_preview() ) {
				return;
			}

			$this->publicize    = publicize_init();
			$publicize_services = $this->publicize->get_services( 'connected' );
			$this->services     = array_intersect( array_keys( $publicize_services ), $this->theme_supported_services );

			add_action( 'publicize_connected', array( $this, 'check_links' ), 20 );
			add_action( 'publicize_disconnected', array( $this, 'check_links' ), 20 );
			add_action( 'customize_register', array( $this, 'customize_register' ) );
			add_filter( 'sanitize_option_jetpack_options', array( $this, 'sanitize_link' ) );
		}

		/**
		 * Compares the currently saved links with the connected services and removes
		 * links from services that are no longer connected.
		 *
		 * @return void
		 */
		public function check_links() {
			$active_links = array_intersect_key( $this->links, array_flip( $this->services ) );

			if ( $active_links !== $this->links ) {
				$this->links = $active_links;
				Jetpack_Options::update_option( 'social_links', $active_links );
			}
		}

		/**
		 * Add social link dropdown to the Customizer.
		 *
		 * @param WP_Customize_Manager $wp_customize Theme Customizer object.
		 */
		public function customize_register( $wp_customize ) {
			$wp_customize->add_section(
				'jetpack_social_links',
				array(
					'title'    => esc_html__( 'Connect', 'jetpack' ),
					'priority' => 35,
				)
			);

			foreach ( array_keys( $this->publicize->get_services( 'all' ) ) as $service ) {
				$choices = $this->get_customize_select( $service );

				if ( empty( $choices ) ) {
					continue;
				}

				$wp_customize->add_setting(
					"jetpack_options[social_links][$service]",
					array(
						'type'    => 'option',
						'default' => '',
					)
				);

				$wp_customize->add_control(
					"jetpack-$service",
					array(
						'label'    => $this->publicize->get_service_label( $service ),
						'section'  => 'jetpack_social_links',
						'settings' => "jetpack_options[social_links][$service]",
						'type'     => 'select',
						'choices'  => $choices,
					)
				);
			}
		}

		/**
		 * Sanitizes social links.
		 *
		 * @param array $option The incoming values to be sanitized.
		 * @returns array
		 */
		public function sanitize_link( $option ) {
			foreach ( $this->services as $service ) {
				if ( ! empty( $option['social_links'][ $service ] ) ) {
					$option['social_links'][ $service ] = esc_url_raw( $option['social_links'][ $service ] );
				} else {
					unset( $option['social_links'][ $service ] );
				}
			}

			return $option;
		}

		/**
		 * Returns whether there are any social links set.
		 *
		 * @returns bool
		 */
		public function has_social_links() {
			return ! empty( $this->links );
		}

		/**
		 * Return available social links.
		 *
		 * @returns array
		 */
		public function get_social_links() {
			return $this->links;
		}

		/**
		 * Short-circuits get_option and get_theme_mod calls.
		 *
		 * @param string $link The incoming value to be replaced.
		 * @returns string $link The social link that we've got.
		 */
		public function get_social_link_filter( $link ) {
			if ( preg_match( '/_jetpack-(.+)$/i', current_filter(), $matches ) && ! empty( $this->links[ $matches[1] ] ) ) {
				return $this->links[ $matches[1] ];
			}

			return $link;
		}

		/**
		 * Puts together an array of choices for a specific service.
		 *
		 * @param string $service The social service.
		 * @return array An associative array with profile links and display names.
		 */
		private function get_customize_select( $service ) {
			$choices = array(
				'' => __( '&mdash; Select &mdash;', 'jetpack' ),
			);

			if ( isset( $this->links[ $service ] ) ) {
				$choices[ $this->links[ $service ] ] = $this->links[ $service ];
			}

			$connected_services = $this->publicize->get_services( 'connected' );
			if ( isset( $connected_services[ $service ] ) ) {
				foreach ( $connected_services[ $service ] as $c ) {
					$profile_link = $this->publicize->get_profile_link( $service, $c );

					if ( false === $profile_link ) {
						continue;
					}

					$choices[ $profile_link ] = $this->publicize->get_display_name( $service, $c );
				}
			}

			if ( 1 === count( $choices ) ) {
				return array();
			}

			return $choices;
		}

		/**
		 * Back-compat function for versions prior to 4.0.
		 */
		private function is_customize_preview() {
			global $wp_customize;
			return is_a( $wp_customize, 'WP_Customize_Manager' ) && $wp_customize->is_preview();
		}
	}

} // end if ( ! class_exists( 'Social_Links' )