story_post_type = $story_post_type; $this->assets = $assets; } /** * Initialize admin-related functionality. * * @since 1.25.0 */ public function register(): void { $post_type = $this->story_post_type->get_slug(); add_filter( "wp_{$post_type}_revisions_to_keep", [ $this, 'revisions_to_keep' ] ); add_filter( '_wp_post_revision_fields', [ $this, 'filter_revision_fields' ], 10, 2 ); add_filter( 'wp_get_revision_ui_diff', [ $this, 'filter_revision_ui_diff' ], 10, 3 ); add_action( 'admin_print_footer_scripts-revision.php', [ $this, 'enqueue_player_script' ] ); } /** * Force WordPress to only keep 10 revisions for the web stories post type. * * @since 1.25.0 * * @param int|bool $num Number of revisions to store. * @return int Number of revisions to store. */ public function revisions_to_keep( $num ): int { $num = (int) $num; return $num >= 0 && $num < 10 ? $num : 10; } /** * Filters the revision fields to ensure that JSON representation gets saved to Story revisions. * * @since 1.25.0 * * @param array|mixed $fields Array of allowed revision fields. * @param array $story Story post array. * @return array|mixed Array of allowed fields. * * @template T * * @phpstan-param PostData $story * @phpstan-return ($fields is array ? array : mixed) */ public function filter_revision_fields( $fields, array $story ) { if ( ! \is_array( $fields ) ) { return $fields; } if ( $this->story_post_type->get_slug() === $story['post_type'] || ( 'revision' === $story['post_type'] && ! empty( $story['post_parent'] ) && get_post_type( $story['post_parent'] ) === $this->story_post_type->get_slug() ) ) { $fields['post_content_filtered'] = __( 'Story data', 'web-stories' ); } return $fields; } /** * Filters the fields displayed in the post revision diff UI. * * @since 1.25.0 * * @param array[]|mixed $fields Array of revision UI fields. Each item is an array of id, name, and diff. * @param WP_Post|false $compare_from The revision post to compare from or false if dealing with the first revision. * @param WP_Post $compare_to The revision post to compare to. * @return array[]|mixed Filtered array of revision UI fields. * * @phpstan-return array|mixed */ public function filter_revision_ui_diff( $fields, $compare_from, WP_Post $compare_to ) { if ( ! \is_array( $fields ) ) { return $fields; } $parent = get_post_parent( $compare_to ); if ( ! $parent instanceof WP_Post || $this->story_post_type->get_slug() !== $parent->post_type ) { return $fields; } $player_from = ''; if ( $compare_from instanceof WP_Post ) { $player_from = $this->get_story_player( $compare_from ); } $player_to = $this->get_story_player( $compare_to ); $args = [ 'show_split_view' => true, 'title_left' => __( 'Removed' ), // phpcs:ignore WordPress.WP.I18n.MissingArgDomain 'title_right' => __( 'Added' ), // phpcs:ignore WordPress.WP.I18n.MissingArgDomain ]; /** This filter is documented in wp-admin/includes/revision.php */ $args = apply_filters( 'revision_text_diff_options', $args, 'post_content', $compare_from, $compare_to ); $fields_to_return = []; /** * Revision field. * * @phpstan-var RevisionField $field * @var array $field */ foreach ( $fields as $field ) { if ( 'post_title' === $field['id'] ) { $fields_to_return[] = $field; } if ( 'post_content' === $field['id'] || 'post_content_filtered' === $field['id'] ) { $field['title'] = __( 'Content', 'web-stories' ); $diff = ''; // In split screen mode, show the title before/after side by side. if ( true === $args['show_split_view'] ) { $diff .= ''; } else { $diff .= ''; } $diff .= ''; $diff .= '
' . $player_from . '' . $player_to . '' . $player_from . '
' . $player_to . '
'; $field['diff'] = $diff; $fields_to_return[] = $field; return $fields_to_return; } } return $fields; } /** * Enqueues amp-story-player assets on the revisions screen. * * @since 1.25.0 */ public function enqueue_player_script(): void { $this->assets->enqueue_style( AMP_Story_Player_Assets::SCRIPT_HANDLE ); $this->assets->enqueue_script( AMP_Story_Player_Assets::SCRIPT_HANDLE ); wp_add_inline_script( AMP_Story_Player_Assets::SCRIPT_HANDLE, <<<'JS' const loadPlayers = () => document.querySelectorAll('amp-story-player').forEach(playerEl => (new AmpStoryPlayer(window, playerEl)).load()); const originalFrame = wp.revisions.view.Frame; wp.revisions.view.Frame = originalFrame.extend({ render: function() { originalFrame.prototype.render.apply(this, arguments); loadPlayers(); this.listenTo( this.model, 'update:diff', () => loadPlayers() ); }, }); JS ); } /** * Returns the story player markup for a given post. * * @since 1.25.0 * * @param WP_Post $post Post instance. * @return string Story player markup. */ protected function get_story_player( WP_Post $post ): string { $url = esc_url( wp_nonce_url( add_query_arg( 'rev_id', $post->ID, get_permalink( $post->post_parent ) ), 'web_stories_revision_for_' . $post->post_parent ) ); $title = esc_html( get_the_title( $post ) ); return <<$title Player; } }