Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
6.39% covered (danger)
6.39%
14 / 219
11.11% covered (danger)
11.11%
1 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
Services_Config
6.39% covered (danger)
6.39%
14 / 219
11.11% covered (danger)
11.11%
1 / 9
3530.42
0.00% covered (danger)
0.00%
0 / 1
 render
0.00% covered (danger)
0.00%
0 / 126
0.00% covered (danger)
0.00%
0 / 1
870
 global_options
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 output_service
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
20
 output_preview
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 1
30
 process_requests
90.00% covered (success)
90.00%
9 / 10
0.00% covered (danger)
0.00%
0 / 1
4.02
 ajax_save_services
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
30
 ajax_new_service
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 1
56
 ajax_delete_service
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
20
 ajax_save_options
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
42
1<?php
2/**
3 * The sharing services configuration UI.
4 *
5 * @package automattic/jetpack-sharing-likes
6 */
7
8declare( strict_types = 1 );
9
10namespace Automattic\Jetpack\Sharing_Likes\Settings;
11
12use Automattic\Jetpack\Status;
13
14/**
15 * The drag-and-drop services list and the settings that go with it.
16 *
17 * Moved here verbatim from Sharing_Admin, minus the block-theme notice it used
18 * to render for Simple only: Sharing_Section now shows that on every platform.
19 */
20final class Services_Config {
21
22    /**
23     * Display services admin UI for settings.
24     *
25     * @return void
26     */
27    public function render() {
28        // Only ever rendered with sharing buttons live, which is what loads Sharing_Service.
29        if ( ! class_exists( 'Sharing_Service' ) ) {
30            return;
31        }
32
33        $sharer  = new \Sharing_Service();
34        $enabled = $sharer->get_blog_services();
35        $global  = $sharer->get_global_options();
36
37        if ( ! isset( $global['sharing_label'] ) ) {
38            $global['sharing_label'] = __( 'Share this:', 'jetpack-sharing-likes' );
39        }
40        ?>
41        <div class="share_manage_options">
42        <p><?php esc_html_e( 'Add sharing buttons to your blog and allow your visitors to share posts with their friends.', 'jetpack-sharing-likes' ); ?></p>
43
44
45        <div id="services-config">
46            <table id="available-services">
47                    <tr>
48                    <td class="description">
49                        <h3><?php esc_html_e( 'Available Services', 'jetpack-sharing-likes' ); ?></h3>
50                        <p><?php esc_html_e( "Drag and drop the services you'd like to enable into the box below.", 'jetpack-sharing-likes' ); ?></p>
51                        <p><a href="#TB_inline?height=395&amp;width=600&amp;inlineId=new-service" class="thickbox" id="add-a-new-service"><?php esc_html_e( 'Add a new service', 'jetpack-sharing-likes' ); ?></a></p>
52                    </td>
53                    <td class="services">
54                        <ul class="services-available" style="height: 100px;">
55                            <?php foreach ( $sharer->get_all_services_blog() as $id => $service ) : ?>
56                                <?php
57                                if ( ! isset( $enabled['all'][ $id ] ) ) {
58                                        $this->output_service( $id, $service );
59                                }
60                                ?>
61                            <?php endforeach; ?>
62                        </ul>
63                        <?php
64                        if ( ( new Status() )->is_private_site() ) {
65                            echo '<p><strong>' . esc_html__( 'Please note that your services have been restricted because your site is private.', 'jetpack-sharing-likes' ) . '</strong></p>';
66                        }
67                        ?>
68                        <br class="clearing" />
69                    </td>
70                    </tr>
71            </table>
72
73            <table id="enabled-services">
74                <tr>
75                    <td class="description">
76                        <h3>
77                            <?php esc_html_e( 'Enabled Services', 'jetpack-sharing-likes' ); ?>
78                            <span class="spinner" style="vertical-align: middle"></span>
79                        </h3>
80                        <p><?php esc_html_e( 'Services dragged here will appear individually.', 'jetpack-sharing-likes' ); ?></p>
81                    </td>
82                    <td class="services" id="share-drop-target">
83                            <h2 id="drag-instructions"
84                            <?php
85                            if ( is_countable( $enabled['visible'] ) && count( $enabled['visible'] ) > 0 ) {
86                                echo ' style="display: none"';}
87                            ?>
88                            ><?php esc_html_e( 'Drag and drop available services here.', 'jetpack-sharing-likes' ); ?></h2>
89
90                                <ul class="services-enabled">
91                                    <?php foreach ( $enabled['visible'] as $id => $service ) : ?>
92                                        <?php $this->output_service( $id, $service, true ); ?>
93                                    <?php endforeach; ?>
94
95                                    <li class="end-fix"></li>
96                                </ul>
97                    </td>
98                    <td id="hidden-drop-target" class="services">
99                            <p><?php esc_html_e( 'Services dragged here will be hidden behind a share button.', 'jetpack-sharing-likes' ); ?></p>
100
101                            <ul class="services-hidden">
102                                    <?php foreach ( $enabled['hidden'] as $id => $service ) : ?>
103                                        <?php $this->output_service( $id, $service, true ); ?>
104                                    <?php endforeach; ?>
105                                    <li class="end-fix"></li>
106                            </ul>
107                    </td>
108                </tr>
109            </table>
110
111            <table id="live-preview">
112                <tr>
113                    <td class="description">
114                        <h3><?php esc_html_e( 'Live Preview', 'jetpack-sharing-likes' ); ?></h3>
115                    </td>
116                    <td class="services">
117                        <h2 <?php echo ( is_countable( $enabled['all'] ) && count( $enabled['all'] ) > 0 ) ? ' style="display: none"' : ''; ?>><?php esc_html_e( 'Sharing is off. Add services above to enable.', 'jetpack-sharing-likes' ); ?></h2>
118                        <div class="sharedaddy sd-sharing-enabled">
119                            <?php if ( is_countable( $enabled['all'] ) && count( $enabled['all'] ) > 0 ) : ?>
120                            <h3 class="sd-title"><?php echo esc_html( $global['sharing_label'] ); ?></h3>
121                            <?php endif; ?>
122                            <div class="sd-content">
123                                <ul class="preview">
124                                    <?php foreach ( $enabled['visible'] as $id => $service ) : ?>
125                                        <?php $this->output_preview( $service ); ?>
126                                    <?php endforeach; ?>
127
128                                    <?php if ( is_countable( $enabled['hidden'] ) && count( $enabled['hidden'] ) > 0 ) : ?>
129                                    <li class="advanced"><a href="#" class="sharing-anchor sd-button share-more"><span><?php esc_html_e( 'More', 'jetpack-sharing-likes' ); ?></span></a></li>
130                                    <?php endif; ?>
131                                </ul>
132
133                                <?php if ( is_countable( $enabled['hidden'] ) && count( $enabled['hidden'] ) > 0 ) : ?>
134                                <div class="sharing-hidden">
135                                    <div class="inner" style="display: none; <?php echo count( $enabled['hidden'] ) === 1 ? 'width:150px;' : ''; ?>">
136                                        <?php if ( count( $enabled['hidden'] ) === 1 ) : ?>
137                                            <ul style="background-image:none;">
138                                        <?php else : ?>
139                                            <ul>
140                                        <?php endif; ?>
141
142                                        <?php
143                                        foreach ( $enabled['hidden'] as $id => $service ) {
144                                            $this->output_preview( $service );
145                                        }
146                                        ?>
147                                        </ul>
148                                    </div>
149                                </div>
150                                <?php endif; ?>
151
152                                <ul class="archive" style="display:none;">
153                                <?php
154                                foreach ( $sharer->get_all_services_blog() as $id => $service ) :
155                                    if ( isset( $enabled['visible'][ $id ] ) ) {
156                                        $service = $enabled['visible'][ $id ];
157                                    } elseif ( isset( $enabled['hidden'][ $id ] ) ) {
158                                        $service = $enabled['hidden'][ $id ];
159                                    }
160
161                                    $service->button_style = 'icon-text';   // The archive needs the full text, which is removed in JS later.
162                                    $service->smart        = false;
163                                    $this->output_preview( $service );
164                                    endforeach;
165                                ?>
166                                    <li class="advanced"><a href="#" class="sharing-anchor sd-button share-more"><span><?php esc_html_e( 'More', 'jetpack-sharing-likes' ); ?></span></a></li>
167                                </ul>
168                            </div>
169                        </div>
170                        <br class="clearing" />
171                    </td>
172                </tr>
173            </table>
174
175                <form method="post" action="<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>" id="save-enabled-shares">
176                    <input type="hidden" name="action" value="sharing_save_services" />
177                    <input type="hidden" name="visible" value="<?php echo esc_attr( implode( ',', array_keys( $enabled['visible'] ) ) ); ?>" />
178                    <input type="hidden" name="hidden" value="<?php echo esc_attr( implode( ',', array_keys( $enabled['hidden'] ) ) ); ?>" />
179                    <input type="hidden" name="_wpnonce" value="<?php echo esc_attr( wp_create_nonce( 'sharing-options' ) ); ?>" />
180                </form>
181        </div>
182
183        <?php
184        ob_start();
185        ?>
186        <table class="form-table">
187            <tbody>
188                <tr valign="top">
189                    <th scope="row"><label><?php esc_html_e( 'Button style', 'jetpack-sharing-likes' ); ?></label></th>
190                    <td>
191                        <select name="button_style" id="button_style">
192                            <option<?php echo ( $global['button_style'] === 'icon-text' ) ? ' selected="selected"' : ''; ?> value="icon-text"><?php esc_html_e( 'Icon + text', 'jetpack-sharing-likes' ); ?></option>
193                            <option<?php echo ( $global['button_style'] === 'icon' ) ? ' selected="selected"' : ''; ?> value="icon"><?php esc_html_e( 'Icon only', 'jetpack-sharing-likes' ); ?></option>
194                            <option<?php echo ( $global['button_style'] === 'text' ) ? ' selected="selected"' : ''; ?> value="text"><?php esc_html_e( 'Text only', 'jetpack-sharing-likes' ); ?></option>
195                            <option<?php echo ( $global['button_style'] === 'official' ) ? ' selected="selected"' : ''; ?> value="official"><?php esc_html_e( 'Official buttons', 'jetpack-sharing-likes' ); ?></option>
196                        </select>
197                    </td>
198                </tr>
199                <tr valign="top">
200                    <th scope="row"><label><?php esc_html_e( 'Sharing label', 'jetpack-sharing-likes' ); ?></label></th>
201                    <td>
202                        <input type="text" name="sharing_label" value="<?php echo esc_attr( $global['sharing_label'] ); ?>" />
203                    </td>
204                </tr>
205                <?php echo self::global_options(); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- third-party field markup, escaped by whoever rendered it. ?>
206            </tbody>
207        </table>
208        <?php
209        Settings_Form::render_fields( Settings_Form::SECTION_SHARING, (string) ob_get_clean() );
210        ?>
211
212    <div id="new-service" style="display: none">
213        <form method="post" action="<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>" id="new-service-form">
214            <table class="form-table">
215                <tbody>
216                    <tr valign="top">
217                        <th scope="row" width="100"><label><?php esc_html_e( 'Service name', 'jetpack-sharing-likes' ); ?></label></th>
218                        <td>
219                            <input type="text" name="sharing_name" id="new_sharing_name" size="40" />
220                        </td>
221                    </tr>
222                    <tr valign="top">
223                        <th scope="row" width="100"><label><?php esc_html_e( 'Sharing URL', 'jetpack-sharing-likes' ); ?></label></th>
224                        <td>
225                            <input type="text" name="sharing_url" id="new_sharing_url" size="40" />
226
227                            <p><?php esc_html_e( 'You can add the following variables to your service sharing URL:', 'jetpack-sharing-likes' ); ?><br/>
228                            <code>%post_id%</code>, <code>%post_title%</code>, <code>%post_slug%</code>, <code>%post_url%</code>, <code>%post_full_url%</code>, <code>%post_excerpt%</code>, <code>%post_tags%</code>, <code>%home_url%</code></p>
229                        </td>
230                    </tr>
231                    <tr valign="top">
232                        <th scope="row" width="100"><label><?php esc_html_e( 'Icon URL', 'jetpack-sharing-likes' ); ?></label></th>
233                        <td>
234                            <input type="text" name="sharing_icon" id="new_sharing_icon" size="40" />
235                            <p><?php esc_html_e( 'Enter the URL of a 16x16px icon you want to use for this service.', 'jetpack-sharing-likes' ); ?></p>
236                        </td>
237                    </tr>
238                    <tr valign="top" width="100">
239                        <th scope="row"></th>
240                        <td>
241                            <input type="submit" class="button-primary" value="<?php esc_attr_e( 'Create Share Button', 'jetpack-sharing-likes' ); ?>" />
242                            <span class="spinner" style="vertical-align: middle"></span>
243                        </td>
244                    </tr>
245
246                    <?php
247                    /**
248                     * Fires after the custom sharing service form
249                     *
250                     * @module sharedaddy
251                     *
252                     * @since jetpack-1.1.0
253                     */
254                    do_action( 'sharing_new_service_form' );
255                    ?>
256                </tbody>
257            </table>
258
259            <?php
260            /**
261             * Fires at the bottom of the admin sharing settings screen.
262             *
263             * @module sharedaddy
264             *
265             * @since jetpack-1.6.0
266             */
267            do_action( 'post_admin_screen_sharing' );
268            ?>
269
270                <div class="inerror" style="display: none; margin-top: 15px">
271                    <p><?php esc_html_e( 'An error occurred creating your new sharing service - please check you gave valid details.', 'jetpack-sharing-likes' ); ?></p>
272                </div>
273
274            <input type="hidden" name="action" value="sharing_new_service" />
275            <input type="hidden" name="_wpnonce" value="<?php echo esc_attr( wp_create_nonce( 'sharing-new_service' ) ); ?>" />
276        </form>
277    </div>
278    </div>
279        <?php
280    }
281
282    /**
283     * The rows that close the settings table: the screen's own, then whatever
284     * `sharing_global_options` adds. `Post_Handler::save_global_options()` saves them.
285     */
286    public static function global_options(): string {
287        $markup = Sharing_Resources::render() . Twitter_Site_Tag::render();
288
289        ob_start();
290
291        /**
292         * Fires at the end of the sharing global options settings table.
293         *
294         * @module sharedaddy
295         *
296         * @since jetpack-1.1.0
297         */
298        do_action( 'sharing_global_options' );
299
300        $markup .= (string) ob_get_clean();
301
302        return trim( $markup );
303    }
304
305    /**
306     * Display a specific sharing service.
307     *
308     * @param string $id            Service unique ID.
309     * @param object $service       Sharing service.
310     * @param bool   $show_dropdown Display a dropdown. Not in use at the moment.
311     *
312     * @return void
313     */
314    public function output_service( $id, $service, $show_dropdown = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
315        $title             = '';
316        $klasses           = array( 'service', 'advanced', 'share-' . $service->get_class() );
317        $displayed_klasses = implode( ' ', $klasses );
318
319        if ( $service->is_deprecated() ) {
320            /* translators: %1$s is the name of a deprecated Sharing Service like "Google+" */
321            $title     = sprintf( __( 'The %1$s sharing service has shut down or discontinued support for sharing buttons. This sharing button is not displayed to your visitors and should be removed.', 'jetpack-sharing-likes' ), $service->get_name() );
322            $klasses[] = 'share-deprecated';
323        }
324
325        ?>
326    <li class="<?php echo esc_attr( $displayed_klasses ); ?>" id="<?php echo esc_attr( $service->get_id() ); ?>" tabindex="0" title="<?php echo esc_attr( $title ); ?>">
327        <span class="options-left"><?php echo esc_html( $service->get_name() ); ?></span>
328        <?php if ( str_starts_with( $service->get_id(), 'custom-' ) || $service->has_advanced_options() ) : ?>
329        <span class="close"><a href="#" class="remove">&times;</a></span>
330        <form method="post" action="<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>">
331            <input type="hidden" name="action" value="sharing_delete_service" />
332            <input type="hidden" name="service" value="<?php echo esc_attr( $id ); ?>" />
333            <input type="hidden" name="_wpnonce" value="<?php echo esc_attr( wp_create_nonce( 'sharing-options_' . $id ) ); ?>" />
334        </form>
335        <?php endif; ?>
336    </li>
337        <?php
338    }
339
340    /**
341     * Display a preview of a sharing service.
342     *
343     * @param object $service Sharing service object.
344     *
345     * @return void
346     */
347    public function output_preview( $service ) {
348        $klasses = array( 'advanced', 'preview-item' );
349
350        if ( $service->button_style !== 'text' || $service->has_custom_button_style() ) {
351            $klasses[] = 'preview-' . $service->get_class();
352            $klasses[] = 'share-' . $service->get_class();
353            if ( $service->is_deprecated() ) {
354                $klasses[] = 'share-deprecated';
355            }
356
357            if ( $service->get_class() !== $service->get_id() ) {
358                $klasses[] = 'preview-' . $service->get_id();
359            }
360        }
361
362        echo '<li class="' . esc_attr( implode( ' ', $klasses ) ) . '">';
363        $service->display_preview();
364        echo '</li>';
365    }
366
367    /**
368     * Save changes to sharing settings.
369     *
370     * @return void
371     */
372    public function process_requests() {
373        if (
374            isset( $_POST['_wpnonce'] )
375            && wp_verify_nonce( sanitize_key( wp_unslash( $_POST['_wpnonce'] ) ), 'sharing-options' )
376        ) {
377            $sharer = new \Sharing_Service();
378
379            /*
380             * set_global_options() rebuilds the whole global array from defaults, so a
381             * payload with no `show` clears it. Placement is edited in its own section,
382             * so carry the current placement through rather than losing it on every save.
383             */
384            $data = $_POST; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput -- set_global_options() validates each field.
385            if ( ! isset( $data['show'] ) ) {
386                $data['show'] = Placement_Section::selected_post_types();
387            }
388
389            $sharer->set_global_options( $data );
390            /**
391             * Fires when updating sharing settings.
392             *
393             * @module sharedaddy
394             *
395             * @since jetpack-1.1.0
396             */
397            do_action( 'sharing_admin_update' );
398
399            wp_safe_redirect( admin_url( 'options-general.php?page=sharing&update=saved' ) );
400            die( 0 );
401        }
402    }
403
404    /**
405     * Save changes to sharing services via AJAX.
406     *
407     * @return void
408     */
409    public function ajax_save_services() {
410        if (
411            isset( $_POST['_wpnonce'] )
412            && wp_verify_nonce( sanitize_key( wp_unslash( $_POST['_wpnonce'] ) ), 'sharing-options' )
413            && isset( $_POST['hidden'] )
414            && isset( $_POST['visible'] )
415        ) {
416            $sharer = new \Sharing_Service();
417
418            $sharer->set_blog_services(
419                explode( ',', sanitize_text_field( wp_unslash( $_POST['visible'] ) ) ),
420                explode( ',', sanitize_text_field( wp_unslash( $_POST['hidden'] ) ) )
421            );
422            die( 0 );
423        }
424    }
425
426    /**
427     * Create a new custom sharing service via AJAX.
428     *
429     * @return never
430     */
431    public function ajax_new_service() {
432        if (
433            isset( $_POST['_wpnonce'] )
434            && isset( $_POST['sharing_name'] )
435            && isset( $_POST['sharing_url'] )
436            && isset( $_POST['sharing_icon'] )
437            && wp_verify_nonce( sanitize_key( wp_unslash( $_POST['_wpnonce'] ) ), 'sharing-new_service' )
438        ) {
439            $sharer  = new \Sharing_Service();
440            $service = $sharer->new_service(
441                sanitize_text_field( wp_unslash( $_POST['sharing_name'] ) ),
442                esc_url_raw( wp_unslash( $_POST['sharing_url'] ) ),
443                esc_url_raw( wp_unslash( $_POST['sharing_icon'] ) )
444            );
445
446            if ( $service ) {
447                $this->output_service( $service->get_id(), $service );
448                echo '<!--->';
449                $service->button_style = 'icon-text';
450                $this->output_preview( $service );
451
452                die( 0 );
453            }
454        }
455
456        // Fail
457        die( '1' );
458    }
459
460    /**
461     * Delete a sharing service via AJAX.
462     *
463     * @return void
464     */
465    public function ajax_delete_service() {
466        if (
467            isset( $_POST['_wpnonce'] )
468            && isset( $_POST['service'] )
469            && wp_verify_nonce(
470                sanitize_key( wp_unslash( $_POST['_wpnonce'] ) ),
471                'sharing-options_' . sanitize_text_field( wp_unslash( $_POST['service'] ) )
472            )
473        ) {
474            $sharer = new \Sharing_Service();
475            $sharer->delete_service( sanitize_text_field( wp_unslash( $_POST['service'] ) ) );
476        }
477    }
478
479    /**
480     * Save changes to sharing settings via AJAX.
481     *
482     * @return void
483     */
484    public function ajax_save_options() {
485        if (
486            isset( $_POST['_wpnonce'] )
487            && isset( $_POST['service'] )
488            && wp_verify_nonce(
489                sanitize_key( wp_unslash( $_POST['_wpnonce'] ) ),
490                'sharing-options_' . sanitize_text_field( wp_unslash( $_POST['service'] ) )
491            )
492        ) {
493            $sharer  = new \Sharing_Service();
494            $service = $sharer->get_service( sanitize_text_field( wp_unslash( $_POST['service'] ) ) );
495
496            if ( $service && $service instanceof \Sharing_Advanced_Source ) {
497                $service->update_options( $_POST );
498
499                $sharer->set_service( sanitize_text_field( wp_unslash( $_POST['service'] ) ), $service );
500            }
501
502            $this->output_service( $service->get_id(), $service, true );
503            echo '<!--->';
504            $service->button_style = 'icon-text';
505            $this->output_preview( $service );
506            die( 0 );
507        }
508    }
509}