Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 6
CRAP
0.00% covered (danger)
0.00%
0 / 1
Social_Admin_Page
0.00% covered (danger)
0.00%
0 / 35
0.00% covered (danger)
0.00%
0 / 6
156
0.00% covered (danger)
0.00%
0 / 1
 init
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 __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
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 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\Publicize\Publicize_Utils as Utils;
14use Automattic\Jetpack\Status\Host;
15
16/**
17 * The class to handle the Social Admin Page.
18 */
19class Social_Admin_Page {
20
21    /**
22     * The instance of the class.
23     *
24     * @var Social_Admin_Page
25     */
26    private static $instance;
27
28    /**
29     * Initialize the class.
30     *
31     * @return Social_Admin_Page
32     */
33    public static function init() {
34        if ( ! isset( self::$instance ) ) {
35            self::$instance = new self();
36        }
37
38        return self::$instance;
39    }
40
41    /**
42     * The constructor.
43     */
44    private function __construct() {
45        add_action( 'admin_menu', array( $this, 'add_menu' ) );
46    }
47
48    /**
49     * Add the admin menu.
50     */
51    public function add_menu() {
52
53        // Remove the old Social menu item, if it exists.
54        Admin_Menu::remove_menu( 'jetpack-social' );
55
56        // If this isn't an admin (or someone with the capability to change the module status )
57        // and Publicize is inactive, then don't render the admin page.
58        if ( ! current_user_can( 'manage_options' ) && ! Utils::is_publicize_active() ) {
59            return;
60        }
61
62        // We don't need Jetpack connection on WP.com.
63        $needs_site_connection = ! ( new Host() )->is_wpcom_platform() && ! ( new Connection_Manager() )->is_connected();
64
65        /**
66         * If the Jetpack Social plugin is not active,
67         * we want to hide the menu if the site is not connected.
68         */
69        if ( ! defined( 'JETPACK_SOCIAL_PLUGIN_DIR' ) && $needs_site_connection ) {
70            return;
71        }
72
73        $page_suffix = Admin_Menu::add_menu(
74            /** "Jetpack Social" is a product name, do not translate. */
75            'Jetpack Social',
76            'Social',
77            'publish_posts',
78            'jetpack-social',
79            array( $this, 'render' ),
80            4
81        );
82
83        add_action( 'load-' . $page_suffix, array( $this, 'admin_init' ) );
84    }
85
86    /**
87     * Initialize the admin resources.
88     */
89    public function admin_init() {
90        /**
91         * Use priority 20 to ensure that we can dequeue the old Social assets.
92         */
93        add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ), 20 );
94
95        // Initialize the media library for the social image generator.
96        wp_enqueue_media();
97    }
98
99    /**
100     * Render the admin page.
101     */
102    public function render() {
103        ?>
104            <div id="jetpack-social-root"></div>
105        <?php
106    }
107
108    /**
109     * Enqueue admin scripts and styles.
110     */
111    public function enqueue_admin_scripts() {
112
113        // Dequeue the old Social assets.
114        wp_dequeue_script( 'jetpack-social' );
115        wp_dequeue_style( 'jetpack-social' );
116
117        Assets::register_script(
118            'social-admin-page',
119            '../build/social-admin-page.js',
120            __FILE__,
121            array(
122                'in_footer'  => true,
123                'textdomain' => 'jetpack-publicize-pkg',
124                'enqueue'    => true,
125            )
126        );
127    }
128}