Skip to content

Commit

Permalink
Merge branch 'develop' into php-testing
Browse files Browse the repository at this point in the history
  • Loading branch information
roborourke authored Jul 25, 2024
2 parents cbf9e8f + b6e9b07 commit ae65e9b
Show file tree
Hide file tree
Showing 15 changed files with 273 additions and 119 deletions.
24 changes: 12 additions & 12 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ jobs:
runs-on: ubuntu-20.04
steps:
- name: Checkout repository
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Setup Node
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version-file: '.nvmrc'

- name: Install PHP
uses: shivammathur/setup-php@2.7.0
uses: shivammathur/setup-php@v2
with:
php-version: 7.4
extensions: mysqli, xmlwriter
Expand All @@ -28,13 +28,13 @@ jobs:
- name: Get Composer Cache Directory
id: composer-cache-dir
run: |
echo "::set-output name=dir::$(composer config cache-files-dir)"
echo "DIR=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
- name: Cache PHP Dependencies
id: composer-cache
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: ${{ steps.composer-cache-dir.outputs.dir }}
path: ${{ steps.composer-cache-dir.outputs.DIR }}
key: ${{ runner.os }}-composer-7.4-${{ hashFiles('composer.lock') }}

- name: Install PHP Dependencies
Expand All @@ -43,16 +43,16 @@ jobs:
- name: Get npm cache directory
id: npm-cache-dir
run: |
echo "::set-output name=dir::$(npm config get cache)"
echo "::set-output name=npm-version::$(npm -v)"
echo "::set-output name=node-version::$(node -v)"
echo "DIR=$(npm config get cache)" >> $GITHUB_OUTPUT
echo "NPM_VERSION=$(npm -v)" >> $GITHUB_OUTPUT
echo "NODE_VERSION=$(node -v)" >> $GITHUB_OUTPUT
- name: Cache JS Dependencies
id: npm-cache
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: ${{ steps.npm-cache-dir.outputs.dir }}
key: ${{ runner.os }}-npm-${{ steps.npm-cache-dir.outputs.node-version }}-${{ steps.npm-cache-dir.outputs.npm-version }}-${{ hashFiles('package-lock.json') }}
path: ${{ steps.npm-cache-dir.outputs.DIR }}
key: ${{ runner.os }}-npm-${{ steps.npm-cache-dir.outputs.NODE_VERSION }}-${{ steps.npm-cache-dir.outputs.NPM_VERSION }}-${{ hashFiles('package-lock.json') }}

- name: Install JS Dependencies
run: npm install --legacy-peer-deps
Expand Down
16 changes: 8 additions & 8 deletions .github/workflows/js-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ jobs:
runs-on: ubuntu-20.04
steps:
- name: Checkout repository
uses: actions/checkout@v1
uses: actions/checkout@v4

- name: Setup Node
uses: actions/setup-node@v3
uses: actions/setup-node@v4
with:
node-version-file: '.nvmrc'

Expand All @@ -29,16 +29,16 @@ jobs:
- name: Get npm cache directory
id: npm-cache-dir
run: |
echo "::set-output name=dir::$(npm config get cache)"
echo "::set-output name=npm-version::$(npm -v)"
echo "::set-output name=node-version::$(node-v)"
echo "DIR=$(npm config get cache)" >> $GITHUB_OUTPUT
echo "NPM_VERSION=$(npm -v)" >> $GITHUB_OUTPUT
echo "NODE_VERSION=$(node -v)" >> $GITHUB_OUTPUT
- name: Cache Dependencies
id: npm-cache
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: ${{ steps.npm-cache-dir.outputs.dir }}
key: ${{ runner.os }}-npm-${{ steps.npm-cache-dir.outputs.node-version }}-${{ steps.npm-cache-dir.outputs.npm-version }}-${{ hashFiles('package-lock.json') }}
path: ${{ steps.npm-cache-dir.outputs.DIR }}
key: ${{ runner.os }}-npm-${{ steps.npm-cache-dir.outputs.NODE_VERSION }}-${{ steps.npm-cache-dir.outputs.NPM_VERSION }}-${{ hashFiles('package-lock.json') }}

- name: Install Dependencies
run: npm install --legacy-peer-deps
Expand Down
12 changes: 6 additions & 6 deletions .github/workflows/php-standards.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ jobs:
runs-on: ubuntu-20.04
steps:
- name: Checkout repository
uses: actions/checkout@v1
uses: actions/checkout@v4

- name: Install PHP
uses: shivammathur/setup-php@2.7.0
uses: shivammathur/setup-php@v2
with:
php-version: '7.4'
coverage: none
Expand All @@ -31,21 +31,21 @@ jobs:
- name: Get Composer Cache Directory
id: composer-cache-dir
run: |
echo "::set-output name=dir::$(composer config cache-files-dir)"
echo "DIR=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
- name: Cache PHP Dependencies
id: composer-cache
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: ${{ steps.composer-cache-dir.outputs.dir }}
path: ${{ steps.composer-cache-dir.outputs.DIR }}
key: ${{ runner.os }}-composer-7.2-${{ hashFiles('composer.lock') }}

- name: Install PHP Dependencies
run: |
composer install --prefer-dist --no-progress --no-suggest --no-interaction
- name: PHPCS cache
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: tests/cache
key: ${{ runner.os }}-phpcs-7.2-${{ hashFiles('plugin.php') }}
Expand Down
12 changes: 6 additions & 6 deletions .github/workflows/test-nightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ jobs:
runs-on: ubuntu-20.04
steps:
- name: Checkout repository
uses: actions/checkout@v1
uses: actions/checkout@v4

- name: Install PHP
uses: shivammathur/setup-php@2.7.0
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: mysqli, xmlwriter
Expand All @@ -38,19 +38,19 @@ jobs:
- name: Get Composer Cache Directory
id: composer-cache-dir
run: |
echo "::set-output name=dir::$(composer config cache-files-dir)"
echo "DIR=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
- name: Cache PHP Dependencies
id: composer-cache
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: ${{ steps.composer-cache-dir.outputs.dir }}
path: ${{ steps.composer-cache-dir.outputs.DIR }}
key: ${{ runner.os }}-composer-${{ matrix.php }}-${{ hashFiles('composer.lock') }}

- name: Install PHP Dependencies
run: |
composer install --prefer-dist --no-progress --no-suggest --no-interaction --ignore-platform-reqs
composer require --dev --update-with-dependencies --prefer-dist roots/wordpress="dev-nightly" wp-phpunit/wp-phpunit="dev-master"
composer require --dev --update-with-dependencies --prefer-dist roots/wordpress="dev-main" wp-phpunit/wp-phpunit="dev-master"
- name: Run the tests
run: |
Expand Down
12 changes: 6 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ jobs:
- '8.2'
- '8.3'
fail-fast: false
name: WP 6.4 / PHP ${{ matrix.php }}
name: WP 6.6 / PHP ${{ matrix.php }}
runs-on: ubuntu-20.04
steps:
- name: Checkout repository
uses: actions/checkout@v1
uses: actions/checkout@v4

- name: Install PHP
uses: shivammathur/setup-php@2.7.0
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php }}
extensions: mysqli, xmlwriter
Expand All @@ -43,14 +43,14 @@ jobs:
- name: Get Composer Cache Directory
id: composer-cache-dir
run: |
echo "::set-output name=dir::$(composer config cache-files-dir)"
echo "DIR=$(composer config cache-files-dir)" >> $GITHUB_OUTPUT
- name: Cache PHP Dependencies
id: composer-cache
uses: actions/cache@v2
uses: actions/cache@v4
with:
path: ${{ steps.composer-cache-dir.outputs.dir }}
key: ${{ runner.os }}-composer-${{ matrix.php }}-${{ hashFiles('composer.lock') }}-wp6.4
key: ${{ runner.os }}-composer-${{ matrix.php }}-${{ hashFiles('composer.lock') }}-wp6.6

- name: Install PHP Dependencies
run: |
Expand Down
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Authorship

Stable tag: 0.2.16
Stable tag: 0.2.17
Requires at least: 5.4
Tested up to: 6.2
Requires PHP: 7.2
Expand Down Expand Up @@ -61,9 +61,10 @@ _Features without a checkmark are still work in progress._
### For development use

* Clone this repo into your plugins directory
* Install the dependencies:
* Ensure you have Composer v2 and Node v16 installed
* Install the dependencies:
`composer install && npm install`
* Start the dev server:
* Start the dev server:
`npm run start`

## Design Decisions
Expand Down Expand Up @@ -133,7 +134,7 @@ This endpoint allows:

### `authorship` field

This field is added to the endpoint for all suported post types (by default, ones which that have post type support for `author`), for example `wp/v2/posts`. This field is readable and writable and accepts and provides an array of IDs of users attributed to the post.
This field is added to the endpoint for all supported post types (by default, ones which that have post type support for `author`), for example `wp/v2/posts`. This field is readable and writable and accepts and provides an array of IDs of users attributed to the post.

In addition, user objects are embedded in the `_embedded['wp:authorship']` field in the response if `_embed` is set and the authenticated user can list users.

Expand Down
91 changes: 91 additions & 0 deletions inc/class-insert-post-handler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php
/**
* Handler for encapsulating hook callbacks called when inserting posts.
*
* @package authorship
*/

declare( strict_types=1 );

namespace Authorship;

use Exception;
use WP_Post;

/**
* Core class for encapsulating hook callbacks called when inserting posts.
*
* This is needed because state is passed between the callbacks.
*/
class InsertPostHandler {
/**
* @var array<mixed>
*/
private $postarr = [];

/**
* Filters slashed post data just before it is inserted into the database.
*
* @param mixed[] $data An array of slashed, sanitized, and processed post data.
* @param mixed[] $postarr An array of sanitized (and slashed) but otherwise unmodified post data.
* @param mixed $unsanitized_postarr An array (or object that implements array access, like a WP_Post) of
* slashed yet _unsanitized_ and unprocessed post data as originally passed
* to wp_insert_post().
* @return mixed[] An array of slashed, sanitized, and processed post data.
*/
function filter_wp_insert_post_data( array $data, array $postarr, $unsanitized_postarr ) : array {
// Make sure the unsanitized post array is actually an array. Core sometimes passes it as a WP_Post object.
$this->postarr = (array) $unsanitized_postarr;

return $data;
}

/**
* Fires once a post has been saved.
*
* @param int $post_ID Post ID.
* @param WP_Post $post Post object.
* @param bool $update Whether this is an existing post being updated.
*/
function action_wp_insert_post( int $post_ID, WP_Post $post, bool $update ) : void {
$unsanitized_postarr = $this->postarr;

$this->postarr = [];

if ( isset( $unsanitized_postarr['tax_input'] ) && ! empty( $unsanitized_postarr['tax_input'][ TAXONOMY ] ) ) {
return;
}

$existing_authors = get_authors( $post );

if ( $update && ! isset( $unsanitized_postarr[ POSTS_PARAM ] ) && $existing_authors ) {
return;
}

if ( isset( $unsanitized_postarr[ POSTS_PARAM ] ) ) {
$authors = $unsanitized_postarr[ POSTS_PARAM ];
} else {
/**
* Set the default authorship author. Defaults to the original post author.
*
* @param array $authors Authors to add to a post on insert if none have been passed. Default to post author.
* @param WP_Post $post Post object.
*/
$authors = array_filter( apply_filters(
'authorship_default_author',
[ isset( $unsanitized_postarr['post_author'] ) ? $unsanitized_postarr['post_author'] : null ],
$post
) );
}

if ( empty( $authors ) ) {
return;
}

try {
set_authors( $post, wp_parse_id_list( $authors ) );
} catch ( Exception $e ) {
// Nothing at the moment.
}
}
}
20 changes: 15 additions & 5 deletions inc/cli/class-migrate-command.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ class Migrate_Command extends WP_CLI_Command {
* - false
* ---
*
* [--post-type=<post-type>]
* : Post type, or comma separated list of post types.
* ---
* default: post
* ---
*
* ## EXAMPLES
*
* wp authorship migrate wp-authors --dry-run=true
Expand Down Expand Up @@ -79,6 +85,9 @@ public function wp_authors( $args, $assoc_args ) : void {
WP_CLI::warning( 'Overwriting of previous Authorship data is set to true.' );
}

$post_types = explode( ',', $assoc_args['post-type'] );
WP_CLI::line( sprintf( 'Updating post types: %s', implode( ', ', $post_types ) ) );

$tax_query = $overwrite ? [] : [
[
'taxonomy' => 'authorship',
Expand All @@ -93,6 +102,7 @@ public function wp_authors( $args, $assoc_args ) : void {
$posts = get_posts( [
'posts_per_page' => $posts_per_page,
'paged' => $paged,
'post_type' => $post_types,
'post_status' => 'any',
'ignore_sticky_posts' => true,
'suppress_filters' => false,
Expand Down Expand Up @@ -249,8 +259,8 @@ function ppa( $args, $assoc_args ) : void {
// Usually invalid taxonomy, lets catch and report this.
if ( is_wp_error( $ppa_terms ) ) {
WP_CLI::error( 'There was an error fetching the PublishPress Author data, is the plugin activated?', false );
WP_CLI::error( $ppa_terms );
exit;
WP_CLI::error( $ppa_terms, false );
exit( 1 );
}

/**
Expand Down Expand Up @@ -351,9 +361,9 @@ private function get_ppa_user_id( WP_Term $ppa_author, bool $create_users = fals
// If this fails we want the debug data, so print out the
// arguments so we can reproduce later.
if ( is_wp_error( $ppa_user_id ) ) {
WP_CLI::error( 'Could not create Authorship user with these arguments:' );
WP_CLI::error( $ppa_user_id );
return -1;
WP_CLI::error( 'Could not create Authorship user with these arguments:', false );
WP_CLI::error( $ppa_user_id, false );
exit( 1 );
}

return $ppa_user_id;
Expand Down
Loading

0 comments on commit ae65e9b

Please sign in to comment.