Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
19.70% covered (danger)
19.70%
13 / 66
25.00% covered (danger)
25.00%
2 / 8
CRAP
0.00% covered (danger)
0.00%
0 / 1
Publicize_Setup
19.70% covered (danger)
19.70%
13 / 66
25.00% covered (danger)
25.00%
2 / 8
496.06
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 / 13
0.00% covered (danger)
0.00%
0 / 1
30
 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\Socialspec\Socialspec;
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         * The connect popup (auth_flow=v2) is redirected back to a same-origin admin-post
93         * endpoint that broadcasts the result to the opener, so it must always be available.
94         */
95        Keyring_Result_Handler::init();
96
97        /**
98         * The SocialSpec documents are public and do not depend on Publicize being enabled.
99         */
100        if ( class_exists( Socialspec::class ) ) {
101            Socialspec::init();
102        }
103
104        if ( ! $is_wpcom_simple ) {
105            /**
106             * We need this only on Jetpack sites for Google Site auto-verification.
107             */
108            add_action( 'init', array( Keyring_Helper::class, 'init' ), 9 );
109
110            // Not with the other controllers: the Social dashboard also loads while Publicize is off. Simple always has it on.
111            new REST_API\Script_Data_Controller();
112        }
113
114        if ( $is_wpcom_simple ) {
115            /**
116             * Publicize is always enabled on WPCOM,
117             * we can call the initialization method directly.
118             */
119            add_action( 'plugins_loaded', array( self::class, 'on_jetpack_feature_publicize_enabled' ) );
120        }
121    }
122
123    /**
124     * To configure the publicize package, when called via the Config package.
125     */
126    public static function on_jetpack_feature_publicize_enabled() {
127        if ( self::$initialized || ! self::should_load() ) {
128            return;
129        }
130
131        self::$initialized = true;
132
133        $is_wpcom_simple = ( new Host() )->is_wpcom_simple();
134
135        global $publicize;
136        /**
137         * If publicize is not initialzed on WPCOM,
138         * it means that we are either on a public facing page
139         * or a page where Publicize is not needed.
140         * So, we will skip the whole set up here.
141         */
142        if ( $is_wpcom_simple && ! $publicize ) {
143            return;
144        }
145
146        global $publicize_ui;
147
148        if ( ! isset( $publicize_ui ) ) {
149            $publicize_ui = new Publicize_UI();
150        }
151
152        $rest_controllers = array(
153            REST_API\Connections_Controller::class,
154            REST_API\Connections_Post_Field::class,
155            REST_API\Keyring_Result_Controller::class,
156            REST_API\Scheduled_Actions_Controller::class,
157            REST_API\Services_Controller::class,
158            REST_API\Share_Post_Controller::class,
159            REST_API\Share_Status_Controller::class,
160            REST_API\Social_Image_Generator_Controller::class,
161            REST_API\Render_Messages_Controller::class,
162            REST_API\Message_Templates_Placeholders_Controller::class,
163            Jetpack_Social_Settings\Settings::class,
164        );
165
166        if ( $is_wpcom_simple ) {
167            $rest_controllers[] = REST_API\Script_Data_Controller::class;
168        }
169
170        // Load the REST controllers.
171        foreach ( $rest_controllers as $controller ) {
172            if ( $is_wpcom_simple ) {
173                wpcom_rest_api_v2_load_plugin( $controller );
174            } else {
175                new $controller();
176            }
177        }
178
179        add_action( 'current_screen', array( self::class, 'add_filters_and_actions_for_screen' ), 5 );
180
181        ( new Social_Image_Generator\Setup() )->init();
182    }
183
184    /**
185     * If the current_screen has 'edit' as the base, add filter to change the post list tables.
186     *
187     * @param object $current_screen The current screen.
188     */
189    public static function add_filters_and_actions_for_screen( $current_screen ) {
190        if ( 'edit' !== $current_screen->base ) {
191            return;
192        }
193
194        /**
195         * Filter to enable/disable the Share action on the post list screen.
196         *
197         * The Share action allows users to reshare published posts via Jetpack Social.
198         * It is automatically enabled for plans that support the 'republicize' feature,
199         * but can be disabled via this filter.
200         *
201         * @since 0.2.0 Originally in jetpack-post-list package.
202         * @since $$NEXT_VERSION$$ Moved to jetpack-publicize package.
203         *
204         * @param bool   $show_share Whether to show the share action. Default true.
205         * @param string $post_type  The current post type.
206         */
207        $show_share_action = Current_Plan::supports( 'republicize' )
208            && apply_filters( 'jetpack_post_list_display_share_action', true, $current_screen->post_type );
209
210        if ( $show_share_action ) {
211            self::maybe_add_share_action( $current_screen->post_type );
212        }
213    }
214
215    /**
216     * Add the Share action for post types that support publicize.
217     *
218     * @param string $post_type The post type.
219     */
220    public static function maybe_add_share_action( $post_type ) {
221        if (
222            post_type_supports( $post_type, 'publicize' ) &&
223            use_block_editor_for_post_type( $post_type )
224        ) {
225            add_filter( 'post_row_actions', array( self::class, 'add_share_action' ), 20, 2 );
226            add_filter( 'page_row_actions', array( self::class, 'add_share_action' ), 20, 2 );
227        }
228    }
229
230    /**
231     * Add the Share action link to the post row actions.
232     *
233     * @param array    $post_actions The current post actions.
234     * @param \WP_Post $post The post object.
235     * @return array Modified post actions.
236     */
237    public static function add_share_action( $post_actions, $post ) {
238        $edit_url = get_edit_post_link( $post->ID, 'raw' );
239        if ( ! $edit_url || 'publish' !== $post->post_status ) {
240            return $post_actions;
241        }
242
243        $url   = add_query_arg( 'jetpack-editor-action', 'share_post', $edit_url );
244        $text  = _x( 'Share', 'Share the post on social networks', 'jetpack-publicize-pkg' );
245        $title = _draft_or_post_title( $post );
246        /* translators: post title */
247        $label                 = sprintf( __( 'Share "%s" via Jetpack Social', 'jetpack-publicize-pkg' ), $title );
248        $post_actions['share'] = sprintf( '<a href="%s" aria-label="%s">%s</a>', esc_url( $url ), esc_attr( $label ), esc_html( $text ) );
249
250        return $post_actions;
251    }
252
253    /**
254     * Retrieves the blog ID based on the environment we're running in.
255     *
256     * @return int The WPCOM blog ID.
257     */
258    public static function get_blog_id() {
259        return defined( 'IS_WPCOM' ) && IS_WPCOM ? get_current_blog_id() : \Jetpack_Options::get_option( 'id' );
260    }
261}