post_type, self::post_types(), true )
&& current_user_can( 'edit_posts' )
&& ! use_block_editor_for_post( $post );
}
/**
* Add the composer box to the classic editor's side column.
*
* @param string $post_type Post type.
* @param mixed $post Post being edited.
*/
public static function meta_box( string $post_type, $post ): void {
if ( ! $post instanceof \WP_Post || ! self::is_classic_screen( $post ) ) {
return;
}
add_meta_box(
'orbitwrite-share',
__( 'Share on Orbitwrite', 'orbitwrite' ),
array( self::class, 'render_meta_box' ),
$post_type,
'side',
// After core's own boxes (`core`), so under Publish, never above
// it: `high` would push the Publish button down the screen.
'default',
// The block editor has its own sidebar; never show both.
array( '__back_compat_meta_box' => true )
);
}
/**
* The box: a mount point carrying what the page can't tell the script.
* Title, permalink and featured image are read live from the form.
*
* @param \WP_Post $post Post.
*/
public static function render_meta_box( \WP_Post $post ): void {
printf(
'',
(int) $post->ID,
esc_attr( $post->post_status ),
esc_url( self::permalink_for( $post ) ),
esc_html__( 'The Orbitwrite composer needs JavaScript.', 'orbitwrite' )
);
}
/**
* The address a post has, or will have once published. A draft's
* get_permalink() is `?p=123`, which is not what a social post should
* link to, so drafts use the sample permalink with their slug filled in.
*
* @param \WP_Post $post Post.
*/
private static function permalink_for( \WP_Post $post ): string {
if ( 'publish' === $post->post_status || ! function_exists( 'get_sample_permalink' ) ) {
return (string) get_permalink( $post );
}
list( $template, $name ) = get_sample_permalink( $post->ID );
return str_replace( array( '%postname%', '%pagename%' ), (string) $name, (string) $template );
}
/**
* Load the sidebar in the block editor, for post types that have a
* public page to share.
*/
public static function enqueue_editor(): void {
$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
if ( ! $screen || ! in_array( $screen->post_type, self::post_types(), true ) ) {
return;
}
self::enqueue_build( 'editor' );
}
/**
* Post types the sidebar appears on.
*/
public static function post_types(): array {
$types = get_post_types(
array(
'public' => true,
'show_in_rest' => true,
)
);
unset( $types['attachment'] );
/**
* Filters the post types the Orbitwrite sidebar appears on.
*
* @param string[] $types Post type names.
*/
return array_values( (array) apply_filters( 'orbitwrite_post_types', array_values( $types ) ) );
}
/**
* Enqueue one built entry point and its stylesheet.
*
* @param string $entry `admin`, `auto-share`, `dashboard`, `editor` or `classic`.
*/
private static function enqueue_build( string $entry ): void {
$asset_file = ORBITWRITE_PLUGIN_DIR . 'build/' . $entry . '.asset.php';
if ( ! file_exists( $asset_file ) ) {
return;
}
$asset = require $asset_file;
$handle = 'orbitwrite-' . $entry;
wp_enqueue_script(
$handle,
plugins_url( 'build/' . $entry . '.js', ORBITWRITE_PLUGIN_FILE ),
$asset['dependencies'],
$asset['version'],
true
);
wp_set_script_translations( $handle, 'orbitwrite' );
if ( in_array( $entry, array( 'admin', 'editor', 'classic' ), true ) ) {
// The composer's first question, answered in the page: no
// request and no spinner before it can show anything.
wp_add_inline_script(
$handle,
'window.orbitwriteBoot = ' . wp_json_encode(
array( 'status' => Rest::status_payload( get_current_user_id(), self::current_url() ) )
) . ';',
'before'
);
}
if ( file_exists( ORBITWRITE_PLUGIN_DIR . 'build/' . $entry . '.css' ) ) {
wp_enqueue_style(
$handle,
plugins_url( 'build/' . $entry . '.css', ORBITWRITE_PLUGIN_FILE ),
array( 'wp-components' ),
$asset['version']
);
}
}
/**
* The address of the admin screen being drawn, for connecting to come
* back to.
*/
private static function current_url(): string {
$uri = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : '';
// The admin's own origin (scheme, host and any port), then the path.
return '' === $uri ? self::page_url() : preg_replace( '#^(https?://[^/]+).*$#', '$1', admin_url() ) . $uri;
}
/**
* The dashboard widget: the last four weeks, for anyone who can write.
*/
public static function dashboard_widget(): void {
if ( ! current_user_can( 'edit_posts' ) ) {
return;
}
wp_add_dashboard_widget(
'orbitwrite_summary',
__( 'Orbitwrite: last 4 weeks', 'orbitwrite' ),
array( self::class, 'render_dashboard_widget' )
);
}
/**
* The widget's body: a mount point, or the way to connect.
*/
public static function render_dashboard_widget(): void {
if ( ! Token_Store::get( get_current_user_id() ) ) {
printf(
'