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

WP.com Posts: Show thumbnail on the posts list screen #19777

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/renovate.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
"automattic/jetpack-status",
"automattic/jetpack-sync",
"automattic/jetpack-terms-of-service",
"automattic/jetpack-tracking"
"automattic/jetpack-tracking",
"automattic/jetpack-wpcom-posts"
],
"enabled": false
},
Expand Down
13 changes: 13 additions & 0 deletions projects/packages/wpcom-posts/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Files not needed to be distributed in the package.
.gitattributes export-ignore
.github/ export-ignore
package.json export-ignore

# Files to include in the mirror repo, but excluded via gitignore
# /src/js/example.min.js production-include

# Files to exclude from the mirror repo, but included in the monorepo.
.gitignore production-exclude
changelog/** production-exclude
phpunit.xml.dist production-exclude
tests/** production-exclude
3 changes: 3 additions & 0 deletions projects/packages/wpcom-posts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
vendor/
node_modules/
wordpress/
7 changes: 7 additions & 0 deletions projects/packages/wpcom-posts/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Changelog

All notable changes to this project will be documented in this file.

The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

20 changes: 20 additions & 0 deletions projects/packages/wpcom-posts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# wpcom-posts

Enhancements for the "Posts" screens on WordPress.com sites.

## How to install wpcom-posts

### Installation From Git Repo

## Contribute

## Get Help

## Security

Need to report a security vulnerability? Go to [https://automattic.com/security/](https://automattic.com/security/) or directly to our security bug bounty site [https://hackerone.com/automattic](https://hackerone.com/automattic).

## License

wpcom-posts is licensed under [GNU General Public License v2 (or later)](./LICENSE.txt)

21 changes: 21 additions & 0 deletions projects/packages/wpcom-posts/actions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe this is something that should be within the Jetpack plugin instead, or within wpcomsh, and then this package would only be the package itself?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or something that is used with the Config package.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer to leave this out of the Jetpack plugin for various reasons:

  • This is part of a technical spike exploring how we can build a package which will be exclusively consumed by WP.com Simple and Atomic sites.
  • I don't see a clear fit for this feature in Jetpack (especially after branding it as a solution for Security, Backups, Performance, and Growth). Even if we decide there is a fit, there is not certainty that all future features that will be bundled in this package may fit.

I do see value in setting up this via the Config package (which will be done on the WP.com sites), so I'll explore that approach.

/**
* Action Hooks for the WordPress.com's Posts enhancements.
*
* @package automattic/jetpack-wpcom-posts
*/

namespace Automattic\Jetpack\WPcom\Posts;

if ( ! defined( 'ABSPATH' ) ) {
return;
}

/**
* Initializes the thumbnail enhancements.
*/
function setup_thumbnail() {
new Thumbnail();
}

add_action( 'init', __NAMESPACE__ . '\setup_thumbnail' );
Empty file.
54 changes: 54 additions & 0 deletions projects/packages/wpcom-posts/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"name": "automattic/jetpack-wpcom-posts",
"description": "Enhancements for the \"Posts\" screens on WordPress.com sites.",
"type": "library",
"license": "GPL-2.0-or-later",
"require": {},
"require-dev": {
"yoast/phpunit-polyfills": "0.2.0",
"automattic/jetpack-changelogger": "^1.1",
"automattic/wordbless": "dev-master"
},
"autoload": {
"files": [
"actions.php"
],
"classmap": [
"src/"
]
},
"scripts": {
"phpunit": [
"@composer update",
"./vendor/phpunit/phpunit/phpunit --colors=always"
],
"test-coverage": [
"@composer update",
"phpdbg -d memory_limit=2048M -d max_execution_time=900 -qrr ./vendor/bin/phpunit --coverage-clover \"$COVERAGE_DIR/clover.xml\""
],
"test-php": [
"@composer phpunit"
],
"post-update-cmd": "php -r \"copy('vendor/automattic/wordbless/src/dbless-wpdb.php', 'wordpress/wp-content/db.php');\""
},
"repositories": [
{
"type": "path",
"url": "../*",
"options": {
"monorepo": true
}
}
],
"minimum-stability": "dev",
"prefer-stable": true,
"extra": {
"mirror-repo": "Automattic/jetpack-wpcom-posts",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be okay to enable autotagger for this package.

Suggested change
"mirror-repo": "Automattic/jetpack-wpcom-posts",
"autotagger": true,
"mirror-repo": "Automattic/jetpack-wpcom-posts",

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Didn't know about that, thanks! I guess we can maybe include an additional step in the Generate Wizard asking about this option, so it increases the visibility of this feature.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sdixon194 Do you think we could add this to the generate command?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing, we can do that! Opened an issue to track it here: #19816

"changelogger": {
"link-template": "https://github.com/Automattic/jetpack-wpcom-posts/compare/v${old}...v${new}"
},
"branch-alias": {
"dev-master": "0.1.x-dev"
}
}
}
14 changes: 14 additions & 0 deletions projects/packages/wpcom-posts/license.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
This program is free software; you can redistribute it and/or
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We haven't typically added licenses to our packages until now, but you make a good point. I've asked in p2y3YZ-4v3-p2

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, this was added by the Generate Wizard.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems @sdixon194 planned for it already 🥳

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup! I commented on @kraftbj 's issue here in case we needed to update it: #19802

modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.


You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
29 changes: 29 additions & 0 deletions projects/packages/wpcom-posts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"private": true,
"description": "Enhancements for the \"Posts\" screens on WordPress.com sites.",
"homepage": "https://jetpack.com",
"bugs": {
"url": "https://github.com/Automattic/jetpack/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/Automattic/jetpack.git"
},
"license": "GPL-2.0-or-later",
"author": "Automattic",
"scripts": {
"build": "echo 'Not implemented.",
"build-js": "echo 'Not implemented.",
"build-production": "echo 'Not implemented.",
"build-production-js": "echo 'Not implemented.",
"clean": "true",
"distclean": "rm -rf node_modules && yarn clean",
"install-if-deps-outdated": "yarn install --check-files --production=false --frozen-lockfile",
"validate-es5": "eslint --parser-options=ecmaVersion:5 --no-eslintrc --no-ignore"
},
"devDependencies": {},
"engines": {
"node": "^14.16.0",
"yarn": "^1.3.2"
}
}
17 changes: 17 additions & 0 deletions projects/packages/wpcom-posts/phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<phpunit bootstrap="tests/php/bootstrap.php" backupGlobals="false" colors="true">
<testsuites>
<testsuite name="main">
<directory prefix="test" suffix=".php">tests/php</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">.</directory>
<exclude>
<directory suffix=".php">tests</directory>
<directory suffix=".php">vendor</directory>
<directory suffix=".php">wordpress</directory>
</exclude>
</whitelist>
</filter>
</phpunit>
69 changes: 69 additions & 0 deletions projects/packages/wpcom-posts/src/class-thumbnail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
/**
* Thumbnail enhancements for the "Posts" screens on WordPress.com sites.
*
* @package automattic/jetpack-wpcom-posts
*/

namespace Automattic\Jetpack\WPcom\Posts;

/**
* Class Thumbnail.
*/
class Thumbnail {
/**
* Thumbnail constructor.
*/
public function __construct() {
add_filter( 'manage_posts_columns', array( $this, 'add_posts_column_header' ) );
add_filter( 'manage_pages_columns', array( $this, 'add_posts_column_header' ) );
add_action( 'manage_posts_custom_column', array( $this, 'display_posts_column_content' ), 10, 2 );
add_action( 'manage_pages_custom_column', array( $this, 'display_posts_column_content' ), 10, 2 );
add_action( 'admin_print_styles-edit.php', array( $this, 'load_columns_css' ) );
}

/**
* Adds a new column header for displaying the thumbnail of a post.
*
* @param array $columns An array of column names.
* @return array An array of column names.
*/
public function add_posts_column_header( $columns ) {
// Place if before author.
$pos = array_search( 'author', array_keys( $columns ), true );
if ( ! is_int( $pos ) ) {
return $columns;
}
$chunks = array_chunk( $columns, $pos, true );
$chunks[0]['thumbnail'] = ''; // Deliberately empty.

return call_user_func_array( 'array_merge', $chunks );
}

/**
* Displays the thumbnail content.
*
* @param string $column The name of the column to display.
* @param int $post_id The current post ID.
*/
public function display_posts_column_content( $column, $post_id ) {
if ( 'thumbnail' !== $column ) {
return;
}

echo get_the_post_thumbnail( $post_id, array( 50, 50 ), array( 'style' => 'height: auto;' ) );
}

/**
* Load CSS needed for the thumbnail column.
*/
public function load_columns_css() {
?>
<style type="text/css">
.fixed .column-thumbnail {
width: 5em;
}
</style>
<?php
}
}
16 changes: 16 additions & 0 deletions projects/packages/wpcom-posts/tests/php/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
/**
* Bootstrap.
*
* @package automattic/
*/

/**
* Include the composer autoloader.
*/
require_once __DIR__ . '/../../vendor/autoload.php';

/**
* Load WorDBless
*/
\WorDBless\Load::load();
78 changes: 78 additions & 0 deletions projects/packages/wpcom-posts/tests/php/test-class-thumbnail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php //phpcs:ignore WordPress.Files.FileName.InvalidClassFileName
/**
* Test methods from Automattic\Jetpack\WPcom\Posts\Thumbnail
*
* @package automattic/jetpack-wpcom-posts
*/

namespace Automattic\Jetpack\WPcom\Posts;

use WorDBless\BaseTestCase;

/**
* Class Test_Thumbnail
*/
class Test_Thumbnail extends BaseTestCase {
/**
* Core class used to implement displaying posts in a list table.
*
* @var WP_Posts_List_Table
*/
protected $table;

/**
* Test post.
*
* @var WP_Post
*/
protected $post;

/**
* Setup runs before each test.
*
* @before
*/
public function set_up() {
new Thumbnail();
$this->table = _get_list_table( 'WP_Posts_List_Table', array( 'screen' => 'edit-page' ) );

$this->post = wp_insert_post(
array(
'post_status' => 'publish',
'post_title' => 'Post title',
'post_content' => 'Post content',
'post_excerpt' => 'Post excerpt',
'post_type' => 'post',
)
);

add_filter( 'post_thumbnail_html', array( $this, 'mock_post_thumbnail_html' ), 10, 4 );
}

public function tear_down() {
remove_filter( 'post_thumbnail_html', array( $this, 'mock_post_thumbnail_html' ) );
}

public function mock_post_thumbnail_html( $html, $post_id, $size, $attr ) {
$width = $size[0];
$height = $size[1];
$style = $attr['style'];

return "My thumbnail of $width x $height with a $style style";
}

/**
* Checks that a new column header for thumbnails is added to the posts list table.
*/
public function test_thumbnail_column_header() {
$columns = $this->table->get_columns();
$this->assertSame( '', $columns['thumbnail'] );
}

/**
* Checks that the post thumbnail is displayed in the new column cell new.
*/
public function test_thumbnail_column_content() {
$this->table->column_default( 'thumbnail' );
}
}