| 1 |
<?php |
| 2 |
/** |
| 3 |
* The admin-specific functionality of the plugin |
| 4 |
* |
| 5 |
* @package civist |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Configures Civist menus, pages and scripts in WordPress administration. |
| 10 |
*/ |
| 11 |
class Civist_Admin { |
| 12 |
/** |
| 13 |
* The plugin name. |
| 14 |
* |
| 15 |
* @var string |
| 16 |
*/ |
| 17 |
private $plugin_name; |
| 18 |
/** |
| 19 |
* The plugin file. |
| 20 |
* |
| 21 |
* @var string |
| 22 |
*/ |
| 23 |
private $plugin_file; |
| 24 |
/** |
| 25 |
* The plugin slug. |
| 26 |
* |
| 27 |
* @var string |
| 28 |
*/ |
| 29 |
private $plugin_slug; |
| 30 |
/** |
| 31 |
* The id of the media button in the post/page editor. |
| 32 |
* |
| 33 |
* @var string |
| 34 |
*/ |
| 35 |
private $editor_button_id; |
| 36 |
/** |
| 37 |
* The id of the reconnect link in the plugins page. |
| 38 |
* |
| 39 |
* @var string |
| 40 |
*/ |
| 41 |
private $reconnect_link_id; |
| 42 |
/** |
| 43 |
* The plugin settings manager. |
| 44 |
* |
| 45 |
* @var Civist_Settings_Manager |
| 46 |
*/ |
| 47 |
private $settings_manager; |
| 48 |
/** |
| 49 |
* The plugin settings UI. |
| 50 |
* |
| 51 |
* @var Civist_Settings |
| 52 |
*/ |
| 53 |
private $settings; |
| 54 |
/** |
| 55 |
* The plugin scripts. |
| 56 |
* |
| 57 |
* @var Civist_Scripts |
| 58 |
*/ |
| 59 |
private $scripts; |
| 60 |
/** |
| 61 |
* The plugin editor functionality. |
| 62 |
* |
| 63 |
* @var Civist_Editor |
| 64 |
*/ |
| 65 |
private $editor; |
| 66 |
/** |
| 67 |
* The plugin connected flag. |
| 68 |
* |
| 69 |
* @var bool |
| 70 |
*/ |
| 71 |
private $is_plugin_connected; |
| 72 |
|
| 73 |
/** |
| 74 |
* The Civist_Admin class constructor |
| 75 |
* |
| 76 |
* @param string $plugin_name The name of the plugin. |
| 77 |
* @param string $plugin_file The file of the plugin. |
| 78 |
* @param string $plugin_slug The slug of the plugin. |
| 79 |
* @param Civist_Settings_Manager $settings_manager The plugin settings manager. |
| 80 |
* @param Civist_Settings $settings The plugin settings UI. |
| 81 |
* @param Civist_Scripts $scripts The plugin scripts. |
| 82 |
* @param Civist_Editor $editor The plugin editor functionality. |
| 83 |
*/ |
| 84 |
public function __construct( $plugin_name, $plugin_file, $plugin_slug, Civist_Settings_Manager $settings_manager, Civist_Settings $settings, Civist_Scripts $scripts, Civist_Editor $editor ) { |
| 85 |
$this->plugin_name = $plugin_name; |
| 86 |
$this->plugin_file = $plugin_file; |
| 87 |
$this->plugin_slug = $plugin_slug; |
| 88 |
$this->reconnect_link_id = $plugin_slug . '_reconnect_kink'; |
| 89 |
$this->settings_manager = $settings_manager; |
| 90 |
$this->settings = $settings; |
| 91 |
$this->scripts = $scripts; |
| 92 |
$this->editor = $editor; |
| 93 |
$this->is_plugin_connected = $this->settings_manager->is_connected(); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Build the plugin's menu entries. |
| 98 |
*/ |
| 99 |
public function add_admin_menu() { |
| 100 |
if ( current_user_can( 'edit_pages' ) || current_user_can( 'manage_options' ) ) { |
| 101 |
global $submenu; |
| 102 |
$plugin_page_url = admin_url( 'admin.php' ) . '?page=' . $this->plugin_slug; |
| 103 |
$manager_cap = $this->is_plugin_connected ? 'edit_pages' : 'manage_options'; |
| 104 |
add_menu_page( $this->plugin_name, $this->plugin_name, $manager_cap, $this->plugin_slug, array( $this, 'render_manager' ), 'dashicons-civist_symbol_wp', 22 ); |
| 105 |
if ( ! $this->is_plugin_connected ) { |
| 106 |
$submenu['options-general.php'][] = array( $this->plugin_name, 'manage_options', $plugin_page_url ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
| 107 |
} else { |
| 108 |
/* translators: The menu link to show the plugin dashboard page. */ |
| 109 |
$submenu[ $this->plugin_slug ][] = array( _x( 'Dashboard', 'wp.plugin.manager.menu.dashboard', 'civist' ), 'edit_pages', $plugin_page_url . '#/' ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
| 110 |
/* translators: The menu link to show the plugin all petitions page. */ |
| 111 |
$submenu[ $this->plugin_slug ][] = array( _x( 'Petitions', 'wp.plugin.manager.menu.all_petitions', 'civist' ), 'edit_pages', $plugin_page_url . '#/petitions' ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
| 112 |
/* translators: The menu link to show the plugin all donation forms page. */ |
| 113 |
$submenu[ $this->plugin_slug ][] = array( _x( 'Donation Forms', 'wp.plugin.manager.menu.all_donation_forms', 'civist' ), 'edit_pages', $plugin_page_url . '#/donation_forms' ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
| 114 |
/* translators: The menu link to show the plugin all supporters page. */ |
| 115 |
$submenu[ $this->plugin_slug ][] = array( _x( 'Supporters', 'wp.plugin.manager.menu.all_supporters', 'civist' ), 'edit_pages', $plugin_page_url . '#/supporters' ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
| 116 |
/* translators: The menu link to show the settings page. */ |
| 117 |
$submenu[ $this->plugin_slug ][] = array( _x( 'Settings', 'wp.plugin.manager.menu.settings', 'civist' ), 'manage_options', admin_url( 'options-general.php' ) . '?page=' . $this->plugin_slug . '-settings' ); // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited |
| 118 |
add_options_page( $this->plugin_slug, $this->plugin_name, 'manage_options', $this->plugin_slug . '-settings', array( $this->settings, 'options_page' ) ); |
| 119 |
} |
| 120 |
// TODO: Remove me when advanced settings are no longer necessary. |
| 121 |
add_submenu_page( '', $this->plugin_name, $this->plugin_name, 'manage_options', $this->plugin_slug . '-advanced-settings', array( $this->settings, 'advanced_options_page' ) ); |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
/** |
| 126 |
* Render the plugin manager page. |
| 127 |
*/ |
| 128 |
public function render_manager() { |
| 129 |
if ( ! current_user_can( 'edit_pages' ) ) { |
| 130 |
wp_die( "You don't have sufficient permissions to access this page." ); |
| 131 |
} |
| 132 |
require_once 'civist-container.php'; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* Sets async attr in script. |
| 137 |
* |
| 138 |
* @param string $url The url of the script. |
| 139 |
*/ |
| 140 |
public function add_async( $url ) { |
| 141 |
if ( strpos( $url, '#asyncload' ) === false ) { |
| 142 |
return $url; |
| 143 |
} |
| 144 |
return str_replace( '#asyncload', '', $url ) . ' async'; |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Register and enqueue plugin's scripts and stylesheets. |
| 149 |
* |
| 150 |
* @param string $hook_suffix The current admin page. |
| 151 |
*/ |
| 152 |
public function enqueue_scripts( $hook_suffix ) { |
| 153 |
include 'civist-scripts.php'; // exposes $webpack-files variable. |
| 154 |
|
| 155 |
$this->scripts->enqueue_civist_icon_font( $civist_webpack_files ); |
| 156 |
// advanced settings page. |
| 157 |
if ( strpos( $hook_suffix, $this->plugin_slug . '-advanced-settings' ) !== false ) { |
| 158 |
return; |
| 159 |
} |
| 160 |
|
| 161 |
if ( ! $this->is_plugin_connected ) { |
| 162 |
if ( strpos( $hook_suffix, $this->plugin_slug ) !== false ) { |
| 163 |
// registration page. |
| 164 |
$this->scripts->enqueue_registration_scripts( $civist_webpack_files ); |
| 165 |
} |
| 166 |
} elseif ( strpos( $hook_suffix, 'plugins' ) !== false ) { |
| 167 |
// plugins page. |
| 168 |
$this->scripts->enqueue_plugins_scripts( $civist_webpack_files, $this->reconnect_link_id ); |
| 169 |
|
| 170 |
} elseif ( strpos( $hook_suffix, $this->plugin_slug ) !== false && strpos( $hook_suffix, 'settings' ) === false ) { |
| 171 |
// manager page. |
| 172 |
$this->scripts->enqueue_manager_scripts( $civist_webpack_files ); |
| 173 |
|
| 174 |
} elseif ( strpos( $hook_suffix, $this->plugin_slug . '-settings' ) !== false ) { |
| 175 |
// settings page. |
| 176 |
$this->scripts->enqueue_settings_scripts( $civist_webpack_files ); |
| 177 |
} elseif ( strpos( $hook_suffix, 'post' ) !== false ) { |
| 178 |
// post/page editor page. |
| 179 |
$this->editor->enqueue_editor_scripts(); |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
/** |
| 185 |
* Renders the plugin reconnect link in the plugin entry . |
| 186 |
* |
| 187 |
* @param string $links The links array. |
| 188 |
* @return array The links modified by the function. |
| 189 |
*/ |
| 190 |
public function add_reconnect_link( $links ) { |
| 191 |
if ( ! $this->is_plugin_connected ) { |
| 192 |
return $links; |
| 193 |
} |
| 194 |
$new_links = array( |
| 195 |
/* translators: The link to reconect Civist in the WordPress admin plugins page under the Civist plugin entry. */ |
| 196 |
'<a href="#/" id="' . esc_html( $this->reconnect_link_id ) . '">' . _x( 'Reconnect', 'wp.plugin.plugins.link.reconnect', 'civist' ) . '</a>', |
| 197 |
); |
| 198 |
|
| 199 |
return array_merge( $links, $new_links ); |
| 200 |
} |
| 201 |
} |
| 202 |
|