| @@ -1,0 +1,338 @@ | ||
| 1 | +<?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName | |
| 2 | +/** | |
| 3 | + * Set up Sharing functionality and management in wp-admin. | |
| 4 | + * | |
| 5 | + * @package automattic/jetpack | |
| 6 | + */ | |
| 7 | + | |
| 8 | +// phpcs:disable Universal.Files.SeparateFunctionsFromOO.Mixed -- TODO: Move classes to appropriately-named class files. | |
| 9 | + | |
| 10 | +use Automattic\Jetpack\Assets; | |
| 11 | +use Automattic\Jetpack\Sharing_Likes\Post_Sharing_Switch; | |
| 12 | +use Automattic\Jetpack\Sharing_Likes\Settings\Post_Handler; | |
| 13 | +use Automattic\Jetpack\Sharing_Likes\Settings\Services_Config; | |
| 14 | +use Automattic\Jetpack\Sharing_Likes\Settings\Settings_Page; | |
| 15 | +use Automattic\Jetpack\Status\Host; | |
| 16 | + | |
| 17 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 18 | + exit( 0 ); | |
| 19 | +} | |
| 20 | + | |
| 21 | +if ( ! defined( 'WP_SHARING_PLUGIN_URL' ) ) { | |
| 22 | + define( 'WP_SHARING_PLUGIN_URL', plugin_dir_url( __FILE__ ) ); | |
| 23 | + define( 'WP_SHARING_PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); | |
| 24 | +} | |
| 25 | + | |
| 26 | +/** | |
| 27 | + * Utilities to manage sharing settings from wp-admin. | |
| 28 | + */ | |
| 29 | +class Sharing_Admin { | |
| 30 | + | |
| 31 | + /** | |
| 32 | + * The services configuration UI. | |
| 33 | + * | |
| 34 | + * @var Services_Config | |
| 35 | + */ | |
| 36 | + private $services_config; | |
| 37 | + | |
| 38 | + /** | |
| 39 | + * Constructor. | |
| 40 | + * Hook into WordPress to add our functionality. | |
| 41 | + */ | |
| 42 | + public function __construct() { | |
| 43 | + require_once WP_SHARING_PLUGIN_DIR . 'sharing-service.php'; | |
| 44 | + | |
| 45 | + $this->services_config = new Services_Config(); | |
| 46 | + | |
| 47 | + // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- nonces are handled in process_requests. | |
| 48 | + if ( isset( $_GET['page'] ) && $_GET['page'] === 'sharing' ) { | |
| 49 | + add_action( 'admin_init', array( $this, 'admin_init' ) ); | |
| 50 | + } | |
| 51 | + | |
| 52 | + // Insert our CSS and JS | |
| 53 | + add_action( 'load-settings_page_sharing', array( $this, 'sharing_head' ) ); | |
| 54 | + | |
| 55 | + // Catch AJAX | |
| 56 | + add_action( 'wp_ajax_sharing_save_services', array( $this->services_config, 'ajax_save_services' ) ); | |
| 57 | + add_action( 'wp_ajax_sharing_save_options', array( $this->services_config, 'ajax_save_options' ) ); | |
| 58 | + add_action( 'wp_ajax_sharing_new_service', array( $this->services_config, 'ajax_new_service' ) ); | |
| 59 | + add_action( 'wp_ajax_sharing_delete_service', array( $this->services_config, 'ajax_delete_service' ) ); | |
| 60 | + } | |
| 61 | + | |
| 62 | + /** | |
| 63 | + * Enqueue scripts and styles on the sharing settings page. | |
| 64 | + * | |
| 65 | + * @return void | |
| 66 | + */ | |
| 67 | + public function sharing_head() { | |
| 68 | + wp_enqueue_script( | |
| 69 | + 'sharing-js', | |
| 70 | + Assets::get_file_url_for_environment( | |
| 71 | + '_inc/build/sharedaddy/admin-sharing.min.js', | |
| 72 | + 'modules/sharedaddy/admin-sharing.js' | |
| 73 | + ), | |
| 74 | + array( 'jquery', 'jquery-ui-draggable', 'jquery-ui-droppable', 'jquery-ui-sortable', 'jquery-form' ), | |
| 75 | + JETPACK__VERSION, | |
| 76 | + false | |
| 77 | + ); | |
| 78 | + | |
| 79 | + // admin-sharing.js reads this before it submits, so leaving it undefined breaks service removal. | |
| 80 | + wp_add_inline_script( | |
| 81 | + 'sharing-js', | |
| 82 | + 'var sharing_loading_icon = ' . wp_json_encode( admin_url( '/images/loading.gif' ), JSON_UNESCAPED_SLASHES | JSON_HEX_TAG | JSON_HEX_AMP ) . ';', | |
| 83 | + 'before' | |
| 84 | + ); | |
| 85 | + | |
| 86 | + /** | |
| 87 | + * Filters the switch that if set to true allows Jetpack to use minified assets. Defaults to true | |
| 88 | + * if the SCRIPT_DEBUG constant is not set or set to false. The filter overrides it. | |
| 89 | + * | |
| 90 | + * @since 6.2.0 | |
| 91 | + * | |
| 92 | + * @param boolean $var should Jetpack use minified assets. | |
| 93 | + */ | |
| 94 | + $postfix = apply_filters( 'jetpack_should_use_minified_assets', true ) ? '.min' : ''; | |
| 95 | + if ( is_rtl() ) { | |
| 96 | + wp_enqueue_style( 'sharing-admin', WP_SHARING_PLUGIN_URL . 'admin-sharing-rtl' . $postfix . '.css', false, JETPACK__VERSION ); | |
| 97 | + } else { | |
| 98 | + wp_enqueue_style( 'sharing-admin', WP_SHARING_PLUGIN_URL . 'admin-sharing' . $postfix . '.css', false, JETPACK__VERSION ); | |
| 99 | + } | |
| 100 | + wp_enqueue_style( 'sharing', WP_SHARING_PLUGIN_URL . 'sharing.css', false, JETPACK__VERSION ); | |
| 101 | + | |
| 102 | + wp_enqueue_style( 'social-logos' ); | |
| 103 | + wp_enqueue_script( 'sharing-js-fe', WP_SHARING_PLUGIN_URL . 'sharing.js', array(), 4, false ); | |
| 104 | + add_thickbox(); | |
| 105 | + } | |
| 106 | + | |
| 107 | + /** | |
| 108 | + * Load the process that handles saving changes on the sharing settings page. | |
| 109 | + * | |
| 110 | + * @return void | |
| 111 | + */ | |
| 112 | + public function admin_init() { | |
| 113 | + $this->services_config->process_requests(); | |
| 114 | + } | |
| 115 | + | |
| 116 | + /** | |
| 117 | + * Save changes to sharing settings. | |
| 118 | + * | |
| 119 | + * @deprecated 16.3 Use Automattic\Jetpack\Sharing_Likes\Settings\Services_Config::process_requests() instead. | |
| 120 | + * | |
| 121 | + * @return void | |
| 122 | + */ | |
| 123 | + public function process_requests() { | |
| 124 | + _deprecated_function( __METHOD__, 'jetpack-16.3', 'Automattic\Jetpack\Sharing_Likes\Settings\Services_Config::process_requests' ); | |
| 125 | + } | |
| 126 | + | |
| 127 | + /** | |
| 128 | + * Register Sharing settings menu page in Settings > Sharing. | |
| 129 | + * | |
| 130 | + * @deprecated 16.3 Use Automattic\Jetpack\Sharing_Likes\Settings\Settings_Page::register_menu() instead. | |
| 131 | + * | |
| 132 | + * @return void | |
| 133 | + */ | |
| 134 | + public function subscription_menu() { | |
| 135 | + _deprecated_function( __METHOD__, 'jetpack-16.3', 'Automattic\Jetpack\Sharing_Likes\Settings\Settings_Page::register_menu' ); | |
| 136 | + } | |
| 137 | + | |
| 138 | + /** | |
| 139 | + * Save changes to sharing services via AJAX. | |
| 140 | + * | |
| 141 | + * @deprecated 16.3 Use Automattic\Jetpack\Sharing_Likes\Settings\Services_Config::ajax_save_services() instead. | |
| 142 | + * | |
| 143 | + * @return void | |
| 144 | + */ | |
| 145 | + public function ajax_save_services() { | |
| 146 | + _deprecated_function( __METHOD__, 'jetpack-16.3', 'Automattic\Jetpack\Sharing_Likes\Settings\Services_Config::ajax_save_services' ); | |
| 147 | + } | |
| 148 | + | |
| 149 | + /** | |
| 150 | + * Create a new custom sharing service via AJAX. | |
| 151 | + * | |
| 152 | + * @deprecated 16.3 Use Automattic\Jetpack\Sharing_Likes\Settings\Services_Config::ajax_new_service() instead. | |
| 153 | + * | |
| 154 | + * @return void | |
| 155 | + */ | |
| 156 | + public function ajax_new_service() { | |
| 157 | + _deprecated_function( __METHOD__, 'jetpack-16.3', 'Automattic\Jetpack\Sharing_Likes\Settings\Services_Config::ajax_new_service' ); | |
| 158 | + } | |
| 159 | + | |
| 160 | + /** | |
| 161 | + * Delete a sharing service via AJAX. | |
| 162 | + * | |
| 163 | + * @deprecated 16.3 Use Automattic\Jetpack\Sharing_Likes\Settings\Services_Config::ajax_delete_service() instead. | |
| 164 | + * | |
| 165 | + * @return void | |
| 166 | + */ | |
| 167 | + public function ajax_delete_service() { | |
| 168 | + _deprecated_function( __METHOD__, 'jetpack-16.3', 'Automattic\Jetpack\Sharing_Likes\Settings\Services_Config::ajax_delete_service' ); | |
| 169 | + } | |
| 170 | + | |
| 171 | + /** | |
| 172 | + * Save changes to sharing settings via AJAX. | |
| 173 | + * | |
| 174 | + * @deprecated 16.3 Use Automattic\Jetpack\Sharing_Likes\Settings\Services_Config::ajax_save_options() instead. | |
| 175 | + * | |
| 176 | + * @return void | |
| 177 | + */ | |
| 178 | + public function ajax_save_options() { | |
| 179 | + _deprecated_function( __METHOD__, 'jetpack-16.3', 'Automattic\Jetpack\Sharing_Likes\Settings\Services_Config::ajax_save_options' ); | |
| 180 | + } | |
| 181 | + | |
| 182 | + /** | |
| 183 | + * Display a preview of a sharing service. | |
| 184 | + * | |
| 185 | + * @deprecated 16.3 Use Automattic\Jetpack\Sharing_Likes\Settings\Services_Config::output_preview() instead. | |
| 186 | + * | |
| 187 | + * @param object $service Sharing service object. | |
| 188 | + * | |
| 189 | + * @return void | |
| 190 | + */ | |
| 191 | + public function output_preview( $service ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | |
| 192 | + _deprecated_function( __METHOD__, 'jetpack-16.3', 'Automattic\Jetpack\Sharing_Likes\Settings\Services_Config::output_preview' ); | |
| 193 | + } | |
| 194 | + | |
| 195 | + /** | |
| 196 | + * Display a specific sharing service. | |
| 197 | + * | |
| 198 | + * @deprecated 16.3 Use Automattic\Jetpack\Sharing_Likes\Settings\Services_Config::output_service() instead. | |
| 199 | + * | |
| 200 | + * @param string $id Service unique ID. | |
| 201 | + * @param object $service Sharing service. | |
| 202 | + * @param bool $show_dropdown Display a dropdown. Not in use at the moment. | |
| 203 | + * | |
| 204 | + * @return void | |
| 205 | + */ | |
| 206 | + public function output_service( $id, $service, $show_dropdown = false ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | |
| 207 | + _deprecated_function( __METHOD__, 'jetpack-16.3', 'Automattic\Jetpack\Sharing_Likes\Settings\Services_Config::output_service' ); | |
| 208 | + } | |
| 209 | + | |
| 210 | + /** | |
| 211 | + * Display admin UI within a Jetpack header and footer. | |
| 212 | + * | |
| 213 | + * @deprecated 16.3 Use Automattic\Jetpack\Sharing_Likes\Settings\Settings_Page::render() instead. | |
| 214 | + * | |
| 215 | + * @return void | |
| 216 | + */ | |
| 217 | + public function wrapper_admin_page() { | |
| 218 | + _deprecated_function( __METHOD__, 'jetpack-16.3', 'Automattic\Jetpack\Sharing_Likes\Settings\Settings_Page::render' ); | |
| 219 | + } | |
| 220 | + | |
| 221 | + /** | |
| 222 | + * Sharing settings inner page structure. | |
| 223 | + * | |
| 224 | + * @deprecated 16.3 Use Automattic\Jetpack\Sharing_Likes\Settings\Settings_Page::render() instead. | |
| 225 | + * | |
| 226 | + * @return void | |
| 227 | + */ | |
| 228 | + public function management_page() { | |
| 229 | + _deprecated_function( __METHOD__, 'jetpack-16.3', 'Automattic\Jetpack\Sharing_Likes\Settings\Settings_Page::render' ); | |
| 230 | + } | |
| 231 | + | |
| 232 | + /** | |
| 233 | + * Check if we should encourage to use the site editor instead of the legacy sharing settings. | |
| 234 | + * | |
| 235 | + * @deprecated 16.3 Use Automattic\Jetpack\Sharing_Likes\Settings\Environment instead. | |
| 236 | + * | |
| 237 | + * @return bool | |
| 238 | + */ | |
| 239 | + public function should_use_site_editor() { | |
| 240 | + _deprecated_function( __METHOD__, 'jetpack-16.3', 'Automattic\Jetpack\Sharing_Likes\Settings\Environment' ); | |
| 241 | + return false; | |
| 242 | + } | |
| 243 | + | |
| 244 | + /** | |
| 245 | + * Display services admin UI for settings. | |
| 246 | + * | |
| 247 | + * @deprecated 16.3 Use Automattic\Jetpack\Sharing_Likes\Settings\Services_Config::render() instead. | |
| 248 | + * | |
| 249 | + * @return void | |
| 250 | + */ | |
| 251 | + public function services_config_display() { | |
| 252 | + _deprecated_function( __METHOD__, 'jetpack-16.3', 'Automattic\Jetpack\Sharing_Likes\Settings\Services_Config::render' ); | |
| 253 | + } | |
| 254 | + | |
| 255 | + /** | |
| 256 | + * Display sharing block admin UI for settings. | |
| 257 | + * | |
| 258 | + * @deprecated 16.3 Use Automattic\Jetpack\Sharing_Likes\Settings\Sharing_Section::render() instead. | |
| 259 | + * | |
| 260 | + * @return void | |
| 261 | + */ | |
| 262 | + public function sharing_block_display() { | |
| 263 | + _deprecated_function( __METHOD__, 'jetpack-16.3', 'Automattic\Jetpack\Sharing_Likes\Settings\Sharing_Section::render' ); | |
| 264 | + } | |
| 265 | + | |
| 266 | + /** | |
| 267 | + * Display the "Go to the site editor" prompt. | |
| 268 | + * | |
| 269 | + * @deprecated 16.3 Use Automattic\Jetpack\Sharing_Likes\Settings\Sharing_Section::render() instead. | |
| 270 | + * | |
| 271 | + * @return void | |
| 272 | + */ | |
| 273 | + public function site_editor_prompt_display() { | |
| 274 | + _deprecated_function( __METHOD__, 'jetpack-16.3', 'Automattic\Jetpack\Sharing_Likes\Settings\Sharing_Section::render' ); | |
| 275 | + } | |
| 276 | +} | |
| 277 | + | |
| 278 | +/** | |
| 279 | + * Callback to get the value for the jetpack_sharing_enabled field. | |
| 280 | + * | |
| 281 | + * @deprecated 16.3 Use Automattic\Jetpack\Sharing_Likes\Post_Sharing_Switch::get_value() instead. | |
| 282 | + * | |
| 283 | + * @param array $post The post object. | |
| 284 | + * | |
| 285 | + * @return bool | |
| 286 | + */ | |
| 287 | +function jetpack_post_sharing_get_value( array $post ) { | |
| 288 | + _deprecated_function( __FUNCTION__, 'jetpack-16.3', 'Automattic\Jetpack\Sharing_Likes\Post_Sharing_Switch::get_value' ); | |
| 289 | + return Post_Sharing_Switch::get_value( $post ); | |
| 290 | +} | |
| 291 | + | |
| 292 | +/** | |
| 293 | + * Callback to set sharing_disabled post_meta when the | |
| 294 | + * jetpack_sharing_enabled field is updated. | |
| 295 | + * | |
| 296 | + * @deprecated 16.3 Use Automattic\Jetpack\Sharing_Likes\Post_Sharing_Switch::update_value() instead. | |
| 297 | + * | |
| 298 | + * @param bool $enable_sharing Should sharing be enabled on this post. | |
| 299 | + * @param WP_Post $post_object The post object. | |
| 300 | + * | |
| 301 | + * @return int|bool | |
| 302 | + */ | |
| 303 | +function jetpack_post_sharing_update_value( $enable_sharing, $post_object ) { | |
| 304 | + _deprecated_function( __FUNCTION__, 'jetpack-16.3', 'Automattic\Jetpack\Sharing_Likes\Post_Sharing_Switch::update_value' ); | |
| 305 | + return Post_Sharing_Switch::update_value( $enable_sharing, $post_object ); | |
| 306 | +} | |
| 307 | + | |
| 308 | +/** | |
| 309 | + * Add Sharing post_meta to the REST API Post response. | |
| 310 | + * | |
| 311 | + * @deprecated 16.3 Use Automattic\Jetpack\Sharing_Likes\Post_Sharing_Switch::register_rest_field() instead. | |
| 312 | + */ | |
| 313 | +function jetpack_post_sharing_register_rest_field() { | |
| 314 | + _deprecated_function( __FUNCTION__, 'jetpack-16.3', 'Automattic\Jetpack\Sharing_Likes\Post_Sharing_Switch::register_rest_field' ); | |
| 315 | + Post_Sharing_Switch::register_rest_field(); | |
| 316 | +} | |
| 317 | + | |
| 318 | +Post_Sharing_Switch::init(); | |
| 319 | + | |
| 320 | +/** | |
| 321 | + * Initialize sharing settings in WP Admin. | |
| 322 | + * | |
| 323 | + * @return void | |
| 324 | + */ | |
| 325 | +function sharing_admin_init() { | |
| 326 | + global $sharing_admin; | |
| 327 | + | |
| 328 | + $sharing_admin = new Sharing_Admin(); | |
| 329 | + | |
| 330 | + // load-jetpack.php registers the screen everywhere else, but does not run on | |
| 331 | + // WordPress.com Simple, where this file is the only Sharing code loaded. | |
| 332 | + if ( ( new Host() )->is_wpcom_simple() ) { | |
| 333 | + Settings_Page::init(); | |
| 334 | + Post_Handler::init(); | |
| 335 | + } | |
| 336 | +} | |
| 337 | + | |
| 338 | +add_action( 'init', 'sharing_admin_init' ); | |