Skip to content

Commit

Permalink
add support for Users (#33)
Browse files Browse the repository at this point in the history
* add support for users

* Upgrade shivammathur/setup-php in CI (#35)

* add support for users

* Upgrade CI (#34)

from https://github.com/ergebnis/php-library-template

* Upgrade shivammathur/setup-php in CI

Co-authored-by: leogermani <[email protected]>

* fix retun of get_metadata when meta is not found

* remove unecessary inheritance

* make test more strict

* Use szepeviktor/phive-install action (#36)

It is a brand new GitHub action

* fix linting

* fix no user found return value

* add test for non existent meta

* enable WP_debug

fix delete_option
fix tests with wp_debug on

* Fix CS (#38)

* Do not abuse existing domain names (#37)

Please see https://tools.ietf.org/html/rfc2606#section-2

* Handle cache gently

* remove useless declaration. avoid conflict

* Update README.md

Co-authored-by: Viktor Szépe <[email protected]>

Co-authored-by: Viktor Szépe <[email protected]>
  • Loading branch information
leogermani and szepeviktor authored Dec 1, 2020
1 parent ef84448 commit f3e55e0
Show file tree
Hide file tree
Showing 16 changed files with 820 additions and 336 deletions.
92 changes: 38 additions & 54 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
uses: "actions/[email protected]"

- name: "Install PHP with extensions"
uses: "shivammathur/setup-php@2.4.1"
uses: "shivammathur/setup-php@2.8.0"
with:
php-version: "${{ matrix.php-version }}"
extensions: "${{ env.PHP_EXTENSIONS }}"
Expand All @@ -43,39 +43,31 @@ jobs:
run: "composer validate --strict"

- name: "Determine composer cache directory"
id: "determine-composer-cache-directory"
run: "echo \"::set-output name=directory::$(composer config cache-dir)\""
shell: "bash"
run: "echo \"COMPOSER_CACHE_DIR=$(composer config cache-dir)\" >> $GITHUB_ENV"

- name: "Cache PHAR files installed with phive"
uses: "actions/[email protected]"
with:
path: "~/.phive"
path: "${{ runner.temp }}/.phive"
key: "php-${{ matrix.php-version }}-phive-${{ matrix.dependencies }}-${{ hashFiles('.phive/phars.xml') }}"
restore-keys: "php-${{ matrix.php-version }}-phive-${{ matrix.dependencies }}-"

- name: "Install phive from cache"
run: |
if [ ! -r "${HOME}/.phive/phive.phar" ]; then
mkdir -p "${HOME}/bin" "${HOME}/.phive"
wget -O "${HOME}/.phive/phive.phar" "https://phar.io/releases/phive.phar"
wget -O "${HOME}/.phive/phive.phar.asc" "https://phar.io/releases/phive.phar.asc"
gpg --batch --keyserver ha.pool.sks-keyservers.net --keyserver-options timeout=30 --recv-keys 0x9D8A98B29B2D5D79 \
|| gpg --batch --keyserver ha.pool.sks-keyservers.net --keyserver-options timeout=30 --recv-keys 0x9D8A98B29B2D5D79
if ! gpg --batch --verify "${HOME}/.phive/phive.phar.asc" "${HOME}/.phive/phive.phar"; then
echo "Invalid phive signature" 1>&2
rm -f "${HOME}/.phive/phive.phar"
exit 11
fi
rm "${HOME}/.phive/phive.phar.asc"
fi
install --verbose --mode=0755 -T -D "${HOME}/.phive/phive.phar" "${HOME}/bin/phive"
echo "::add-path::${HOME}/bin"
"${HOME}/bin/phive" install --trust-gpg-keys C00543248C87FB13,31C7E470E2138192
- name: "Install PHIVE"
uses: "szepeviktor/phive@v1"
with:
home: "${{ runner.temp }}/.phive"

- name: "Install PHP tools with PHIVE"
uses: "szepeviktor/phive-install@v1"
with:
home: "${{ runner.temp }}/.phive"
trustGpgKeys: "C00543248C87FB13,31C7E470E2138192"

- name: "Cache dependencies installed with composer"
uses: "actions/[email protected]"
with:
path: "${{ steps.determine-composer-cache-directory.outputs.directory }}"
path: "${{ env.COMPOSER_CACHE_DIR }}"
key: "php-${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-${{ hashFiles('composer.lock') }}"
restore-keys: "php-${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-"

Expand Down Expand Up @@ -118,47 +110,39 @@ jobs:
uses: "actions/[email protected]"

- name: "Install PHP with extensions"
uses: "shivammathur/setup-php@2.4.1"
uses: "shivammathur/setup-php@2.8.0"
with:
php-version: "${{ matrix.php-version }}"
extensions: "${{ env.PHP_EXTENSIONS }}"
coverage: "none"
tools: "composer:v1"

- name: "Determine composer cache directory"
id: "determine-composer-cache-directory"
run: "echo \"::set-output name=directory::$(composer config cache-dir)\""
shell: "bash"
run: "echo \"COMPOSER_CACHE_DIR=$(composer config cache-dir)\" >> $GITHUB_ENV"

- name: "Cache PHAR files installed with phive"
uses: "actions/[email protected]"
with:
path: "~/.phive"
path: "${{ runner.temp }}/.phive"
key: "php-${{ matrix.php-version }}-phive-${{ matrix.dependencies }}-${{ hashFiles('.phive/phars.xml') }}"
restore-keys: "php-${{ matrix.php-version }}-phive-${{ matrix.dependencies }}-"

- name: "Install phive from cache"
run: |
if [ ! -r "${HOME}/.phive/phive.phar" ]; then
mkdir -p "${HOME}/bin" "${HOME}/.phive"
wget -O "${HOME}/.phive/phive.phar" "https://phar.io/releases/phive.phar"
wget -O "${HOME}/.phive/phive.phar.asc" "https://phar.io/releases/phive.phar.asc"
gpg --batch --keyserver ha.pool.sks-keyservers.net --keyserver-options timeout=30 --recv-keys 0x9D8A98B29B2D5D79 \
|| gpg --batch --keyserver ha.pool.sks-keyservers.net --keyserver-options timeout=30 --recv-keys 0x9D8A98B29B2D5D79
if ! gpg --batch --verify "${HOME}/.phive/phive.phar.asc" "${HOME}/.phive/phive.phar"; then
echo "Invalid phive signature" 1>&2
rm -f "${HOME}/.phive/phive.phar"
exit 11
fi
rm "${HOME}/.phive/phive.phar.asc"
fi
install --verbose --mode=0755 -T -D "${HOME}/.phive/phive.phar" "${HOME}/bin/phive"
echo "::add-path::${HOME}/bin"
"${HOME}/bin/phive" install --trust-gpg-keys C00543248C87FB13,31C7E470E2138192
- name: "Install PHIVE"
uses: "szepeviktor/phive@v1"
with:
home: "${{ runner.temp }}/.phive"

- name: "Install PHP tools with PHIVE"
uses: "szepeviktor/phive-install@v1"
with:
home: "${{ runner.temp }}/.phive"
trustGpgKeys: "C00543248C87FB13,31C7E470E2138192"

- name: "Cache dependencies installed with composer"
uses: "actions/[email protected]"
with:
path: "${{ steps.determine-composer-cache-directory.outputs.directory }}"
path: "${{ env.COMPOSER_CACHE_DIR }}"
key: "php-${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-${{ hashFiles('composer.lock') }}"
restore-keys: "php-${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-"

Expand Down Expand Up @@ -211,21 +195,21 @@ jobs:
uses: "actions/[email protected]"

- name: "Install PHP with extensions"
uses: "shivammathur/setup-php@2.4.1"
uses: "shivammathur/setup-php@2.8.0"
with:
php-version: "${{ matrix.php-version }}"
extensions: "${{ env.PHP_EXTENSIONS }}"
coverage: "none"
tools: "composer:v1"

- name: "Determine composer cache directory"
id: "determine-composer-cache-directory"
run: "echo \"::set-output name=directory::$(composer config cache-dir)\""
shell: "bash"
run: "echo \"COMPOSER_CACHE_DIR=$(composer config cache-dir)\" >> $GITHUB_ENV"

- name: "Cache dependencies installed with composer"
uses: "actions/[email protected]"
with:
path: "${{ steps.determine-composer-cache-directory.outputs.directory }}"
path: "${{ env.COMPOSER_CACHE_DIR }}"
key: "php-${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-${{ hashFiles('composer.lock') }}"
restore-keys: "php-${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-"

Expand Down Expand Up @@ -265,21 +249,21 @@ jobs:
uses: "actions/[email protected]"

- name: "Install PHP with extensions"
uses: "shivammathur/setup-php@2.4.1"
uses: "shivammathur/setup-php@2.8.0"
with:
php-version: "${{ matrix.php-version }}"
extensions: "${{ env.PHP_EXTENSIONS }}"
coverage: "none"
tools: "composer:v1"

- name: "Determine composer cache directory"
id: "determine-composer-cache-directory"
run: "echo \"::set-output name=directory::$(composer config cache-dir)\""
shell: "bash"
run: "echo \"COMPOSER_CACHE_DIR=$(composer config cache-dir)\" >> $GITHUB_ENV"

- name: "Cache dependencies installed with composer"
uses: "actions/[email protected]"
with:
path: "${{ steps.determine-composer-cache-directory.outputs.directory }}"
path: "${{ env.COMPOSER_CACHE_DIR }}"
key: "php-${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-${{ hashFiles('composer.lock') }}"
restore-keys: "php-${{ matrix.php-version }}-composer-${{ matrix.dependencies }}-"

Expand Down
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,32 @@ Manipulating (creating, updating, deleting) posts and attachments will work. Fet
* `wp_get_attachment_image`
* and almost anything related to the manipulation of one attachment

Note: Fetching for posts using `WP_Query` will not work (yet)! To get a post use `get_post( $id )`.
Note: Fetching posts using `WP_Query` will not work (yet)! To fetch a post, use `get_post( $id )`.

#### Users and capabilities

You can create, edit and delete users.

Here is a non-exaustive list of functions supported:

* `wp_insert_user`
* `wp_update_user`
* `wp_delete_user`
* `get_userdata`
* `new WP_User( $id )` to fetch a user
* `user_can`
* `current_user_can`
* `set_current_user`
* `get_current_user_id`
* `wp_get_current_user`
* `get_user_meta`
* `update_user_meta`
* `add_user_meta`
* `delete_user_meta`

Posts can be assigned to users and proper capabilities will be correctly checked. When deleting a user, reassigning posts to other user will also work.

Note: Fetching users using `WP_Users_Query` will not work! To fetch a user, use `get_userdata()`, `get_user_by` or `WP_User` class.

### Populating default options

Expand Down
1 change: 1 addition & 0 deletions src/BaseTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public function tearDown() {
Options::init()->clear_options();
Posts::init()->clear_all_posts();
PostMeta::init()->clear_all_meta();
Users::init()->clear_all_users();
$this->clear_uploads();
}

Expand Down
28 changes: 28 additions & 0 deletions src/ClearCacheGroup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace WorDBless;

trait ClearCacheGroup {

/**
* Clears the cache for the current group
*
* @return void
*/
public function clear_cache_group() {
global $wp_object_cache;

if ( ! $this->cache_group ) {
return;
}

if ( ! isset( $wp_object_cache->cache[ $this->cache_group ] ) || ! is_array( $wp_object_cache->cache[ $this->cache_group ] ) ) {
return;
}

foreach ( array_keys( $wp_object_cache->cache[ $this->cache_group ] ) as $key ) {
wp_cache_delete( $key, $this->cache_group );
}
}

}
8 changes: 8 additions & 0 deletions src/Load.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ public static function load() {
define( 'WP_REPAIRING', true ); // Will not try to install WordPress
define( 'WP_CONTENT_DIR', ABSPATH . 'wp-content' );

$_SERVER['SERVER_NAME'] = 'anything.example';
$_SERVER['HTTP_HOST'] = 'anything.example';

global $table_prefix;
$table_prefix = 'wp_';

require ABSPATH . '/wp-settings.php';
require_once ABSPATH . 'wp-admin/includes/admin.php';
if ( ! file_exists( ABSPATH . 'wp-content/uploads' ) ) {
Expand All @@ -29,6 +35,8 @@ public static function load() {
Options::init();
Posts::init();
PostMeta::init();
Users::init();
UserMeta::init();
WpDie::init();
}

Expand Down
Loading

1 comment on commit f3e55e0

@szepeviktor
Copy link
Collaborator

Choose a reason for hiding this comment

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

🎉

Please sign in to comment.