Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
15.09% covered (danger)
15.09%
8 / 53
0.00% covered (danger)
0.00%
0 / 10
CRAP
0.00% covered (danger)
0.00%
0 / 1
Social_Admin_Page
15.09% covered (danger)
15.09%
8 / 53
0.00% covered (danger)
0.00%
0 / 10
439.77
0.00% covered (danger)
0.00%
0 / 1
 init
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
2.15
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 maybe_load_wp_build
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 add_menu
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
42
 admin_init
83.33% covered (warning)
83.33%
5 / 6
0.00% covered (danger)
0.00%
0 / 1
3.04
 render
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 enqueue_admin_scripts
50.00% covered (danger)
50.00%
1 / 2
0.00% covered (danger)
0.00%
0 / 1
2.50
 load_wp_build
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
12
 alias_screen_id_for_wp_build
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 is_social_admin_request
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
12
1<?php
2/**
3 * Social Admin Page class.
4 *
5 * @package automattic/jetpack-publicize
6 */
7
8namespace Automattic\Jetpack\Publicize;
9
10use Automattic\Jetpack\Admin_UI\Admin_Menu;
11use Automattic\Jetpack\Connection\Initial_State as Connection_Initial_State;
12use Automattic\Jetpack\Connection\Manager as Connection_Manager;
13use Automattic\Jetpack\Current_Plan;
14use Automattic\Jetpack\Publicize\Publicize_Utils as Utils;
15use Automattic\Jetpack\Status\Host;
16
17/**
18 * The class to handle the Social Admin Page.
19 */
20class Social_Admin_Page {
21
22    /**
23     * Nonce action used when refreshing plan data.
24     */
25    public const REFRESH_PLAN_NONCE_ACTION = 'jetpack_social_refresh_plan_data';
26
27    /**
28     * The instance of the class.
29     *
30     * @var Social_Admin_Page
31     */
32    private static $instance;
33
34    /**
35     * Initialize the class.
36     *
37     * @return Social_Admin_Page
38     */
39    public static function init() {
40        if ( ! isset( self::$instance ) ) {
41            self::$instance = new self();
42        }
43
44        return self::$instance;
45    }
46
47    /**
48     * The constructor.
49     */
50    private function __construct() {
51        // Load the wp-build dashboard at admin_menu priority 1 so its render
52        // function is defined before `add_menu` (priority 10) registers the page.
53        add_action( 'admin_menu', array( __CLASS__, 'maybe_load_wp_build' ), 1 );
54        add_action( 'admin_menu', array( $this, 'add_menu' ) );
55    }
56
57    /**
58     * Load the wp-build dashboard on the Social admin request.
59     *
60     * Hooked to `admin_menu` priority 1 so the wp-build render function and
61     * enqueue hook are in place before `add_menu()` runs at the default priority.
62     *
63     * @return void
64     */
65    public static function maybe_load_wp_build() {
66        if ( ! self::is_social_admin_request() ) {
67            return;
68        }
69
70        self::load_wp_build();
71        add_action( 'current_screen', array( __CLASS__, 'alias_screen_id_for_wp_build' ) );
72    }
73
74    /**
75     * Add the admin menu.
76     */
77    public function add_menu() {
78
79        // Remove the old Social menu item, if it exists.
80        Admin_Menu::remove_menu( 'jetpack-social' );
81
82        // If this isn't an admin (or someone with the capability to change the module status )
83        // and Publicize is inactive, then don't render the admin page.
84        if ( ! current_user_can( 'manage_options' ) && ! Utils::is_publicize_active() ) {
85            return;
86        }
87
88        // We don't need Jetpack connection on WP.com.
89        $needs_site_connection = ! ( new Host() )->is_wpcom_platform() && ! ( new Connection_Manager() )->is_connected();
90
91        /**
92         * If the Jetpack Social plugin is not active,
93         * we want to hide the menu if the site is not connected.
94         */
95        if ( ! defined( 'JETPACK_SOCIAL_PLUGIN_DIR' ) && $needs_site_connection ) {
96            return;
97        }
98
99        $page_suffix = Admin_Menu::add_menu(
100            /** "Jetpack Social" is a product name, do not translate. */
101            'Jetpack Social',
102            'Social',
103            'publish_posts',
104            'jetpack-social',
105            array( $this, 'render' ),
106            4
107        );
108
109        add_action( 'load-' . $page_suffix, array( $this, 'admin_init' ) );
110    }
111
112    /**
113     * Initialize the admin resources.
114     */
115    public function admin_init() {
116        // Refresh data if coming from purchase to ensure it is up to date
117        // without making API calls on every admin page load.
118        if ( isset( $_GET['refresh_plan_data'] ) ) {
119            check_admin_referer( self::REFRESH_PLAN_NONCE_ACTION );
120            if ( apply_filters( 'jetpack_social_should_refresh_plan_data', true ) ) {
121                Current_Plan::refresh_from_wpcom();
122            }
123        }
124
125        /**
126         * Use priority 20 to ensure that we can dequeue the old Social assets.
127         */
128        add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ), 20 );
129
130        // Initialize the media library for the social image generator.
131        wp_enqueue_media();
132    }
133
134    /**
135     * Render the admin page by delegating to the wp-build dashboard.
136     *
137     * `maybe_load_wp_build()` defines the render function before this fires;
138     * render nothing on an unbuilt dev checkout rather than fatal.
139     */
140    public function render() {
141        if ( function_exists( 'jetpack_social_jetpack_social_dashboard_wp_admin_render_page' ) ) {
142            // Generated by wp-build (build/pages/jetpack-social-dashboard/page-wp-admin.php), so phan can't see it.
143            // @phan-suppress-next-line PhanUndeclaredFunction
144            jetpack_social_jetpack_social_dashboard_wp_admin_render_page();
145        }
146    }
147
148    /**
149     * Enqueue admin scripts and styles.
150     */
151    public function enqueue_admin_scripts() {
152        /*
153         * wp-build owns its own enqueue pipeline. The chassis reads connection
154         * state via `useConnection()`, which has no REST resolver, so hydrate it
155         * inline onto the prerequisites script that loads before the chassis module.
156         */
157        if ( wp_script_is( 'jetpack-social-dashboard-wp-admin-prerequisites', 'registered' ) ) {
158            Connection_Initial_State::render_script( 'jetpack-social-dashboard-wp-admin-prerequisites' );
159        }
160    }
161
162    /**
163     * Load the wp-build entry file and register its polyfills.
164     *
165     * Only called on `?page=jetpack-social` admin requests. Keeps wp-build off
166     * every other request.
167     *
168     * @return void
169     */
170    private static function load_wp_build() {
171        $build_index = dirname( __DIR__ ) . '/build/build.php';
172
173        if ( ! file_exists( $build_index ) ) {
174            return;
175        }
176
177        require_once $build_index;
178
179        // The wp-build dashboard (unlike the Social bundles) uses the full polyfill set:
180        // the @wordpress/boot|route|a11y modules, wp-notices, wp-views, etc.
181        if ( ! class_exists( '\Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Polyfills' ) ) {
182            return;
183        }
184
185        \Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Polyfills::register(
186            'jetpack-social',
187            array_merge(
188                \Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Polyfills::SCRIPT_HANDLES,
189                \Automattic\Jetpack\WP_Build_Polyfills\WP_Build_Polyfills::MODULE_IDS
190            )
191        );
192    }
193
194    /**
195     * Alias the current screen ID to satisfy wp-build's auto-generated enqueue check.
196     *
197     * The wp-build `<page>-wp-admin` enqueue callback fires only when the screen ID
198     * matches the wp-build page slug (`jetpack-social-dashboard`). Our wp-admin menu
199     * slug stays `jetpack-social`, so we mutate the screen object in place to make
200     * the check pass without changing the user-facing URL.
201     *
202     * Hooked only when we're on the Social admin page, so this never affects any
203     * other request.
204     *
205     * @param \WP_Screen|null $screen The current screen object (passed by WP).
206     * @return void
207     */
208    public static function alias_screen_id_for_wp_build( $screen ) {
209        if ( ! is_object( $screen ) ) {
210            return;
211        }
212
213        $screen->id = 'jetpack-social-dashboard';
214    }
215
216    /**
217     * Returns true when the current request targets the Social admin page.
218     *
219     * Used to scope wp-build loading to the one page that needs it. The
220     * `$_GET['page']` value is populated by wp-admin/admin.php before any of
221     * our hooks fire, so this check is reliable from the constructor onwards.
222     *
223     * @return bool
224     */
225    private static function is_social_admin_request() {
226        if ( ! is_admin() || ! isset( $_GET['page'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
227            return false;
228        }
229
230        return sanitize_text_field( wp_unslash( $_GET['page'] ) ) === 'jetpack-social'; // phpcs:ignore WordPress.Security.NonceVerification.Recommended
231    }
232}