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

Add an image thumbnail to wp-admin post list. #20720

Merged
merged 122 commits into from
Sep 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
122 commits
Select commit Hold shift + click to select a range
a7fbf58
[not verified] First approach
retrofox Aug 4, 2021
d265817
[not verified] posts-page: start to rendering posts list with components
retrofox Aug 4, 2021
669d90f
[not verified] add package to renovate github file
retrofox Aug 5, 2021
3bb4eac
[not verified] wp-admin-plus: add composer compiling scripts
retrofox Aug 5, 2021
575e3e8
[not verified] doc
retrofox Aug 5, 2021
115ab9e
[not verified] rendering date button in date column
retrofox Aug 5, 2021
655fd84
[not verified] eslint: fix override eslint path
retrofox Aug 5, 2021
213ce60
[not verified] set post date optimistically
retrofox Aug 6, 2021
5bd01b8
[not verified] update github files
retrofox Aug 6, 2021
f1043c1
[not verified] wp-admin-plus: changelog
retrofox Aug 6, 2021
7066efa
[not verified] remove codeclimate file
retrofox Aug 6, 2021
8a66e58
[not verified] remove editorconfig file
retrofox Aug 6, 2021
c64c255
[not verified] cleaning...
retrofox Aug 6, 2021
134102d
[not verified] wp-admin-plus: set selected day color
retrofox Aug 6, 2021
a2c9e4f
[not verified] twaeking post date label
retrofox Aug 6, 2021
13970f4
[not verified] wp-admin-plus: refresh post status once saves
retrofox Aug 6, 2021
f5094b2
[not verified] move post-date cmp to its own folder
retrofox Aug 6, 2021
ab52d40
[not verified] add PostStatusLabel component
retrofox Aug 6, 2021
4a372e3
[not verified] refact -> add usePost hook
retrofox Aug 6, 2021
8b0b7d4
[not verified] usePost: refactoring hook
retrofox Aug 9, 2021
ba3bba8
[not verified] add @wordpress/eslint-plugin dev dep
retrofox Aug 9, 2021
69eb8f7
[not verified] post status label: pass status to hook
retrofox Aug 9, 2021
39a1c06
[not verified] Populate post status with espcial cases. Tidy.
retrofox Aug 9, 2021
44d221a
[not verified] refactor pick and organize client data
retrofox Aug 9, 2021
5a3e3d0
[not verified] update dependencies
retrofox Aug 9, 2021
d86b04f
[not verified] add eslint config file
retrofox Aug 9, 2021
665df17
[not verified] requier `any` post status
retrofox Aug 9, 2021
87fce19
[not verified] add "@babel/runtime": "7.14.0" dependency
retrofox Aug 9, 2021
c903825
[not verified] rename package to wp-admin-posts-list-page
retrofox Aug 10, 2021
94080e9
[not verified] rename (again clap !) package name
retrofox Aug 11, 2021
f74e5b1
[not verified] communicate: init component
retrofox Aug 11, 2021
f844cae
[not verified] [not verified]
retrofox Aug 12, 2021
b07c920
[not verified] improve jsdoc
retrofox Aug 12, 2021
6aca7bd
[not verified] Copied thumbnail code from add/wpcom-posts-package to …
Aug 16, 2021
08717b8
[not verified] rename package name
retrofox Aug 17, 2021
afb43ce
[not verified] rename changelog filename
retrofox Aug 17, 2021
50c0520
[not verified] remove post-date stuff
retrofox Aug 17, 2021
f877e94
[not verified] rename to Notice component. doc.
retrofox Aug 17, 2021
505228f
[not verified] inject post-feature-image container
retrofox Aug 17, 2021
f4b91b4
[not verified] Add basic FeatureImage component
retrofox Aug 17, 2021
6441d41
[not verified] remove thumbnail class
retrofox Aug 18, 2021
a5d9d12
[not verified] First attempt to get the first image found in a post i…
Aug 17, 2021
93c188f
[not verified] check and get post image
retrofox Aug 18, 2021
91e12e2
[not verified] pick images inspecting post content
retrofox Aug 18, 2021
0aeddab
[not verified] If no featured image is set, get the first image on th…
Aug 18, 2021
a42ac66
[not verified] Updated comments to pass linter.
DustyReagan Aug 19, 2021
0d19e72
[not verified] Apply suggestions from code review
DustyReagan Aug 20, 2021
984c59f
[not verified] Added composer.lock & .jshintrc to .gitignore. Replace…
DustyReagan Aug 20, 2021
9dd1f8b
[not verified] Fixed infinite "jetpack build packages/post-list" buil…
DustyReagan Aug 20, 2021
2abe5da
[not verified] Rebase
DustyReagan Aug 20, 2021
59ec5e8
[not verified] Added content to changelog file. Alphabetized renovate…
DustyReagan Aug 20, 2021
a91daf0
[not verified] Upgrade @wordpress/scripts to v18, and adjust build sc…
pablinos Aug 23, 2021
d7833cd
[not verified] add no-featured-image image
retrofox Aug 23, 2021
1f7d8b0
[not verified] change style by image when no-featured
retrofox Aug 23, 2021
d582ea3
[not verified] add a super basic Tooltip when no featured image
retrofox Aug 23, 2021
9ef4e49
[not verified] use a div to wrap no-featured-image
retrofox Aug 23, 2021
4438258
[not verified] Add Assets package as dependency
allilevine Aug 23, 2021
f57d060
[not verified] tweak tooltip style
retrofox Aug 23, 2021
340d7f7
[not verified] clean CSS code
retrofox Aug 24, 2021
967e756
[not verified] add basic eslint config file
retrofox Aug 24, 2021
585b0fd
[not verified] Updated get_featured_or_first_post_image to find the c…
DustyReagan Aug 24, 2021
bc87fd2
[not verified] update eslint-plugin version
retrofox Aug 24, 2021
d1589ef
[not verified] Added unit test stub files.
DustyReagan Aug 24, 2021
195f438
[not verified] Add unit test support
kraftbj Aug 24, 2021
44099cb
[not verified] Updated our deprecated XML configuration scheme using …
DustyReagan Aug 25, 2021
e1e6cb0
[not verified] Refactor to use global data object, and remove unused …
pablinos Aug 25, 2021
46039eb
[not verified] Remove unused eslintrc file
pablinos Aug 25, 2021
5732d4c
[not verified] update building scripts
retrofox Aug 25, 2021
dabd87e
[not verified] minor doc improvement
retrofox Aug 25, 2021
3733e82
[not verified] removing post-list dependency
retrofox Aug 25, 2021
9e30b07
[not verified] Moved image get function to its own class and broke it…
DustyReagan Aug 25, 2021
5060ba5
[not verified] Refactor feature flag query var.
pablinos Aug 25, 2021
7355580
[not verified] add @wordpress/icons dependency
retrofox Aug 25, 2021
cad099a
[not verified] replace bitmap image by icon when no thumb
retrofox Aug 25, 2021
6f8ced5
[not verified] build for dev without watching changes
retrofox Aug 25, 2021
c5e1afc
[not verified] Added new tests for Post_Image.
DustyReagan Aug 25, 2021
abfd8cd
[not verified] change image icon by a custom one
retrofox Aug 25, 2021
c832854
[not verified] Renamed Post_Image class to Post_Thumbnail. Added phpc…
DustyReagan Aug 25, 2021
de73641
[not verified] Ensure we only activate when thumbnails are supported
pablinos Aug 25, 2021
e3dd648
[not verified] Renamed Admin class to Post_List.
DustyReagan Aug 25, 2021
828602d
[not verified] Adding project structure requirments.
pablinos Aug 26, 2021
a3ee92d
[not verified] Update package version number
pablinos Aug 26, 2021
c1271fc
[not verified] Correct .gitkeep location
pablinos Aug 26, 2021
137706b
[not verified] add alt image property
retrofox Aug 26, 2021
1bff25b
[not verified] remove post-list dependency from jetpack
retrofox Aug 26, 2021
c5fb220
[not verified] remove code about post date
retrofox Aug 26, 2021
ab60e59
[not verified] change package description
retrofox Aug 26, 2021
2d2bba5
[not verified] improve getting image alt value
retrofox Aug 26, 2021
93bf71d
[not verified] Refactor how the package is initialized and remove act…
allilevine Aug 26, 2021
f83135b
[not verified] Update projects/packages/post-list/.eslintrc.js
retrofox Aug 26, 2021
80bba35
[not verified] Update projects/packages/post-list/.eslintrc.js
retrofox Aug 26, 2021
5179ad0
[not verified] Update projects/packages/post-list/.eslintrc.js
retrofox Aug 26, 2021
1ba2c03
[not verified] Update projects/packages/post-list/src/components/feat…
retrofox Aug 26, 2021
2c62cb7
[not verified] Update projects/packages/post-list/composer.json
retrofox Aug 26, 2021
53955f3
[not verified] Update projects/packages/post-list/src/class-post-list…
retrofox Aug 26, 2021
afc2f64
[not verified] Remove is_wp_admin_posts_list_page check
allilevine Aug 30, 2021
462ae85
[not verified] remove PACKAGE_VERSION class constant
retrofox Aug 30, 2021
1f2bf6c
[not verified] remove unused packages
retrofox Aug 30, 2021
7534495
[not verified] Removed actions.php reference.
DustyReagan Aug 31, 2021
4e44b72
[not verified] Added wp_set_script_translations.
pablinos Aug 31, 2021
046c127
[not verified] Implemented @dataProvider in test-post-thumbnail.php. …
DustyReagan Aug 31, 2021
ebc6c8b
[not verified] Defined $image_alt = null. Updated comment.
DustyReagan Aug 31, 2021
f99efb9
[not verified] Added /wordpress to .gitignore.
DustyReagan Aug 31, 2021
40a688e
[not verified] Rebase, updated lock files, update noded version in pa…
DustyReagan Aug 31, 2021
4fdca82
[not verified] Updating unit test configuration.
DustyReagan Aug 31, 2021
7c8020a
[not verified] Updated yoast/phpunit-polyfills version number. Edited…
DustyReagan Sep 1, 2021
ce0721c
[not verified] rebase
retrofox Sep 1, 2021
7b40518
[not verified] create root element for the client app
retrofox Sep 1, 2021
7a9d9c9
[not verified] re-implement the app using portal
retrofox Sep 1, 2021
3abf339
[not verified] replace React by vanilla JS
retrofox Sep 1, 2021
028be08
[not verified] clean compiling JS stuff
retrofox Sep 1, 2021
65cb4ca
[not verified] Updated unit tests, comments, and refactored namespace.
DustyReagan Sep 1, 2021
53ff0e6
[not verified] Removed .eslintrc.js.
DustyReagan Sep 2, 2021
ecc7601
[not verified] Removed post-list dependency from Jetpack.
DustyReagan Sep 2, 2021
f171845
[not verified] Attempt to remove projects/plugins/jetpack/composer.lo…
DustyReagan Sep 2, 2021
203ff22
[not verified] Removed changelog since we removed the dependecy.
DustyReagan Sep 2, 2021
501ad0c
[not verified] Updated Readme.md
DustyReagan Sep 7, 2021
454e4ba
[not verified] Added package version number.
DustyReagan Sep 7, 2021
46ecce8
[not verified] Escape return values just in case.
DustyReagan Sep 7, 2021
9bffd30
[not verified] Check for empty $post_content first and return null if…
DustyReagan Sep 7, 2021
c307802
[not verified] Updated version number.
DustyReagan Sep 7, 2021
69c95c8
Apply suggestions from code review
DustyReagan Sep 8, 2021
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
1 change: 1 addition & 0 deletions .github/renovate.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"automattic/jetpack-options",
"automattic/jetpack-partner",
"automattic/jetpack-password-checker",
"automattic/jetpack-post-list",
"automattic/jetpack-redirect",
"automattic/jetpack-roles",
"automattic/jetpack-status",
Expand Down
16 changes: 16 additions & 0 deletions projects/packages/post-list/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Files not needed to be distributed in the package.
.gitattributes export-ignore
.github/ export-ignore
package.json export-ignore
webpack.config.js export-ignore
gulpfile.babel.js export-ignore
.babelrc export-ignore

# Files to include in the mirror repo
/build/** production-include

# Files to exclude from the mirror repo
/changelog/** production-exclude
/.eslintrc.js production-exclude
.gitignore production-exclude
_inc/** production-exclude
6 changes: 6 additions & 0 deletions projects/packages/post-list/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/build
/node_modules
/.cache
/wordpress
composer.lock
.jshintrc
6 changes: 6 additions & 0 deletions projects/packages/post-list/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# 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).
35 changes: 35 additions & 0 deletions projects/packages/post-list/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Jetpack Post List Package

Enhance the classic view of the Admin section of your WordPress site.

## How to Use

Package is published in [Packagist](https://packagist.org/packages/automattic/jetpack-post-list).

Use composer to add the package to your project:
```bash
composer add automattic/jetpack-post-list
```

Then you need to initialize it on the `admin_init` hook:

```php
add_action( 'admin_init', array( '\Automattic\Jetpack\Post_List\Post_List', 'configure' ) );
```

## Development

### Production
```bash
jetpack build -p packages/post-list
```

### Development
```bash
jetpack build packages/post-list
```

### Development Watching Mode 👀
```bash
jetpack watch packages/post-list
```
Empty file.
4 changes: 4 additions & 0 deletions projects/packages/post-list/changelog/add-post-list-package
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: added

Add the new Post List package to Jetpack project
57 changes: 57 additions & 0 deletions projects/packages/post-list/composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"name": "automattic/jetpack-post-list",
"description": "Enhance the classic view of the Admin section of your WordPress site",
"type": "library",
"license": "GPL-2.0-or-later",
"require": {
"automattic/jetpack-assets": "^1.11"
},
"require-dev": {
"automattic/wordbless": "@dev",
"automattic/jetpack-changelogger": "^1.2",
"yoast/phpunit-polyfills": "1.0.1"
},
"autoload": {
"classmap": [
"src/"
]
},
"repositories": [
{
"type": "path",
"url": "../*",
"options": {
"monorepo": true
}
}
],
"scripts": {
"phpunit": [
"./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 update",
"@composer phpunit"
],
"post-update-cmd": "php -r \"copy('vendor/automattic/wordbless/src/dbless-wpdb.php', 'wordpress/wp-content/db.php');\""
},
"minimum-stability": "dev",
"prefer-stable": true,
"extra": {
"autotagger": true,
"mirror-repo": "automattic/jetpack-post-list",
"version-constants": {
"::PACKAGE_VERSION": "src/class-post-list.php"
},
"changelogger": {
"link-template": "https://github.com/automattic/jetpack-post-list/compare/v${old}...v${new}"
},
"branch-alias": {
"dev-master": "0.1.x-dev"
}
}
}
17 changes: 17 additions & 0 deletions projects/packages/post-list/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>
122 changes: 122 additions & 0 deletions projects/packages/post-list/src/class-post-list.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php
/**
* The Post List Admin Area.
*
* @package automattic/jetpack-post-list
*/

namespace Automattic\Jetpack\Post_List;

use Automattic\Jetpack\Assets;

/**
* The Post_List Admin Area
*/
class Post_List {

const PACKAGE_VERSION = '0.1.0-alpha';

/**
* The configuration method that is called from the jetpack-config package.
*/
public static function configure() {
$post_list = self::get_instance();
$post_list->register();
}

/**
* Initialize the Post List UI.
*
* @return Post_List Post_List instance.
*/
public static function get_instance() {
return new Post_List();
}

/**
* Sets up Post List action callbacks if needed.
*/
public function register() {
if ( ! did_action( 'jetpack_on_posts_list_init' ) ) {
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
add_action( 'in_admin_footer', array( $this, 'create_app_root_element' ) );

/**
* Action called after initializing Post_List Admin resources.
*
* @since $$next-version$$
*/
do_action( 'jetpack_on_posts_list_init' );
}
}

/**
* Enqueue scripts depending on the post-list query var.
*
* @param string $hook Page hook.
*/
public function enqueue_scripts( $hook ) {
if ( 'edit.php' === $hook ) {
$plugin_path = Assets::get_file_url_for_environment( './index.js', './index.js', __FILE__ );

wp_enqueue_script(
'jetpack_posts_list_ui_script',
pablinos marked this conversation as resolved.
Show resolved Hide resolved
$plugin_path,
array(),
self::PACKAGE_VERSION,
true
);

wp_enqueue_style(
'jetpack_posts_list_ui_style',
plugin_dir_url( __DIR__ ) . './src/style.css',
array(),
self::PACKAGE_VERSION
);

wp_style_add_data(
'jetpack_posts_list_ui_style',
'rtl',
plugin_dir_url( __DIR__ ) . 'build/style.rtl.css'
);

wp_set_script_translations( 'jetpack_posts_list_ui_script', 'jetpack' );

add_action( 'admin_footer', array( $this, 'print_post_data' ) );
}
}

/**
* Outputs a JSON blob to the global `wp_admin_posts` variable, for use
* by the JS application
*/
public function print_post_data() {
global $wp_query;

if ( ! post_type_supports( $wp_query->query['post_type'], 'thumbnail' ) ) {
return;
}

$post_data = array_map(
function ( $post ) {
$thumbnail = Post_Thumbnail::get_post_thumbnail( $post );
return array(
'id' => $post->ID,
'type' => $post->post_type,
'featured_image' => $thumbnail,
DustyReagan marked this conversation as resolved.
Show resolved Hide resolved
);
},
$wp_query->posts
);
wp_add_inline_script( 'jetpack_posts_list_ui_script', 'var wpAdminPosts = ' . wp_json_encode( $post_data ) );
}

/**
* Add a placeholder element for the
* to mount the client app (root element).
*/
public function create_app_root_element() {
echo '<div id="wp-post-list-app" style="display: none;"></div>';
}
}

107 changes: 107 additions & 0 deletions projects/packages/post-list/src/class-post-thumbnail.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php
/**
* This file contains the Post_Thumbnail class used for finding a suitable thumbnail for a post.
*
* @package automattic/jetpack-post-list
*/

namespace Automattic\Jetpack\Post_List;

/**
* The Post_Thumbnail class contains methods to find and return a suitable thumbnail for a post.
*/
class Post_Thumbnail {
/**
* Returns the featured image thumbnail or if no featured image is set, return the first image in the post. If
* neither exists returns the image array with null values.
*
* @param object $post The current post.
* @return array The thumbnail image id and URLs
*/
public static function get_post_thumbnail( $post ) {
$image_id = null;
$image_url = null;
$image_thumb = null;
$image_alt = null;

$post_id = $post->ID;

// If a featured image exists for the post, use that thumbnail.
if ( has_post_thumbnail( $post_id ) ) {
$image_id = get_post_thumbnail_id( $post_id );
$image_url = get_the_post_thumbnail_url( $post_id );
$image_thumb = get_the_post_thumbnail_url( $post_id, array( 50, 50 ) );
$image_alt = get_post_meta( $post_id, '_wp_attachment_image_alt', true );
} else {
// If a featured image does not exist look for the first "media library" hosted image on the post.
$attachment_id = self::get_first_image_id_from_post_content( $post->post_content );

if ( null !== $attachment_id ) {
$image_id = $attachment_id;
$image_url = wp_get_attachment_image_url( $attachment_id, 'full-size' );
$image_thumb = wp_get_attachment_image_url( $attachment_id, array( 50, 50 ) );
$image_alt = get_post_meta( $attachment_id, '_wp_attachment_image_alt', true );
}
}

// Escape values just in case.
return array(
'id' => esc_attr( $image_id ),
'url' => esc_url( $image_url ),
'thumb' => esc_url( $image_thumb ),
'alt' => esc_attr( $image_alt ),
);
}

/**
* Looks for the first image in the post content containing a class value of 'wp-image-{$attachment_id}' and
* returns the $attachment_id from that class value.
*
* @param string $post_content The current post's HTML content.
* @return int The image attachment id.
*/
public static function get_first_image_id_from_post_content( $post_content ) {
// If $post_content does not contain a value of substance, return null right away and avoid trying to parse it.
if ( empty( $post_content ) ) {
return null;
}

$attachment_id = null;
$dom = new \DOMDocument();

// libxml_use_internal_errors(true) silences PHP warnings and errors from malformed HTML in loadHTML().
// you can consult libxml_get_last_error() or libxml_get_errors() to check for errors if needed.
libxml_use_internal_errors( true );
$dom->loadHTML( $post_content );

// Media library images have a class attribute value containing 'wp-image-{$attachment_id}'.
// Use DomXPath to parse the post content and get the first img tag containing 'wp-image-' as a class value.
$class_name = 'wp-image-';
$dom_x_path = new \DomXPath( $dom );
$nodes = $dom_x_path->query( "//img[contains(@class, '$class_name')]/@class" );

if ( $nodes->length > 0 ) {
// Get the class attribute value of the 1st image node (aka index 0).
$class_value = $nodes[0]->value;

// Ignore all class attribute values except 'wp-image{$attachment_id}'.
// Regex english translation: Look for a word \b, that does not start or end with a hyphen (?!-), that
// starts with 'wp-image-', and ends with a number of any length \d+.
$class_name_found = preg_match( '/\b(?!-)wp-image-\d+(?!-)\b/', $class_value, $class_value );

if ( $class_name_found ) {
// Get the $attachment_id from the end of the class name value.
$attachment_id = str_replace( $class_name, '', $class_value[0] );

// If the ID we found is numeric, cast it as an int. Else, make it null.
if ( is_numeric( $attachment_id ) ) {
$attachment_id = (int) $attachment_id;
} else {
$attachment_id = null;
}
}
}

return $attachment_id;
}
}
Loading