| @@ -1,17 +1,69 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | /** |
| 3 | - * Block Editor - Republicize feature. | |
| 3 | + * Block Editor - Publicize and Republicize features. | |
| 4 | 4 | * |
| 5 | 5 | * @package automattic/jetpack |
| 6 | - **/ | |
| 6 | + */ | |
| 7 | 7 | |
| 8 | +namespace Automattic\Jetpack\Extensions\Publicize; | |
| 9 | + | |
| 10 | +use Automattic\Jetpack\Connection\Manager as Connection_Manager; | |
| 11 | +use Automattic\Jetpack\Modules; | |
| 12 | +use Automattic\Jetpack\Status; | |
| 13 | +use Automattic\Jetpack\Status\Host; | |
| 14 | +use Jetpack_Gutenberg; | |
| 15 | + | |
| 16 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 17 | + exit( 0 ); | |
| 18 | +} | |
| 19 | + | |
| 20 | +/** | |
| 21 | + * Register both Publicize and Republicize plugins. | |
| 22 | + * | |
| 23 | + * @return void | |
| 24 | + */ | |
| 25 | +function register_plugins() { | |
| 26 | + /** This filter is documented in projects/packages/publicize/src/class-publicize-base.php */ | |
| 27 | + $capability = apply_filters( 'jetpack_publicize_capability', 'publish_posts' ); | |
| 28 | + | |
| 29 | + // Capability check. | |
| 30 | + if ( | |
| 31 | + ! current_user_can( $capability ) | |
| 32 | + ) { | |
| 33 | + Jetpack_Gutenberg::set_extension_unavailable( 'publicize', 'unauthorized' ); | |
| 34 | + return; | |
| 35 | + } | |
| 36 | + | |
| 37 | + /* | |
| 38 | + * The extension is available even when the module is not active, | |
| 39 | + * so we can display a nudge to activate the module instead of the block. | |
| 40 | + * However, since non-admins cannot activate modules, we do not display the empty block for them. | |
| 41 | + */ | |
| 42 | + if ( ! ( new Modules() )->is_active( 'publicize' ) && ! current_user_can( 'jetpack_activate_modules' ) ) { | |
| 43 | + return; | |
| 44 | + } | |
| 45 | + | |
| 46 | + // Connection check. | |
| 47 | + if ( | |
| 48 | + ( new Host() )->is_wpcom_simple() | |
| 49 | + || ( ( new Connection_Manager( 'jetpack' ) )->has_connected_owner() && ! ( new Status() )->is_offline_mode() ) | |
| 50 | + ) { | |
| 51 | + // Register Publicize. | |
| 52 | + Jetpack_Gutenberg::set_extension_available( 'publicize' ); | |
| 53 | + | |
| 54 | + // Set the republicize availability, depending on the site plan. | |
| 55 | + Jetpack_Gutenberg::set_availability_for_plan( 'republicize' ); | |
| 56 | + } | |
| 57 | +} | |
| 58 | +add_action( 'jetpack_register_gutenberg_extensions', __NAMESPACE__ . '\register_plugins' ); | |
| 59 | + | |
| 8 | 60 | // Populate the available extensions with republicize. |
| 9 | 61 | add_filter( |
| 10 | 62 | 'jetpack_set_available_extensions', |
| 11 | 63 | function ( $extensions ) { |
| 12 | 64 | return array_merge( |
| 13 | - $extensions, | |
| 65 | + (array) $extensions, | |
| 14 | 66 | array( |
| 15 | 67 | 'republicize', |
| 16 | 68 | ) |
| 17 | 69 | ); |
| @@ -17,11 +69,17 @@ | ||
| 17 | 69 | ); |
| 18 | 70 | } |
| 19 | 71 | ); |
| 20 | 72 | |
| 21 | -// Set the republicize availability, depending on the site plan. | |
| 73 | +/** | |
| 74 | + * Publicize declares its support for the 'post' post type by default. | |
| 75 | + * Let's do it here as well, when the module hasn't been activated yet. | |
| 76 | + * It helps us display the Publicize UI in the editor. | |
| 77 | + */ | |
| 22 | 78 | add_action( |
| 23 | - 'jetpack_register_gutenberg_extensions', | |
| 79 | + 'init', | |
| 24 | 80 | function () { |
| 25 | - \Jetpack_Gutenberg::set_availability_for_plan( 'republicize' ); | |
| 81 | + if ( ! ( new Modules() )->is_active( 'publicize' ) ) { | |
| 82 | + add_post_type_support( 'post', 'publicize' ); | |
| 83 | + } | |
| 26 | 84 | } |
| 27 | 85 | ); |