Skip to content

Commit

Permalink
Merge pull request #468 from GatherPress/mauteri-cli
Browse files Browse the repository at this point in the history
Updates and fixes.
  • Loading branch information
mauteri authored Dec 29, 2023
2 parents ebe408f + dee1306 commit dd606cd
Show file tree
Hide file tree
Showing 34 changed files with 216 additions and 153 deletions.
5 changes: 3 additions & 2 deletions includes/core/classes/class-assets.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@
* @since 1.0.0
*/
class Assets {

/**
* Enforces a single instance of this class.
*/
use Singleton;

/**
Expand Down Expand Up @@ -386,5 +388,4 @@ protected function get_asset_data( string $asset ): array {

return $this->asset_data[ $asset ];
}

}
5 changes: 2 additions & 3 deletions includes/core/classes/class-autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
* @since 1.0.0
*/
class Autoloader {

/**
* Register method for autoloader.
*
Expand All @@ -46,8 +45,9 @@ static function( $class ): bool {
array_unshift( $structure, 'includes' );

switch ( $class_type ) {
case 'traits':
case 'commands':
case 'settings':
case 'traits':
array_pop( $structure );
array_push( $structure, 'classes', $class_type );
break;
Expand All @@ -69,5 +69,4 @@ static function( $class ): bool {
}
);
}

}
5 changes: 3 additions & 2 deletions includes/core/classes/class-block.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
* @since 1.0.0
*/
class Block {

/**
* Enforces a single instance of this class.
*/
use Singleton;

/**
Expand Down Expand Up @@ -63,5 +65,4 @@ public function register_blocks(): void {
register_block_type( sprintf( '%1$s/build/blocks/%2$s', GATHERPRESS_CORE_PATH, $block ) );
}
}

}
100 changes: 14 additions & 86 deletions includes/core/classes/class-cli.php
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?php
/**
* Class responsible for WP-CLI commands within GatherPress.
* Class responsible for registering WP-CLI commands within GatherPress.
*
* This class handles WP-CLI commands specific to the GatherPress plugin,
* This class registers WP-CLI commands specific to the GatherPress plugin,
* allowing developers to interact with and manage plugin functionality via the command line.
*
* @package GatherPress\Core
Expand All @@ -11,6 +11,9 @@

namespace GatherPress\Core;

use GatherPress\Core\Commands\Cli_Event;
use GatherPress\Core\Commands\Cli_General;
use GatherPress\Core\Traits\Singleton;
use WP_CLI;

/**
Expand All @@ -21,98 +24,23 @@
*
* @since 1.0.0
*/
class Cli extends WP_CLI {

class Cli {
/**
* Perform actions on an event.
*
* This method allows you to perform various actions related to events, such as adding responses.
*
* @since 1.0.0
*
* @param array $args Positional arguments for the script.
* @param array $assoc_args Associative arguments for the script.
* @return void
* Enforces a single instance of this class.
*/
public function event( array $args = array(), array $assoc_args = array() ): void {
$event_id = (int) $args[0];
$action = (string) $args[1];

if ( 'add-response' === $action ) {
$this->add_response( $event_id, $assoc_args );
}
}
use Singleton;

/**
* Generate credits data for the credits page.
* Constructor for the Setup class.
*
* This method generates credits data for displaying on the credits page.
* It retrieves user data from WordPress.org profiles based on the provided version.
* Registers WP-CLI commands for GatherPress if WP-CLI is present.
*
* @since 1.0.0
*
* @param array $args Positional arguments for the script.
* @param array $assoc_args Associative arguments for the script.
* @return void
*/
public function generate_credits( array $args = array(), array $assoc_args = array() ): void {
$credits = require_once GATHERPRESS_CORE_PATH . '/includes/data/credits/credits.php';
$version = $assoc_args['version'] ?? GATHERPRESS_VERSION;
$latest = GATHERPRESS_CORE_PATH . '/includes/data/credits/latest.php';
$data = array();

if ( empty( $credits[ $version ] ) ) {
WP_CLI::error( 'Version does not exist' );
}

unlink( $latest );
$file = fopen( $latest, 'w' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fopen

$data['version'] = $version;

foreach ( $credits[ $version ] as $group => $users ) {
if ( 'contributors' === $group ) {
sort( $users );
}

$data[ $group ] = array();

foreach ( $users as $user ) {
$response = wp_remote_request( sprintf( 'https://profiles.wordpress.org/wp-json/wporg/v1/users/%s', $user ) );
$user_data = json_decode( $response['body'], true );

// Remove unsecure data (eg http) and data we do not need.
unset( $user_data['description'], $user_data['url'], $user_data['meta'], $user_data['_links'] );

$data[ $group ][] = $user_data;
}
protected function __construct() {
if ( defined( 'WP_CLI' ) && WP_CLI ) { // @codeCoverageIgnore
WP_CLI::add_command( 'gatherpress', Cli_General::class ); // @codeCoverageIgnore
WP_CLI::add_command( 'gatherpress event', Cli_Event::class ); // @codeCoverageIgnore
}
fwrite( $file, '<?php return ' . var_export( $data, true ) . ';' ); //phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fwrite,WordPress.PHP.DevelopmentFunctions.error_log_var_export
fclose( $file ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_read_fclose

WP_CLI::success( 'New latest.php file has been generated.' );
}

/**
* Add response to an event.
*
* This method adds a response to the specified event, identified by its Post ID.
*
* @since 1.0.0
*
* @param int $event_id The Post ID of the event.
* @param array $assoc_args Associative arguments for the script, including 'user_id', 'status', and 'guests'.
* @return void
*/
private function add_response( int $event_id, array $assoc_args ): void {
$event = new Event( $event_id );
$user_id = $assoc_args['user_id'];
$status = $assoc_args['status'];
$guests = $assoc_args['guests'] ?? 0;

$response = $event->rsvp->save( $user_id, $status, $guests );

WP_CLI::success( $response );
}

}
5 changes: 3 additions & 2 deletions includes/core/classes/class-event-query.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
* @since 1.0.0
*/
class Event_Query {

/**
* Enforces a single instance of this class.
*/
use Singleton;

/**
Expand Down Expand Up @@ -340,5 +342,4 @@ public function adjust_event_sql( array $pieces, string $type = 'all', string $o

return $pieces;
}

}
5 changes: 2 additions & 3 deletions includes/core/classes/class-event.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
* @since 1.0.0
*/
class Event {

/**
* Cache key format for storing and retrieving event datetimes.
*
Expand Down Expand Up @@ -251,7 +250,8 @@ public function get_display_datetime(): string {
}

if ( ! empty( $start ) && ! empty( $end ) ) {
return sprintf( '%s to %s', $start, $end );
/* translators: %1$s: start datetime, %2$s: end datetime. */
return sprintf( __( '%1$s to %2$s', 'gatherpress' ), $start, $end );
}

return __( '', 'gatherpress' );
Expand Down Expand Up @@ -951,5 +951,4 @@ public function maybe_get_online_event_link(): string {

return $event_link;
}

}
5 changes: 3 additions & 2 deletions includes/core/classes/class-rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@
* @since 1.0.0
*/
class Rest_Api {

/**
* Enforces a single instance of this class.
*/
use Singleton;

/**
Expand Down Expand Up @@ -677,5 +679,4 @@ public function prepare_event_data( WP_REST_Response $response ): WP_REST_Respon

return $response;
}

}
2 changes: 0 additions & 2 deletions includes/core/classes/class-rsvp.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
* @since 1.0.0
*/
class Rsvp {

/**
* Table format for RSVPs.
*
Expand Down Expand Up @@ -413,5 +412,4 @@ function( $role ) {
public function sort_by_timestamp( array $first, array $second ): bool {
return ( strtotime( $first['timestamp'] ) < strtotime( $second['timestamp'] ) );
}

}
5 changes: 3 additions & 2 deletions includes/core/classes/class-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
* @since 1.0.0
*/
class Settings {

/**
* Enforces a single instance of this class.
*/
use Singleton;

const PARENT_SLUG = 'edit.php?post_type=gp_event';
Expand Down Expand Up @@ -639,5 +641,4 @@ public function datetime_preview( string $name, string $value ): void {
);
}
}

}
11 changes: 4 additions & 7 deletions includes/core/classes/class-setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use Exception;
use GatherPress\Core\Traits\Singleton;
use WP_CLI;
use WP_Post;

/**
Expand All @@ -24,7 +23,9 @@
* @since 1.0.0
*/
class Setup {

/**
* Enforces a single instance of this class.
*/
use Singleton;

/**
Expand Down Expand Up @@ -55,14 +56,11 @@ protected function __construct() {
protected function instantiate_classes(): void {
Assets::get_instance();
Block::get_instance();
Cli::get_instance();
Event_Query::get_instance();
Rest_Api::get_instance();
Settings::get_instance();
Venue::get_instance();

if ( defined( 'WP_CLI' ) && WP_CLI ) { // @codeCoverageIgnore
WP_CLI::add_command( 'gatherpress', Cli::class ); // @codeCoverageIgnore
}
}

/**
Expand Down Expand Up @@ -671,5 +669,4 @@ public function check_users_can_register() : void {
);
}
}

}
2 changes: 0 additions & 2 deletions includes/core/classes/class-utility.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
* @since 1.0.0
*/
class Utility {

/**
* Renders a template file.
*
Expand Down Expand Up @@ -117,5 +116,4 @@ public static function timezone_choices(): array {

return $timezones_clean;
}

}
5 changes: 3 additions & 2 deletions includes/core/classes/class-venue.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
* @since 1.0.0
*/
class Venue {

/**
* Enforces a single instance of this class.
*/
use Singleton;

/**
Expand Down Expand Up @@ -392,5 +394,4 @@ public function get_venue_meta( int $post_id, string $post_type ): array {

return $venue_meta;
}

}
Loading

0 comments on commit dd606cd

Please sign in to comment.