Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
6.98% covered (danger)
6.98%
3 / 43
57.14% covered (warning)
57.14%
4 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
jetpack_post_sharing_get_value
n/a
0 / 0
n/a
0 / 0
1
jetpack_post_sharing_update_value
n/a
0 / 0
n/a
0 / 0
1
jetpack_post_sharing_register_rest_field
n/a
0 / 0
n/a
0 / 0
1
sharing_admin_init
100.00% covered (success)
100.00%
3 / 3
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\Initializer;
12use Automattic\Jetpack\Sharing_Likes\Post_Sharing_Switch;
13use Automattic\Jetpack\Sharing_Likes\Settings\Services_Config;
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 * @deprecated 16.3 Use Automattic\Jetpack\Sharing_Likes\Post_Sharing_Switch::get_value() instead.
281 *
282 * @param array $post The post object.
283 *
284 * @return bool
285 */
286function jetpack_post_sharing_get_value( array $post ) {
287    _deprecated_function( __FUNCTION__, 'jetpack-16.3', 'Automattic\Jetpack\Sharing_Likes\Post_Sharing_Switch::get_value' );
288    return Post_Sharing_Switch::get_value( $post );
289}
290
291/**
292 * Callback to set sharing_disabled post_meta when the
293 * jetpack_sharing_enabled field is updated.
294 *
295 * @deprecated 16.3 Use Automattic\Jetpack\Sharing_Likes\Post_Sharing_Switch::update_value() instead.
296 *
297 * @param bool    $enable_sharing Should sharing be enabled on this post.
298 * @param WP_Post $post_object    The post object.
299 *
300 * @return int|bool
301 */
302function jetpack_post_sharing_update_value( $enable_sharing, $post_object ) {
303    _deprecated_function( __FUNCTION__, 'jetpack-16.3', 'Automattic\Jetpack\Sharing_Likes\Post_Sharing_Switch::update_value' );
304    return Post_Sharing_Switch::update_value( $enable_sharing, $post_object );
305}
306
307/**
308 * Add Sharing post_meta to the REST API Post response.
309 *
310 * @deprecated 16.3 Use Automattic\Jetpack\Sharing_Likes\Post_Sharing_Switch::register_rest_field() instead.
311 */
312function jetpack_post_sharing_register_rest_field() {
313    _deprecated_function( __FUNCTION__, 'jetpack-16.3', 'Automattic\Jetpack\Sharing_Likes\Post_Sharing_Switch::register_rest_field' );
314    Post_Sharing_Switch::register_rest_field();
315}
316
317Post_Sharing_Switch::init();
318
319/**
320 * Initialize sharing settings in WP Admin.
321 *
322 * @return void
323 */
324function sharing_admin_init() {
325    global $sharing_admin;
326
327    $sharing_admin = new Sharing_Admin();
328
329    // load-jetpack.php sets the package up everywhere else, but does not run on WordPress.com
330    // Simple, where this file is the only Sharing code loaded, public-api requests included.
331    if ( ( new Host() )->is_wpcom_simple() ) {
332        Initializer::init();
333    }
334}
335
336add_action( 'init', 'sharing_admin_init' );