Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
17.95% covered (danger)
17.95%
7 / 39
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
Social_Admin_Page
17.95% covered (danger)
17.95%
7 / 39
0.00% covered (danger)
0.00%
0 / 6
122.27
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 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 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
2
 enqueue_admin_scripts
0.00% covered (danger)
0.00%
0 / 12
0.00% covered (danger)
0.00%
0 / 1
2
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\Assets;
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        add_action( 'admin_menu', array( $this, 'add_menu' ) );
52    }
53
54    /**
55     * Add the admin menu.
56     */
57    public function add_menu() {
58
59        // Remove the old Social menu item, if it exists.
60        Admin_Menu::remove_menu( 'jetpack-social' );
61
62        // If this isn't an admin (or someone with the capability to change the module status )
63        // and Publicize is inactive, then don't render the admin page.
64        if ( ! current_user_can( 'manage_options' ) && ! Utils::is_publicize_active() ) {
65            return;
66        }
67
68        // We don't need Jetpack connection on WP.com.
69        $needs_site_connection = ! ( new Host() )->is_wpcom_platform() && ! ( new Connection_Manager() )->is_connected();
70
71        /**
72         * If the Jetpack Social plugin is not active,
73         * we want to hide the menu if the site is not connected.
74         */
75        if ( ! defined( 'JETPACK_SOCIAL_PLUGIN_DIR' ) && $needs_site_connection ) {
76            return;
77        }
78
79        $page_suffix = Admin_Menu::add_menu(
80            /** "Jetpack Social" is a product name, do not translate. */
81            'Jetpack Social',
82            'Social',
83            'publish_posts',
84            'jetpack-social',
85            array( $this, 'render' ),
86            4
87        );
88
89        add_action( 'load-' . $page_suffix, array( $this, 'admin_init' ) );
90    }
91
92    /**
93     * Initialize the admin resources.
94     */
95    public function admin_init() {
96        // Refresh data if coming from purchase to ensure it is up to date
97        // without making API calls on every admin page load.
98        if ( isset( $_GET['refresh_plan_data'] ) ) {
99            check_admin_referer( self::REFRESH_PLAN_NONCE_ACTION );
100            if ( apply_filters( 'jetpack_social_should_refresh_plan_data', true ) ) {
101                Current_Plan::refresh_from_wpcom();
102            }
103        }
104
105        /**
106         * Use priority 20 to ensure that we can dequeue the old Social assets.
107         */
108        add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ), 20 );
109
110        // Initialize the media library for the social image generator.
111        wp_enqueue_media();
112    }
113
114    /**
115     * Render the admin page.
116     */
117    public function render() {
118        ?>
119            <div id="jetpack-social-root"></div>
120        <?php
121    }
122
123    /**
124     * Enqueue admin scripts and styles.
125     */
126    public function enqueue_admin_scripts() {
127
128        // Dequeue the old Social assets.
129        wp_dequeue_script( 'jetpack-social' );
130        wp_dequeue_style( 'jetpack-social' );
131
132        Assets::register_script(
133            'social-admin-page',
134            '../build/social-admin-page.js',
135            __FILE__,
136            array(
137                'in_footer'  => true,
138                'textdomain' => 'jetpack-publicize-pkg',
139                'enqueue'    => true,
140            )
141        );
142    }
143}