Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
28.79% covered (danger)
28.79%
19 / 66
28.57% covered (danger)
28.57%
2 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
jetpack_post_sharing_get_value
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
jetpack_post_sharing_update_value
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
jetpack_post_sharing_register_rest_field
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
2
sharing_admin_init
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
Sharing_Admin
0.00% covered (danger)
0.00%
0 / 33
0.00% covered (danger)
0.00%
0 / 3
462
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
12
 sharing_head
0.00% covered (danger)
0.00%
0 / 23
0.00% covered (danger)
0.00%
0 / 1
12
 admin_init
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 process_requests
n/a
0 / 0
n/a
0 / 0
1
 subscription_menu
n/a
0 / 0
n/a
0 / 0
1
 ajax_save_services
n/a
0 / 0
n/a
0 / 0
1
 ajax_new_service
n/a
0 / 0
n/a
0 / 0
1
 ajax_delete_service
n/a
0 / 0
n/a
0 / 0
1
 ajax_save_options
n/a
0 / 0
n/a
0 / 0
1
 output_preview
n/a
0 / 0
n/a
0 / 0
1
 output_service
n/a
0 / 0
n/a
0 / 0
1
 wrapper_admin_page
n/a
0 / 0
n/a
0 / 0
1
 management_page
n/a
0 / 0
n/a
0 / 0
1
 should_use_site_editor
n/a
0 / 0
n/a
0 / 0
1
 services_config_display
n/a
0 / 0
n/a
0 / 0
1
 sharing_block_display
n/a
0 / 0
n/a
0 / 0
1
 site_editor_prompt_display
n/a
0 / 0
n/a
0 / 0
1
1<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
2/**
3 * Set up Sharing functionality and management in wp-admin.
4 *
5 * @package automattic/jetpack
6 */
7
8// phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move classes to appropriately-named class files.
9
10use Automattic\Jetpack\Assets;
11use Automattic\Jetpack\Sharing_Likes\Settings\Post_Handler;
12use Automattic\Jetpack\Sharing_Likes\Settings\Services_Config;
13use Automattic\Jetpack\Sharing_Likes\Settings\Settings_Page;
14use Automattic\Jetpack\Status\Host;
15
16if ( ! defined( 'ABSPATH' ) ) {
17    exit( 0 );
18}
19
20if ( ! defined( 'WP_SHARING_PLUGIN_URL' ) ) {
21    define( 'WP_SHARING_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
22    define( 'WP_SHARING_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
23}
24
25/**
26 * Utilities to manage sharing settings from wp-admin.
27 */
28class Sharing_Admin {
29
30    /**
31     * The services configuration UI.
32     *
33     * @var Services_Config
34     */
35    private $services_config;
36
37    /**
38     * Constructor.
39     * Hook into WordPress to add our functionality.
40     */
41    public function __construct() {
42        require_once WP_SHARING_PLUGIN_DIR . 'sharing-service.php';
43
44        $this->services_config = new Services_Config();
45
46        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- nonces are handled in process_requests.
47        if ( isset( $_GET['page'] ) && $_GET['page'] === 'sharing' ) {
48            add_action( 'admin_init', array( $this, 'admin_init' ) );
49        }
50
51        // Insert our CSS and JS
52        add_action( 'load-settings_page_sharing', array( $this, 'sharing_head' ) );
53
54        // Catch AJAX
55        add_action( 'wp_ajax_sharing_save_services', array( $this->services_config, 'ajax_save_services' ) );
56        add_action( 'wp_ajax_sharing_save_options', array( $this->services_config, 'ajax_save_options' ) );
57        add_action( 'wp_ajax_sharing_new_service', array( $this->services_config, 'ajax_new_service' ) );
58        add_action( 'wp_ajax_sharing_delete_service', array( $this->services_config, 'ajax_delete_service' ) );
59    }
60
61    /**
62     * Enqueue scripts and styles on the sharing settings page.
63     *
64     * @return void
65     */
66    public function sharing_head() {
67        wp_enqueue_script(
68            'sharing-js',
69            Assets::get_file_url_for_environment(
70                '_inc/build/sharedaddy/admin-sharing.min.js',
71                'modules/sharedaddy/admin-sharing.js'
72            ),
73            array( 'jquery', 'jquery-ui-draggable', 'jquery-ui-droppable', 'jquery-ui-sortable', 'jquery-form' ),
74            JETPACK__VERSION,
75            false
76        );
77
78        // admin-sharing.js reads this before it submits, so leaving it undefined breaks service removal.
79        wp_add_inline_script(
80            'sharing-js',
81            'var sharing_loading_icon = ' . wp_json_encode( admin_url( '/images/loading.gif' ), JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ) . ';',
82            'before'
83        );
84
85        /**
86         * Filters the switch that if set to true allows Jetpack to use minified assets. Defaults to true
87         * if the SCRIPT_DEBUG constant is not set or set to false. The filter overrides it.
88         *
89         * @since 6.2.0
90         *
91         * @param boolean $var should Jetpack use minified assets.
92         */
93        $postfix = apply_filters( 'jetpack_should_use_minified_assets', true ) ? '.min' : '';
94        if ( is_rtl() ) {
95            wp_enqueue_style( 'sharing-admin', WP_SHARING_PLUGIN_URL . 'admin-sharing-rtl' . $postfix . '.css', false, JETPACK__VERSION );
96        } else {
97            wp_enqueue_style( 'sharing-admin', WP_SHARING_PLUGIN_URL . 'admin-sharing' . $postfix . '.css', false, JETPACK__VERSION );
98        }
99        wp_enqueue_style( 'sharing', WP_SHARING_PLUGIN_URL . 'sharing.css', false, JETPACK__VERSION );
100
101        wp_enqueue_style( 'social-logos' );
102        wp_enqueue_script( 'sharing-js-fe', WP_SHARING_PLUGIN_URL . 'sharing.js', array(), 4, false );
103        add_thickbox();
104    }
105
106    /**
107     * Load the process that handles saving changes on the sharing settings page.
108     *
109     * @return void
110     */
111    public function admin_init() {
112        $this->services_config->process_requests();
113    }
114
115    /**
116     * Save changes to sharing settings.
117     *
118     * @deprecated 16.3 Use Automattic\Jetpack\Sharing_Likes\Settings\Services_Config::process_requests() instead.
119     *
120     * @return void
121     */
122    public function process_requests() {
123        _deprecated_function( __METHOD__, 'jetpack-16.3', 'Automattic\Jetpack\Sharing_Likes\Settings\Services_Config::process_requests' );
124    }
125
126    /**
127     * Register Sharing settings menu page in Settings > Sharing.
128     *
129     * @deprecated 16.3 Use Automattic\Jetpack\Sharing_Likes\Settings\Settings_Page::register_menu() instead.
130     *
131     * @return void
132     */
133    public function subscription_menu() {
134        _deprecated_function( __METHOD__, 'jetpack-16.3', 'Automattic\Jetpack\Sharing_Likes\Settings\Settings_Page::register_menu' );
135    }
136
137    /**
138     * Save changes to sharing services via AJAX.
139     *
140     * @deprecated 16.3 Use Automattic\Jetpack\Sharing_Likes\Settings\Services_Config::ajax_save_services() instead.
141     *
142     * @return void
143     */
144    public function ajax_save_services() {
145        _deprecated_function( __METHOD__, 'jetpack-16.3', 'Automattic\Jetpack\Sharing_Likes\Settings\Services_Config::ajax_save_services' );
146    }
147
148    /**
149     * Create a new custom sharing service via AJAX.
150     *
151     * @deprecated 16.3 Use Automattic\Jetpack\Sharing_Likes\Settings\Services_Config::ajax_new_service() instead.
152     *
153     * @return void
154     */
155    public function ajax_new_service() {
156        _deprecated_function( __METHOD__, 'jetpack-16.3', 'Automattic\Jetpack\Sharing_Likes\Settings\Services_Config::ajax_new_service' );
157    }
158
159    /**
160     * Delete a sharing service via AJAX.
161     *
162     * @deprecated 16.3 Use Automattic\Jetpack\Sharing_Likes\Settings\Services_Config::ajax_delete_service() instead.
163     *
164     * @return void
165     */
166    public function ajax_delete_service() {
167        _deprecated_function( __METHOD__, 'jetpack-16.3', 'Automattic\Jetpack\Sharing_Likes\Settings\Services_Config::ajax_delete_service' );
168    }
169
170    /**
171     * Save changes to sharing settings via AJAX.
172     *
173     * @deprecated 16.3 Use Automattic\Jetpack\Sharing_Likes\Settings\Services_Config::ajax_save_options() instead.
174     *
175     * @return void
176     */
177    public function ajax_save_options() {
178        _deprecated_function( __METHOD__, 'jetpack-16.3', 'Automattic\Jetpack\Sharing_Likes\Settings\Services_Config::ajax_save_options' );
179    }
180
181    /**
182     * Display a preview of a sharing service.
183     *
184     * @deprecated 16.3 Use Automattic\Jetpack\Sharing_Likes\Settings\Services_Config::output_preview() instead.
185     *
186     * @param object $service Sharing service object.
187     *
188     * @return void
189     */
190    public function output_preview( $service ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
191        _deprecated_function( __METHOD__, 'jetpack-16.3', 'Automattic\Jetpack\Sharing_Likes\Settings\Services_Config::output_preview' );
192    }
193
194    /**
195     * Display a specific sharing service.
196     *
197     * @deprecated 16.3 Use Automattic\Jetpack\Sharing_Likes\Settings\Services_Config::output_service() instead.
198     *
199     * @param string $id            Service unique ID.
200     * @param object $service       Sharing service.
201     * @param bool   $show_dropdown Display a dropdown. Not in use at the moment.
202     *
203     * @return void
204     */
205    public function output_service( $id, $service, $show_dropdown = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
206        _deprecated_function( __METHOD__, 'jetpack-16.3', 'Automattic\Jetpack\Sharing_Likes\Settings\Services_Config::output_service' );
207    }
208
209    /**
210     * Display admin UI within a Jetpack header and footer.
211     *
212     * @deprecated 16.3 Use Automattic\Jetpack\Sharing_Likes\Settings\Settings_Page::render() instead.
213     *
214     * @return void
215     */
216    public function wrapper_admin_page() {
217        _deprecated_function( __METHOD__, 'jetpack-16.3', 'Automattic\Jetpack\Sharing_Likes\Settings\Settings_Page::render' );
218    }
219
220    /**
221     * Sharing settings inner page structure.
222     *
223     * @deprecated 16.3 Use Automattic\Jetpack\Sharing_Likes\Settings\Settings_Page::render() instead.
224     *
225     * @return void
226     */
227    public function management_page() {
228        _deprecated_function( __METHOD__, 'jetpack-16.3', 'Automattic\Jetpack\Sharing_Likes\Settings\Settings_Page::render' );
229    }
230
231    /**
232     * Check if we should encourage to use the site editor instead of the legacy sharing settings.
233     *
234     * @deprecated 16.3 Use Automattic\Jetpack\Sharing_Likes\Settings\Environment instead.
235     *
236     * @return bool
237     */
238    public function should_use_site_editor() {
239        _deprecated_function( __METHOD__, 'jetpack-16.3', 'Automattic\Jetpack\Sharing_Likes\Settings\Environment' );
240        return false;
241    }
242
243    /**
244     * Display services admin UI for settings.
245     *
246     * @deprecated 16.3 Use Automattic\Jetpack\Sharing_Likes\Settings\Services_Config::render() instead.
247     *
248     * @return void
249     */
250    public function services_config_display() {
251        _deprecated_function( __METHOD__, 'jetpack-16.3', 'Automattic\Jetpack\Sharing_Likes\Settings\Services_Config::render' );
252    }
253
254    /**
255     * Display sharing block admin UI for settings.
256     *
257     * @deprecated 16.3 Use Automattic\Jetpack\Sharing_Likes\Settings\Sharing_Section::render() instead.
258     *
259     * @return void
260     */
261    public function sharing_block_display() {
262        _deprecated_function( __METHOD__, 'jetpack-16.3', 'Automattic\Jetpack\Sharing_Likes\Settings\Sharing_Section::render' );
263    }
264
265    /**
266     * Display the "Go to the site editor" prompt.
267     *
268     * @deprecated 16.3 Use Automattic\Jetpack\Sharing_Likes\Settings\Sharing_Section::render() instead.
269     *
270     * @return void
271     */
272    public function site_editor_prompt_display() {
273        _deprecated_function( __METHOD__, 'jetpack-16.3', 'Automattic\Jetpack\Sharing_Likes\Settings\Sharing_Section::render' );
274    }
275}
276
277/**
278 * Callback to get the value for the jetpack_sharing_enabled field.
279 *
280 * When the sharing_disabled post_meta is unset, we follow the global setting in Sharing.
281 * When it is set to 1, we disable sharing on the post, regardless of the global setting.
282 * It is not possible to enable sharing on a post if it is disabled globally.
283 *
284 * @param array $post The post object.
285 *
286 * @return bool
287 */
288function jetpack_post_sharing_get_value( array $post ) {
289    if ( ! isset( $post['id'] ) ) {
290        return false;
291    }
292
293    // if sharing IS disabled on this post, enabled=false, so negate the meta
294    return ! get_post_meta( $post['id'], 'sharing_disabled', true );
295}
296
297/**
298 * Callback to set sharing_disabled post_meta when the
299 * jetpack_sharing_enabled field is updated.
300 *
301 * When the sharing_disabled post_meta is unset, we follow the global setting in Sharing.
302 * When it is set to 1, we disable sharing on the post, regardless of the global setting.
303 * It is not possible to enable sharing on a post if it is disabled globally.
304 *
305 * @param bool    $enable_sharing Should sharing be enabled on this post.
306 * @param WP_Post $post_object    The post object.
307 *
308 * @return int|bool
309 */
310function jetpack_post_sharing_update_value( $enable_sharing, $post_object ) {
311    if ( $enable_sharing ) {
312        // delete the override if we want to enable sharing
313        return delete_post_meta( $post_object->ID, 'sharing_disabled' );
314    } else {
315        return update_post_meta( $post_object->ID, 'sharing_disabled', true );
316    }
317}
318
319/**
320 * Add Sharing post_meta to the REST API Post response.
321 *
322 * @action rest_api_init
323 * @uses register_rest_field
324 * @link https://developer.wordpress.org/rest-api/extending-the-rest-api/modifying-responses/
325 */
326function jetpack_post_sharing_register_rest_field() {
327    $post_types = get_post_types( array( 'public' => true ) );
328    foreach ( $post_types as $post_type ) {
329        register_rest_field(
330            $post_type,
331            'jetpack_sharing_enabled',
332            array(
333                'get_callback'    => 'jetpack_post_sharing_get_value',
334                'update_callback' => 'jetpack_post_sharing_update_value',
335                'schema'          => array(
336                    'description' => __( 'Are sharing buttons enabled?', 'jetpack' ),
337                    'type'        => 'boolean',
338                ),
339            )
340        );
341
342        /**
343         * Ensures all public internal post-types support `sharing`
344         * This feature support flag is used by the REST API and Gutenberg.
345         */
346        add_post_type_support( $post_type, 'jetpack-sharing-buttons' );
347    }
348}
349
350// Add Sharing post_meta to the REST API Post response.
351add_action( 'rest_api_init', 'jetpack_post_sharing_register_rest_field' );
352
353// Some CPTs (e.g. Jetpack portfolios and testimonials) get registered with
354// restapi_theme_init because they depend on theme support, so let's also hook to that
355add_action( 'restapi_theme_init', 'jetpack_post_likes_register_rest_field', 20 );
356
357/**
358 * Initialize sharing settings in WP Admin.
359 *
360 * @return void
361 */
362function sharing_admin_init() {
363    global $sharing_admin;
364
365    $sharing_admin = new Sharing_Admin();
366
367    // load-jetpack.php registers the screen everywhere else, but does not run on
368    // WordPress.com Simple, where this file is the only Sharing code loaded.
369    if ( ( new Host() )->is_wpcom_simple() ) {
370        Settings_Page::init();
371        Post_Handler::init();
372    }
373}
374
375add_action( 'init', 'sharing_admin_init' );