Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mark publisher logo as such in WordPress media library #11836

Merged
merged 11 commits into from
Jun 29, 2022
37 changes: 33 additions & 4 deletions includes/Admin/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,22 @@
use Google\Web_Stories\Model\Story;
use Google\Web_Stories\Renderer\Story\Image;
use Google\Web_Stories\Service_Base;
use Google\Web_Stories\Settings;
use Google\Web_Stories\Story_Post_Type;
use WP_Post;

/**
* Admin class.
*/
class Admin extends Service_Base {

/**
* Settings instance.
*
* @var Settings Settings instance.
*/
private $settings;

/**
* Context instance.
*
Expand All @@ -49,10 +58,12 @@ class Admin extends Service_Base {
/**
* Single constructor.
*
* @param Context $context Context instance.
* @param Settings $settings Settings instance.
* @param Context $context Context instance.
*/
public function __construct( Context $context ) {
$this->context = $context;
public function __construct( Settings $settings, Context $context ) {
$this->settings = $settings;
$this->context = $context;
}

/**
Expand All @@ -64,6 +75,7 @@ public function register(): void {
add_filter( 'admin_body_class', [ $this, 'admin_body_class' ], 99 );
add_filter( 'default_content', [ $this, 'prefill_post_content' ], 10, 2 );
add_filter( 'default_title', [ $this, 'prefill_post_title' ] );
add_filter( 'display_media_states', [ $this, 'media_states' ], 10, 2 );
}

/**
Expand Down Expand Up @@ -155,7 +167,6 @@ public function prefill_post_content( $content, WP_Post $post ) {
];

if ( ! use_block_editor_for_post( $post ) ) {

$content = '[web_stories_embed url="%1$s" title="%2$s" poster="%3$s" width="%4$s" height="%5$s" align="%6$s"]';

return sprintf(
Expand Down Expand Up @@ -229,4 +240,22 @@ public function prefill_post_title( $title ) {
// Otherwise it runs through wptexturize() and the like, which we want to avoid.
return $post->post_title;
}

/**
* Adds active publisher logo to media state output.
*
* @since 1.23.0
*
* @param string[] $media_states Array of media states.
* @param WP_Post $post Post object.
* @return string[] updated media states.
*/
public function media_states( $media_states, $post ): array {
timarney marked this conversation as resolved.
Show resolved Hide resolved
$active_publisher_logo_id = absint( $this->settings->get_setting( $this->settings::SETTING_NAME_ACTIVE_PUBLISHER_LOGO ) );

if ( $post->ID === $active_publisher_logo_id ) {
$media_states[] = __( 'Web Stories Publisher Logo', 'web-stories' );
}
return $media_states;
}
}
29 changes: 28 additions & 1 deletion tests/phpunit/integration/tests/Admin/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@
*/
class Admin extends DependencyInjectedTestCase {

/**
* Settings for test.
*
* @var \Google\Web_Stories\Settings
*/
private $settings;

/**
* Admin user for test.
*
Expand All @@ -47,6 +54,7 @@ class Admin extends DependencyInjectedTestCase {
protected static $post_id;

public static function wpSetUpBeforeClass( $factory ): void {

self::$admin_id = $factory->user->create(
[ 'role' => 'administrator' ]
);
Expand Down Expand Up @@ -80,7 +88,7 @@ public static function wpSetUpBeforeClass( $factory ): void {

public function set_up(): void {
parent::set_up();

$this->settings = $this->injector->make( \Google\Web_Stories\Settings::class );
$this->instance = $this->injector->make( \Google\Web_Stories\Admin\Admin::class );
}

Expand Down Expand Up @@ -222,4 +230,23 @@ public function test_prefill_post_title_no_texturize(): void {

$this->assertSame( 'Story - Test', $result );
}

/**
* @covers ::media_states
*/
public function test_media_states_no_active_logo(): void {
$post = self::factory()->post->create_and_get( [] );
$result = $this->instance->media_states( [], $post );
$this->assertEqualSets( [], $result );
}

/**
* @covers ::media_states
*/
public function test_media_states_with_active_logo(): void {
$post = self::factory()->post->create_and_get( [] );
$this->settings->update_setting( $this->settings::SETTING_NAME_ACTIVE_PUBLISHER_LOGO, $post->ID );
$result = $this->instance->media_states( [], $post );
$this->assertEqualSets( [ 'Web Stories Publisher Logo' ], $result );
}
}