first commit

This commit is contained in:
User A0264400
2026-04-01 23:20:16 +03:00
commit a766acdc90
23071 changed files with 4933189 additions and 0 deletions

View File

@@ -0,0 +1,30 @@
<?php
namespace Yoast\WP\SEO\AI_Free_Sparks\Application;
/**
* Interface Consent_Handler_Interface
*
* This interface defines the methods for handling user consent.
*/
interface Free_Sparks_Handler_Interface {
/**
* Retrieves the timestamp.
*
* @param string $format The format in which to return the timestamp. Defaults to 'Y-m-d H:i:s'.
*
* @return ?string The timestamp when the user started using free sparks, or null if not set.
*/
public function get( string $format = 'Y-m-d H:i:s' ): ?string;
/**
* Registers the starting of the free sparks.
*
* @param ?int $timestamp The timestamp when the user started using free sparks. If null, the current time will be
* used.
*
* @return bool True if the operation was successful, false otherwise.
*/
public function start( ?int $timestamp = null ): bool;
}

View File

@@ -0,0 +1,62 @@
<?php
namespace Yoast\WP\SEO\AI_Free_Sparks\Application;
use Yoast\WP\SEO\Helpers\Options_Helper;
/**
* Handles the free sparks started on timestamp.
*/
class Free_Sparks_Handler implements Free_Sparks_Handler_Interface {
/**
* The key used to store the timestamp when the user started using free sparks.
*
* @var string
*/
public const OPTION_KEY = 'ai_free_sparks_started_on';
/**
* Holds the options helper instance.
*
* @var Options_Helper
*/
private $options_helper;
/**
* Class constructor.
*
* @param Options_Helper $options_helper The options helper.
*/
public function __construct( Options_Helper $options_helper ) {
$this->options_helper = $options_helper;
}
/**
* Retrieves the timestamp.
*
* @param string $format The format in which to return the timestamp. Defaults to 'Y-m-d H:i:s'.
*
* @return ?string The timestamp when the user started using free sparks, or null if not set.
*/
public function get( string $format = 'Y-m-d H:i:s' ): ?string {
$timestamp = $this->options_helper->get( self::OPTION_KEY, null );
if ( $timestamp === null ) {
return null;
}
return \gmdate( $format, (int) $timestamp );
}
/**
* Registers the starting of the free sparks.
*
* @param ?int $timestamp The timestamp when the user started using free sparks. If null, the current time will be
* used.
*
* @return bool True if the operation was successful, false otherwise.
*/
public function start( ?int $timestamp = null ): bool {
return (bool) $this->options_helper->set( self::OPTION_KEY, ( $timestamp === null ) ? \time() : $timestamp, 'wpseo' );
}
}

View File

@@ -0,0 +1,49 @@
<?php
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure.
namespace Yoast\WP\SEO\AI_Free_Sparks\Infrastructure\Endpoints;
use Yoast\WP\SEO\AI_Free_Sparks\User_Interface\Free_Sparks_Route;
use Yoast\WP\SEO\Routes\Endpoint_Interface;
/**
* Represents the free sparks endpoint.
*/
class Free_Sparks_Endpoint implements Endpoint_Interface {
/**
* Gets the name.
*
* @return string
*/
public function get_name(): string {
return 'free_sparks';
}
/**
* Gets the namespace.
*
* @return string
*/
public function get_namespace(): string {
return Free_Sparks_Route::ROUTE_NAMESPACE;
}
/**
* Gets the route.
*
* @return string
*/
public function get_route(): string {
return Free_Sparks_Route::ROUTE_PREFIX;
}
/**
* Gets the URL.
*
* @return string
*/
public function get_url(): string {
return \rest_url( $this->get_namespace() . $this->get_route() );
}
}

View File

@@ -0,0 +1,100 @@
<?php
// phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure.
namespace Yoast\WP\SEO\AI_Free_Sparks\User_Interface;
use WP_REST_Response;
use Yoast\WP\SEO\AI_Free_Sparks\Application\Free_Sparks_Handler_Interface;
use Yoast\WP\SEO\Conditionals\AI_Conditional;
use Yoast\WP\SEO\Main;
use Yoast\WP\SEO\Routes\Route_Interface;
/**
* Registers a route to start free sparks.
*/
class Free_Sparks_Route implements Route_Interface {
/**
* The namespace for this route.
*
* @var string
*/
public const ROUTE_NAMESPACE = Main::API_V1_NAMESPACE;
/**
* The prefix for this route.
*
* @var string
*/
public const ROUTE_PREFIX = '/ai/free_sparks';
/**
* The free sparks handler instance.
*
* @var Free_Sparks_Handler_Interface
*/
private $free_sparks_handler;
/**
* Returns the conditionals based in which this loadable should be active.
*
* @return array<string> The conditionals.
*/
public static function get_conditionals() {
return [ AI_Conditional::class ];
}
/**
* Class constructor.
*
* @param Free_Sparks_Handler_Interface $free_sparks_handler The free sparks handler instance.
*/
public function __construct( Free_Sparks_Handler_Interface $free_sparks_handler ) {
$this->free_sparks_handler = $free_sparks_handler;
}
/**
* Registers routes with WordPress.
*
* @return void
*/
public function register_routes() {
\register_rest_route(
self::ROUTE_NAMESPACE,
self::ROUTE_PREFIX,
[
'methods' => 'POST',
'callback' => [ $this, 'start' ],
'permission_callback' => [ $this, 'can_edit_posts' ],
],
);
}
/**
* Runs the callback to start the free sparks.
*
* @return WP_REST_Response The response of the callback action.
*/
public function start(): WP_REST_Response {
$result = $this->free_sparks_handler->start( null );
if ( ! $result ) {
new WP_REST_Response( 'Failed to start free sparks.', 500 );
}
return new WP_REST_Response( 'Free sparks successfully started.' );
}
/**
* Checks whether the user is logged in and can edit posts.
*
* @return bool Whether the user is logged in and can edit posts.
*/
public function can_edit_posts(): bool {
$user = \wp_get_current_user();
if ( $user === null || $user->ID < 1 ) {
return false;
}
return \user_can( $user, 'edit_posts' );
}
}