Initial commit: Atomaste website

This commit is contained in:
2025-12-10 12:17:30 -05:00
commit 0b9e5d1605
19260 changed files with 5206382 additions and 0 deletions

View File

@@ -0,0 +1,38 @@
<?php
namespace Hostinger\Mcp;
use Hostinger\Admin\Proxy;
use Hostinger\Mcp\Handlers\WebsiteMcpOptInToggled;
use Hostinger\Mcp\Handlers\WebsitePageUpdated;
use Hostinger\Mcp\Handlers\WebsiteUpdated;
defined( 'ABSPATH' ) || exit;
class EventHandlerFactory {
public const MCP_EVENT_UPDATED = 'wordpress.website.updated';
public const MCP_EVENT_PAGE_UPDATED = 'wordpress.website.page_updated';
public const MCP_EVENT_OPTIN_TOGGLED = 'wordpress.website.mcp.opt_in_toggled';
private array $handlers;
private Proxy $proxy;
public function __construct( Proxy $proxy ) {
$this->proxy = $proxy;
$this->handlers = array(
self::MCP_EVENT_UPDATED => WebsiteUpdated::class,
self::MCP_EVENT_PAGE_UPDATED => WebsitePageUpdated::class,
self::MCP_EVENT_OPTIN_TOGGLED => WebsiteMcpOptInToggled::class,
);
}
public function get_handler( string $event ) {
$handler = $this->handlers[ $event ] ?? false;
if ( ! $handler ) {
throw new \WP_Exception( 'Invalid event' );
}
return new $handler( $this->proxy );
}
}

View File

@@ -0,0 +1,61 @@
<?php
namespace Hostinger\Mcp\Handlers;
use Hostinger\Admin\Proxy;
use Hostinger\Mcp\EventHandlerFactory;
defined( 'ABSPATH' ) || exit;
abstract class EventHandler {
public Proxy $proxy;
public function __construct( Proxy $proxy ) {
$this->proxy = $proxy;
}
protected function send_to_proxy( array $args = array() ): bool {
if ( ! $this->can_send( $args ) ) {
$this->debug_mcp( 'Event failed: User is not opted in' . ' -- ' . print_r( $args, true ) );
return false;
}
$event = $args['event'];
$request = $this->proxy->trigger_event( $event, $args );
if ( is_wp_error( $request ) ) {
$this->debug_mcp( 'Event failed: ' . $event . $request->get_error_message() . ' -- ' . print_r( $args, true ) );
return false;
}
$response_code = wp_remote_retrieve_response_code( $request );
if ( $response_code < 300 ) {
$this->debug_mcp( 'Event sent: ' . $event . ' -- ' . print_r( $args, true ) );
return true;
}
$this->debug_mcp( 'Event failed: ' . $event . ' --' . $response_code . ' -- ' . print_r( $args, true ) );
return false;
}
public function can_send( array $args = array() ): bool {
return isset( $args['event'] ) && ( $this->is_optin_event( $args['event'] ) || $this->is_opted_in() );
}
private function is_opted_in(): bool {
$settings = get_option( HOSTINGER_PLUGIN_SETTINGS_OPTION, array() );
return $settings['optin_mcp'] ?? false;
}
private function is_optin_event( $event ): bool {
return $event === EventHandlerFactory::MCP_EVENT_OPTIN_TOGGLED;
}
private function debug_mcp( string $msg ): void {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( 'Hostinger Tools MCP: ' . $msg . PHP_EOL, 3, WP_CONTENT_DIR . '/mcp.log' );
}
}
abstract public function send( array $args = array() ): void;
}

View File

@@ -0,0 +1,11 @@
<?php
namespace Hostinger\Mcp\Handlers;
defined( 'ABSPATH' ) || exit;
class WebsiteMcpOptInToggled extends EventHandler {
public function send( array $args = array() ): void {
$this->send_to_proxy( $args );
}
}

View File

@@ -0,0 +1,35 @@
<?php
namespace Hostinger\Mcp\Handlers;
defined( 'ABSPATH' ) || exit;
class WebsitePageUpdated extends EventHandler {
const ALLOWED_POST_TYPES = array( 'post', 'product', 'page' );
public function send( array $args = array() ): void {
if ( ! $this->can_send( $args ) ) {
return;
}
$post = get_post( $args['post_id'] );
$args['url'] = get_permalink( $post );
$this->send_to_proxy( $args );
}
public function can_send( array $args = array() ): bool {
$post_id = $args['post_id'] ?? false;
if ( ! $post_id ) {
return false;
}
$post = get_post( $post_id );
if ( ! $post ) {
return false;
}
return parent::can_send( $args ) && in_array( $post->post_type, self::ALLOWED_POST_TYPES, true );
}
}

View File

@@ -0,0 +1,31 @@
<?php
namespace Hostinger\Mcp\Handlers;
use Hostinger\Admin\Jobs\ActionScheduler;
defined( 'ABSPATH' ) || exit;
class WebsiteUpdated extends EventHandler {
const MCP_SITE_TRANSIENT = 'hostinger_mcp_site_status';
const MCP_SITE_TRANSIENT_LIFETIME = 1200; // 20 mins
public function send( array $args = array() ): void {
if ( ! $this->can_send( $args ) ) {
return;
}
$status = ActionScheduler::STATUS_FAILED;
if ( $this->send_to_proxy( $args ) ) {
$status = ActionScheduler::STATUS_COMPLETE;
}
set_transient( self::MCP_SITE_TRANSIENT, $status, self::MCP_SITE_TRANSIENT_LIFETIME );
}
public function can_send( array $args = array() ): bool {
return parent::can_send( $args ) && get_transient( self::MCP_SITE_TRANSIENT ) !== ActionScheduler::STATUS_COMPLETE;
}
}