Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
20.63% covered (danger)
20.63%
13 / 63
25.00% covered (danger)
25.00%
2 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
Publicize_Setup
20.63% covered (danger)
20.63%
13 / 63
25.00% covered (danger)
25.00%
2 / 8
449.42
0.00% covered (danger)
0.00%
0 / 1
 configure
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 should_load
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 pre_initialization
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
20
 on_jetpack_feature_publicize_enabled
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 1
90
 add_filters_and_actions_for_screen
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
4
 maybe_add_share_action
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
3
 add_share_action
33.33% covered (danger)
33.33%
3 / 9
0.00% covered (danger)
0.00%
0 / 1
5.67
 get_blog_id
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * Main Publicize class.
4 *
5 * @package automattic/jetpack
6 */
7
8namespace Automattic\Jetpack\Publicize;
9
10use Automattic\Jetpack\Current_Plan;
11use Automattic\Jetpack\Publicize\Abilities\Social_Settings_Abilities;
12use Automattic\Jetpack\Status;
13use Automattic\Jetpack\Status\Host;
14
15/**
16 * The class to configure and initialize the publicize package.
17 */
18class Publicize_Setup {
19
20    /**
21     * Whether to update the plan information from WPCOM when initialising the package.
22     *
23     * @var bool
24     */
25    public static $refresh_plan_info = false;
26
27    /**
28     * Whether the class has been initialized.
29     *
30     * @var bool
31     */
32    public static $initialized = false;
33
34    /**
35     * To configure the publicize package, when called via the Config package.
36     */
37    public static function configure() {
38        add_action( 'jetpack_feature_publicize_enabled', array( __CLASS__, 'on_jetpack_feature_publicize_enabled' ) );
39    }
40
41    /**
42     * Whether to load the Publicize module.
43     *
44     * @return bool
45     */
46    private static function should_load() {
47
48        /**
49         * We do not want to load Publicize on WPCOM private sites.
50         */
51        $is_wpcom_platform_private_site = ( new Host() )->is_wpcom_platform() && ( new Status() )->is_private_site();
52
53        $should_load = ! $is_wpcom_platform_private_site;
54
55        /**
56         * Filters the flag to decide whether to load the Publicize module.
57         *
58         * @since 0.64.0
59         *
60         * @param bool $should_load Whether to load the Publicize module.
61         */
62        return (bool) apply_filters( 'jetpack_publicize_should_load', $should_load );
63    }
64
65    /**
66     * Initialization of publicize logic that should always be loaded,
67     * regardless of whether Publicize is enabled or not.
68     *
69     * You should justify everyting that is done here, as it will be loaded on every pageload.
70     */
71    public static function pre_initialization() {
72        if ( ! self::should_load() ) {
73            return;
74        }
75
76        $is_wpcom_simple = ( new Host() )->is_wpcom_simple();
77
78        /**
79         * Assets are to be loaded in all cases.
80         *
81         * To allow loading of admin page and
82         * the editor placeholder when publicize is OFF.
83         */
84        Publicize_Assets::configure();
85
86        /**
87         * Social admin page is to be always registered.
88         */
89        Social_Admin_Page::init();
90
91        /**
92         * Register the Jetpack Social settings abilities (WordPress Abilities
93         * API). Always wired — regardless of whether Publicize is enabled —
94         * so the `jetpack-social/*` abilities are available in both the
95         * Jetpack plugin and the standalone Social plugin. Registration is
96         * still gated behind the `jetpack_wp_abilities_enabled` filter inside
97         * `Registrar::init()`.
98         */
99        Social_Settings_Abilities::init();
100
101        if ( ! $is_wpcom_simple ) {
102            /**
103             * We need this only on Jetpack sites for Google Site auto-verification.
104             */
105            add_action( 'init', array( Keyring_Helper::class, 'init' ), 9 );
106        }
107
108        if ( $is_wpcom_simple ) {
109            /**
110             * Publicize is always enabled on WPCOM,
111             * we can call the initialization method directly.
112             */
113            add_action( 'plugins_loaded', array( self::class, 'on_jetpack_feature_publicize_enabled' ) );
114        }
115    }
116
117    /**
118     * To configure the publicize package, when called via the Config package.
119     */
120    public static function on_jetpack_feature_publicize_enabled() {
121        if ( self::$initialized || ! self::should_load() ) {
122            return;
123        }
124
125        self::$initialized = true;
126
127        $is_wpcom_simple = ( new Host() )->is_wpcom_simple();
128
129        global $publicize;
130        /**
131         * If publicize is not initialzed on WPCOM,
132         * it means that we are either on a public facing page
133         * or a page where Publicize is not needed.
134         * So, we will skip the whole set up here.
135         */
136        if ( $is_wpcom_simple && ! $publicize ) {
137            return;
138        }
139
140        global $publicize_ui;
141
142        if ( ! isset( $publicize_ui ) ) {
143            $publicize_ui = new Publicize_UI();
144        }
145
146        $rest_controllers = array(
147            REST_API\Connections_Controller::class,
148            REST_API\Connections_Post_Field::class,
149            REST_API\Scheduled_Actions_Controller::class,
150            REST_API\Services_Controller::class,
151            REST_API\Share_Post_Controller::class,
152            REST_API\Share_Status_Controller::class,
153            REST_API\X_Usage_Controller::class,
154            REST_API\Social_Image_Generator_Controller::class,
155            REST_API\Render_Messages_Controller::class,
156            REST_API\Message_Templates_Placeholders_Controller::class,
157            Jetpack_Social_Settings\Settings::class,
158        );
159
160        // Load the REST controllers.
161        foreach ( $rest_controllers as $controller ) {
162            if ( $is_wpcom_simple ) {
163                wpcom_rest_api_v2_load_plugin( $controller );
164            } else {
165                new $controller();
166            }
167        }
168
169        add_action( 'current_screen', array( self::class, 'add_filters_and_actions_for_screen' ), 5 );
170
171        ( new Social_Image_Generator\Setup() )->init();
172
173        // Things that should not happen on WPCOM.
174        if ( ! $is_wpcom_simple ) {
175            add_action( 'rest_api_init', array( new REST_Controller(), 'register_rest_routes' ) );
176        }
177    }
178
179    /**
180     * If the current_screen has 'edit' as the base, add filter to change the post list tables.
181     *
182     * @param object $current_screen The current screen.
183     */
184    public static function add_filters_and_actions_for_screen( $current_screen ) {
185        if ( 'edit' !== $current_screen->base ) {
186            return;
187        }
188
189        /**
190         * Filter to enable/disable the Share action on the post list screen.
191         *
192         * The Share action allows users to reshare published posts via Jetpack Social.
193         * It is automatically enabled for plans that support the 'republicize' feature,
194         * but can be disabled via this filter.
195         *
196         * @since 0.2.0 Originally in jetpack-post-list package.
197         * @since $$NEXT_VERSION$$ Moved to jetpack-publicize package.
198         *
199         * @param bool   $show_share Whether to show the share action. Default true.
200         * @param string $post_type  The current post type.
201         */
202        $show_share_action = Current_Plan::supports( 'republicize' )
203            && apply_filters( 'jetpack_post_list_display_share_action', true, $current_screen->post_type );
204
205        if ( $show_share_action ) {
206            self::maybe_add_share_action( $current_screen->post_type );
207        }
208    }
209
210    /**
211     * Add the Share action for post types that support publicize.
212     *
213     * @param string $post_type The post type.
214     */
215    public static function maybe_add_share_action( $post_type ) {
216        if (
217            post_type_supports( $post_type, 'publicize' ) &&
218            use_block_editor_for_post_type( $post_type )
219        ) {
220            add_filter( 'post_row_actions', array( self::class, 'add_share_action' ), 20, 2 );
221            add_filter( 'page_row_actions', array( self::class, 'add_share_action' ), 20, 2 );
222        }
223    }
224
225    /**
226     * Add the Share action link to the post row actions.
227     *
228     * @param array    $post_actions The current post actions.
229     * @param \WP_Post $post The post object.
230     * @return array Modified post actions.
231     */
232    public static function add_share_action( $post_actions, $post ) {
233        $edit_url = get_edit_post_link( $post->ID, 'raw' );
234        if ( ! $edit_url || 'publish' !== $post->post_status ) {
235            return $post_actions;
236        }
237
238        $url   = add_query_arg( 'jetpack-editor-action', 'share_post', $edit_url );
239        $text  = _x( 'Share', 'Share the post on social networks', 'jetpack-publicize-pkg' );
240        $title = _draft_or_post_title( $post );
241        /* translators: post title */
242        $label                 = sprintf( __( 'Share "%s" via Jetpack Social', 'jetpack-publicize-pkg' ), $title );
243        $post_actions['share'] = sprintf( '<a href="%s" aria-label="%s">%s</a>', esc_url( $url ), esc_attr( $label ), esc_html( $text ) );
244
245        return $post_actions;
246    }
247
248    /**
249     * Retrieves the blog ID based on the environment we're running in.
250     *
251     * @return int The WPCOM blog ID.
252     */
253    public static function get_blog_id() {
254        return defined( 'IS_WPCOM' ) && IS_WPCOM ? get_current_blog_id() : \Jetpack_Options::get_option( 'id' );
255    }
256}