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

Bump/php7.4 #237

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
12 changes: 4 additions & 8 deletions clarkson-core.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,8 @@ class Clarkson_Core {

/**
* Container for autoloadable files.
*
* @var Autoloader
*/
public $autoloader;
public Autoloader $autoloader;

/**
* Initializes all neccessery objects. Is automatically called on 'init'.
Expand All @@ -37,7 +35,7 @@ class Clarkson_Core {
*
* @internal
*/
public function init():void {
public function init(): void {
// Load post objects.
Objects::get_instance();

Expand All @@ -50,11 +48,9 @@ public function init():void {
}

/**
* Define instance.
*
* @var null|Clarkson_Core
* The Clarkson Core instance
*/
protected static $instance = null;
protected static ?Clarkson_Core $instance = null;

/**
* Setting up the class instance.
Expand Down
17 changes: 9 additions & 8 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "level-level/clarkson-core",
"type": "library",
"license": "GPL-2.0-or-later",
"version": "1.4.0",
"description": "A plugin to write Object-Oriented code in combination with the Twig templating engine while keeping the WordPress Way of working in mind.",
"authors": [
{
Expand Down Expand Up @@ -44,19 +45,19 @@
]
},
"require" : {
"php":"^7.2",
"twig/twig": "^3.1",
"twig/string-extra": "^3.1",
"twig/intl-extra": "^3.1",
"twig/html-extra": "^3.1",
"twig/markdown-extra": "^3.1"
"php":"^7.4|^8.0|^8.1",
"twig/twig": "^3.3",
"twig/string-extra": "^3.3",
"twig/intl-extra": "^3.3",
"twig/html-extra": "^3.3",
"twig/markdown-extra": "^3.3"
},
"require-dev": {
"giacocorsiglia/wordpress-stubs": "^5.1",
"vimeo/psalm": "^3.4",
"vimeo/psalm": "^4.20",
"10up/wp_mock": "^0.4.2",
"wp-coding-standards/wpcs": "^2.3",
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.0"
"dealerdirect/phpcodesniffer-composer-installer": "^0.7.2"
},
"config": {
"allow-plugins": {
Expand Down
2 changes: 1 addition & 1 deletion docs/getting-started/installation.rst
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Installation
Requirements
---

- PHP 7.2 or higher
- PHP 7.4 or higher
- WordPress 4.7 or higher

Using composer
Expand Down
1 change: 0 additions & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
<?xml version="1.0"?>
<psalm
totallyTyped="false"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd"
Expand Down
19 changes: 4 additions & 15 deletions src/Autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,18 @@
class Autoloader {
/**
* Define the prefix for the custom user Object classes.
*
* @var string $user_objectname_prefix Classname prefix.
* @internal
*/
public $user_objectname_prefix = 'user_';
public string $user_objectname_prefix = 'user_';

/**
* Changes object names into valid classnames.
*
* A post type with a name 'll-events' can not be a valid classname in PHP.
* Any none alphanumeric character is changed into an `_` and the complete
* name is changed to lowercase.
*
* @param string $str Object name.
*
* @return string Sanitized object name.
*/
public function sanitize_object_name( $str ):string {
public function sanitize_object_name( string $str ): string {
$str = trim( $str );

// Replace - with _ .
Expand All @@ -49,18 +43,13 @@ public function sanitize_object_name( $str ):string {
* Returns the page template filename without extension.
* Returns an empty string when the default page template is in use.
* Returns false if the post is not a page.
*
* @param integer $post_id Post id.
*
* @return string
*/
public function get_template_filename( $post_id ):string {
public function get_template_filename( int $post_id ): string {
$page_template_slug = get_page_template_slug( $post_id );
$filename = '';

if ( ! empty( $page_template_slug ) ) {
$pathinfo = pathinfo( $page_template_slug );
$filename = array_key_exists( 'filename', $pathinfo ) ? (string) $pathinfo['filename'] : '';
$filename = pathinfo( $page_template_slug, PATHINFO_FILENAME );
}
return $filename;
}
Expand Down
22 changes: 22 additions & 0 deletions src/ClarksonWPException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Clarkson_Core;

class ClarksonWPException extends \Exception {
protected \WP_Error $wp_error;

/**
* Construct an \Exception based on the \WP_Error class
*/
public function __construct( \WP_Error $wp_error, int $code = 0, \Throwable $previous = null ) {
parent::__construct( $wp_error->get_error_message(), $code, $previous );
$this->wp_error = $wp_error;
}

/**
* Returns the original \WP_Error object that was returned by WordPress.
*/
public function getWPError(): \WP_Error {
return $this->wp_error;
}
}
6 changes: 3 additions & 3 deletions src/Gutenberg/Block_Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public function init():void {
* @param \WP_Block_type $block_type Gutenberg block to determine class for.
* @return string
*/
public function determine_block_type_class( $block_type ) {
public function determine_block_type_class( \WP_Block_Type $block_type ): string {
$class_name = '\\Gutenberg\\Blocks\\' . $this->sanitize_block_type_name( $block_type->name );

/**
Expand Down Expand Up @@ -85,7 +85,7 @@ public function determine_block_type_class( $block_type ) {
* @param string $str Classname to be sanitized.
* @return string
*/
public function sanitize_block_type_name( $str ) {
public function sanitize_block_type_name( string $str ): string {
$str = trim( $str );

// Replace - with _ .
Expand All @@ -112,7 +112,7 @@ public function sanitize_block_type_name( $str ) {
*
* @internal
*/
public function intercept_gutenberg_rendering( $content ) {
public function intercept_gutenberg_rendering( string $content ): string {
$block_registry = \WP_Block_Type_Registry::get_instance();
foreach ( $block_registry->get_all_registered() as $original_block ) {
$block_type = $this->determine_block_type_class( $original_block );
Expand Down
21 changes: 15 additions & 6 deletions src/Gutenberg/Block_Type.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct( $block_type, $args = array() ) {
*
* @return string
*/
public function get_twig_template_path() {
public function get_twig_template_path(): string {
/**
* Allows theme to overwrite the directory in which Clarkson Core automatically loads twig templates for blocks.
*
Expand Down Expand Up @@ -75,11 +75,20 @@ public function get_twig_template_path() {
/**
* Tries to find a twig file to use for rendering. If the twig file doesn't
* exists it falls back to the original render callback.
* @param array $attributes Block attributes.
* @param string $content Block content.
* @return string Rendered block type output.
*
* @param array $args {
* A list of callback arguments
*
* @type array $attributes Block attributes
* @type string $content Block content
* @type \WP_Block $block (Optional) original block instance
* }
*
* @return string Rendered block type output.
*/
public function clarkson_render_callback( $attributes, $content ) {
public function clarkson_render_callback( ...$args ): string {
list( $attributes, $content ) = $args;

if ( file_exists( $this->get_twig_template_path() ) ) {
$cc_template = Templates::get_instance();
$this->content_attributes = $attributes;
Expand Down Expand Up @@ -118,7 +127,7 @@ public function clarkson_render_callback( $attributes, $content ) {
);
}
if ( is_callable( $this->original_render_callback ) ) {
return (string) call_user_func( $this->original_render_callback, $attributes, $content );
return (string) call_user_func_array( $this->original_render_callback, $args );
}
return $content;
}
Expand Down
52 changes: 23 additions & 29 deletions src/Objects.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,26 @@
* Objects.
*/
class Objects {
const OBJECT_CLASS_NAMESPACE = '\\Clarkson_Core\\WordPress_Object\\';
public const OBJECT_CLASS_NAMESPACE = '\\Clarkson_Core\\WordPress_Object\\';

/**
* Singleton.
*/
protected static ?\Clarkson_Core\Objects $instance = null;

/**
* Get the instance.
*
* @return \Clarkson_Core\Objects
*/
public static function get_instance(): \Clarkson_Core\Objects {
if ( null === self::$instance ) {
self::$instance = new self();
}

return self::$instance;
}

/**
* Convert WP_Term object to a Clarkson Object.
*
Expand Down Expand Up @@ -108,9 +127,6 @@ public function get_users( array $users ): array {
* @return Clarkson_User
*/
public function get_user( \WP_User $user ): Clarkson_User {
/**
* @psalm-var string
*/
$type = self::OBJECT_CLASS_NAMESPACE . 'user';

/**
Expand Down Expand Up @@ -350,9 +366,7 @@ public function get_template( \WP_Post $post ): Clarkson_Template {
}
}

/**
* @psalm-var string
*/
/** @var string */
$class_name = self::OBJECT_CLASS_NAMESPACE . 'base_template';
if ( class_exists( $class_name ) ) {
$object = new $class_name( $post );
Expand Down Expand Up @@ -401,7 +415,7 @@ public function get_post_types( array $post_types ): array {
/**
* Get post type object by post type.
*/
public function get_post_type( \WP_Post_Type $post_type ):Clarkson_Post_Type {
public function get_post_type( \WP_Post_Type $post_type ): Clarkson_Post_Type {
$cc = Clarkson_Core::get_instance();

$class_name = 'post_type_' . $post_type->name;
Expand Down Expand Up @@ -467,7 +481,7 @@ public function get_taxonomies( array $taxonomies ): array {
/**
* Get taxonomy object by taxonomy.
*/
public function get_taxonomy( \WP_Taxonomy $taxonomy ):Clarkson_Taxonomy {
public function get_taxonomy( \WP_Taxonomy $taxonomy ): Clarkson_Taxonomy {
$cc = Clarkson_Core::get_instance();

$class_name = 'taxonomy_' . $taxonomy->name;
Expand Down Expand Up @@ -513,26 +527,6 @@ public function get_taxonomy( \WP_Taxonomy $taxonomy ):Clarkson_Taxonomy {
return new Clarkson_Taxonomy( $taxonomy );
}

/**
* Singleton.
*
* @var null|\Clarkson_Core\Objects $instance The Clarkson core objects.
*/
protected static $instance;

/**
* Get the instance.
*
* @return \Clarkson_Core\Objects
*/
public static function get_instance(): \Clarkson_Core\Objects {
if ( null === self::$instance ) {
self::$instance = new self();
}

return self::$instance;
}

/**
* Clone.
* @codeCoverageIgnore
Expand Down
12 changes: 6 additions & 6 deletions src/Template_Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public function add_author( array $context, \WP_Query $wp_query ): array {
/**
* Adds a term if the current request is a term archive.
*/
public function add_term( array $context, \WP_Query $wp_query ):array {
public function add_term( array $context, \WP_Query $wp_query ): array {
if ( $wp_query->is_tax || $wp_query->is_category || $wp_query->is_tag ) {
$object_loader = Objects::get_instance();
$term = $wp_query->queried_object;
Expand All @@ -54,7 +54,7 @@ public function add_term( array $context, \WP_Query $wp_query ):array {
/**
* Adds the search result count if the current request is a search.
*/
public function add_search_count( array $context, \WP_Query $wp_query ):array {
public function add_search_count( array $context, \WP_Query $wp_query ): array {
if ( $wp_query->is_search ) {
$context['found_posts'] = $wp_query->get( 'filtered_found_posts' ) ? $wp_query->get( 'filtered_found_posts' ) : $wp_query->found_posts;
}
Expand All @@ -64,14 +64,14 @@ public function add_search_count( array $context, \WP_Query $wp_query ):array {
/**
* Adds posts to the current context.
*/
public function add_posts( array $context, \WP_Query $wp_query ):array {
public function add_posts( array $context, \WP_Query $wp_query ): array {
$object_loader = Objects::get_instance();
$context['objects'] = $object_loader->get_objects( $wp_query->posts );
$context['object'] = reset( $context['objects'] );
return $context;
}

public function add_post_type( array $context, \WP_Query $wp_query ):array {
public function add_post_type( array $context, \WP_Query $wp_query ): array {
if ( ! $wp_query->is_post_type_archive ) {
return $context;
}
Expand All @@ -95,7 +95,7 @@ public function add_post_type( array $context, \WP_Query $wp_query ):array {
/**
* Adds posts page overview as Clarkson Object if current page is home
*/
public function add_posts_page( array $context ):array {
public function add_posts_page( array $context ): array {
if ( ! is_home() ) {
return $context;
}
Expand All @@ -107,7 +107,7 @@ public function add_posts_page( array $context ):array {
}

$post = get_post( get_option( 'page_for_posts' ) );
$context['posts_page'] = Objects::get_instance()->get_object( $post );
$context['posts_page'] = $post instanceof \WP_Post ? Objects::get_instance()->get_object( $post ) : null;

return $context;
}
Expand Down
Loading