| 1 |
<?php |
| 2 |
/** |
| 3 |
* The Civist plugin class. |
| 4 |
* |
| 5 |
* @package civist |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* The Civist plugin. |
| 10 |
*/ |
| 11 |
class Civist { |
| 12 |
/** |
| 13 |
* The plugin version. |
| 14 |
* |
| 15 |
* @var string |
| 16 |
*/ |
| 17 |
private $version; |
| 18 |
/** |
| 19 |
* The plugin name. |
| 20 |
* |
| 21 |
* @var string |
| 22 |
*/ |
| 23 |
private $plugin_name; |
| 24 |
/** |
| 25 |
* The plugin slug. |
| 26 |
* |
| 27 |
* @var string |
| 28 |
*/ |
| 29 |
private $plugin_slug; |
| 30 |
/** |
| 31 |
* The plugin text domain. |
| 32 |
* |
| 33 |
* @var string |
| 34 |
*/ |
| 35 |
private $plugin_text_domain; |
| 36 |
/** |
| 37 |
* The plugin file. |
| 38 |
* |
| 39 |
* @var string |
| 40 |
*/ |
| 41 |
private $plugin_file; |
| 42 |
/** |
| 43 |
* The plugin registration url. |
| 44 |
* |
| 45 |
* @var string |
| 46 |
*/ |
| 47 |
private $registration_url; |
| 48 |
/** |
| 49 |
* The url of the geoip service. |
| 50 |
* |
| 51 |
* @var string |
| 52 |
*/ |
| 53 |
private $geoip_url; |
| 54 |
/** |
| 55 |
* The widget supported languages. |
| 56 |
* |
| 57 |
* @var array |
| 58 |
*/ |
| 59 |
private $widget_supported_languages; |
| 60 |
/** |
| 61 |
* The plugin settings manager. |
| 62 |
* |
| 63 |
* @var Civist_Settings_Manager |
| 64 |
*/ |
| 65 |
private $settings_manager; |
| 66 |
/** |
| 67 |
* Enforces HTTPS |
| 68 |
* |
| 69 |
* @var bool |
| 70 |
*/ |
| 71 |
private $enforce_https; |
| 72 |
/** |
| 73 |
* The plugin oembed provider. |
| 74 |
* |
| 75 |
* @var Civist_OEmbed |
| 76 |
*/ |
| 77 |
private $oembed_provider; |
| 78 |
/** |
| 79 |
* The plugin jwt manager. |
| 80 |
* |
| 81 |
* @var Civist_Jwt |
| 82 |
*/ |
| 83 |
private $plugin_jwt; |
| 84 |
/** |
| 85 |
* The plugin scripts. |
| 86 |
* |
| 87 |
* @var Civist_Scripts |
| 88 |
*/ |
| 89 |
private $plugin_scripts; |
| 90 |
|
| 91 |
/** |
| 92 |
* The Civist class constructor |
| 93 |
* |
| 94 |
* @param string $version The version of the plugin. |
| 95 |
* @param string $plugin_name The name of the plugin. |
| 96 |
* @param string $plugin_slug The slug of the plugin. |
| 97 |
* @param string $plugin_text_domain The text domain of the plugin. |
| 98 |
* @param string $plugin_file The file of the plugin. |
| 99 |
* @param string $registration_url The default registration URL for Civist. |
| 100 |
* @param string $geoip_url The url of the GeoIP service. |
| 101 |
* @param array $widget_supported_languages The widget supported languages. |
| 102 |
* @param bool $enforce_https Enforces HTTPS. |
| 103 |
*/ |
| 104 |
public function __construct( $version, $plugin_name, $plugin_slug, $plugin_text_domain, $plugin_file, $registration_url, $geoip_url, $widget_supported_languages, $enforce_https ) { |
| 105 |
$this->version = $version; |
| 106 |
$this->plugin_name = $plugin_name; |
| 107 |
$this->plugin_slug = $plugin_slug; |
| 108 |
$this->plugin_text_domain = $plugin_text_domain; |
| 109 |
$this->plugin_file = $plugin_file; |
| 110 |
$this->registration_url = $registration_url; |
| 111 |
$this->geoip_url = $geoip_url; |
| 112 |
$this->widget_supported_languages = $widget_supported_languages; |
| 113 |
$this->enforce_https = $enforce_https; |
| 114 |
$this->load_dependencies(); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Run the loader to execute all of the hooks with WordPress. |
| 119 |
*/ |
| 120 |
public function run() { |
| 121 |
$this->settings_manager = new Civist_Settings_Manager( $this->plugin_slug ); |
| 122 |
$this->oembed_provider = new Civist_OEmbed( $this->plugin_file, $this->plugin_slug, $this->settings_manager ); |
| 123 |
$this->settings_manager->initialize_plugin_options( |
| 124 |
array( |
| 125 |
'version' => $this->version, |
| 126 |
'registration_url' => $this->registration_url, |
| 127 |
'geoip_url' => $this->geoip_url, |
| 128 |
) |
| 129 |
); |
| 130 |
if ( is_admin() ) { |
| 131 |
$this->plugin_jwt = new Civist_Jwt( $this->plugin_slug, $this->settings_manager ); |
| 132 |
} else { |
| 133 |
$this->plugin_jwt = null; |
| 134 |
} |
| 135 |
$this->plugin_scripts = new Civist_Scripts( $this->plugin_name, $this->plugin_file, $this->plugin_slug, $this->settings_manager, $this->widget_supported_languages, $this->enforce_https, $this->plugin_jwt ); |
| 136 |
|
| 137 |
$this->handle_version_change(); |
| 138 |
$this->define_non_admin_hooks(); |
| 139 |
$this->define_admin_hooks(); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Updates the default settings when there is a version change. |
| 144 |
*/ |
| 145 |
private function handle_version_change() { |
| 146 |
$current_version = $this->settings_manager->get_option_by_name( 'version' ); |
| 147 |
if ( $current_version !== $this->version ) { |
| 148 |
$this->settings_manager->update( |
| 149 |
array( |
| 150 |
'version' => $this->version, |
| 151 |
'registration_url' => $this->registration_url, |
| 152 |
'geoip_url' => $this->geoip_url, |
| 153 |
) |
| 154 |
); |
| 155 |
|
| 156 |
$this->oembed_provider->clear_cache(); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* Loads the plugin dependencies. |
| 162 |
*/ |
| 163 |
private function load_dependencies() { |
| 164 |
require_once 'class-civist-settings-manager.php'; |
| 165 |
require_once 'class-civist-oembed.php'; |
| 166 |
require_once 'class-civist-shortcode.php'; |
| 167 |
require_once 'class-civist-settings.php'; |
| 168 |
require_once 'class-civist-scripts.php'; |
| 169 |
require_once 'class-civist-editor.php'; |
| 170 |
if ( is_admin() ) { |
| 171 |
require_once 'class-civist-jwt.php'; |
| 172 |
require_once 'class-civist-admin.php'; |
| 173 |
require_once 'class-civist-registration.php'; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Registers all of the hoks specific to the admin area. |
| 179 |
*/ |
| 180 |
private function define_admin_hooks() { |
| 181 |
if ( is_admin() ) { |
| 182 |
add_action( 'wp_ajax_' . $this->settings_manager->get_client_action_name(), array( $this->settings_manager, 'handle_settings_from_client' ) ); |
| 183 |
|
| 184 |
add_action( 'wp_ajax_' . $this->plugin_jwt->get_client_action_name(), array( $this->plugin_jwt, 'serve_jwt_token' ) ); |
| 185 |
|
| 186 |
$plugin_settings = new Civist_Settings( $this->plugin_slug, $this->settings_manager ); |
| 187 |
add_action( 'admin_init', array( $plugin_settings, 'register_settings' ) ); |
| 188 |
add_filter( 'plugin_action_links_' . plugin_basename( $this->plugin_file ), array( $plugin_settings, 'register_settings_plugin_action_link' ) ); |
| 189 |
|
| 190 |
$plugin_editor = new Civist_Editor( $this->plugin_name, $this->plugin_file, $this->plugin_slug, $this->plugin_scripts ); |
| 191 |
$plugin_admin = new Civist_Admin( $this->plugin_name, $this->plugin_file, $this->plugin_slug, $this->settings_manager, $plugin_settings, $this->plugin_scripts, $plugin_editor ); |
| 192 |
add_action( 'admin_menu', array( $plugin_admin, 'add_admin_menu' ) ); |
| 193 |
add_action( 'admin_enqueue_scripts', array( $plugin_admin, 'enqueue_scripts' ) ); |
| 194 |
add_filter( 'plugin_action_links_' . plugin_basename( $this->plugin_file ), array( $plugin_admin, 'add_reconnect_link' ), 10, 2 ); |
| 195 |
|
| 196 |
if ( ! $this->settings_manager->is_connected() ) { |
| 197 |
$plugin_registration = new Civist_Registration( $this->plugin_slug ); |
| 198 |
add_action( 'load-plugins.php', array( $plugin_registration, 'show_connect_notice' ) ); |
| 199 |
add_action( 'load-index.php', array( $plugin_registration, 'show_connect_notice' ) ); |
| 200 |
} else { |
| 201 |
add_action( 'media_buttons', array( $plugin_editor, 'add_petition_media_button' ) ); |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
/** |
| 207 |
* Define oembed hook. |
| 208 |
*/ |
| 209 |
public function define_oembed_hook() { |
| 210 |
add_filter( 'embed_oembed_html', array( $this->oembed_provider, 'wrap_widget' ), 10, 4 ); |
| 211 |
} |
| 212 |
|
| 213 |
/** |
| 214 |
* Registers all of the hooks not specific to the admin area. |
| 215 |
*/ |
| 216 |
private function define_non_admin_hooks() { |
| 217 |
if ( $this->settings_manager->is_connected() ) { |
| 218 |
add_action( 'init', array( $this->oembed_provider, 'oembed_provider' ) ); |
| 219 |
add_filter( 'http_request_host_is_external', array( $this->oembed_provider, 'allow_localhost' ), 10, 3 ); |
| 220 |
|
| 221 |
$this->define_oembed_hook(); |
| 222 |
// This is necessary because some plugins (like "Instant Articles for WP") remove all `embed_oembed_html` filters. |
| 223 |
add_action( 'plugins_loaded', array( $this, 'define_oembed_hook' ) ); |
| 224 |
|
| 225 |
$plugin_shortcode = new Civist_Shortcode( $this->plugin_slug ); |
| 226 |
add_action( 'init', array( $plugin_shortcode, 'register_shortcode' ) ); |
| 227 |
$plugin_editor = new Civist_Editor( $this->plugin_name, $this->plugin_file, $this->plugin_slug, $this->plugin_scripts ); |
| 228 |
add_action( 'init', array( $plugin_editor, 'register_plugin_blocks' ) ); |
| 229 |
} |
| 230 |
} |
| 231 |
} |
| 232 |
|