This commit is contained in:
2026-07-02 15:54:39 -06:00
commit 9883323161
17470 changed files with 4470592 additions and 0 deletions
@@ -0,0 +1,263 @@
<?php
/**
* The Public Keys post type class file.
*
* @package GenerateCloud
*/
namespace GenerateCloud\Modules;
use GenerateCloud\Modules\Module;
use GenerateCloud\Modules\Post_Type;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Public Keys post type.
*
* @since 1.0.0
*/
class Dashboard extends Module {
public function load(): void {
add_action( 'admin_menu', [ $this, 'add_menu' ] );
add_action( 'generateblocks_dashboard_tabs', [ $this, 'add_tab' ] );
add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
add_filter( 'generateblocks_dashboard_screens', [ $this, 'add_to_dashboard_pages' ] );
add_action( 'save_post_' . Post_Type::POST_TYPE, [ $this, 'permanently_delete_keys' ], 10, 2 );
add_action( 'generateblocks_settings_area', array( $this, 'add_license_key_area' ), 25 );
}
/**
* Add our Dashboard menu item.
*/
public function add_menu() {
$settings = add_submenu_page(
'generateblocks',
__( 'Cloud', 'generatecloud' ),
__( 'Cloud', 'generatecloud' ),
'manage_options',
'generateblocks-public-keys',
array( $this, 'public_keys' ),
5
);
}
/**
* Add a Local Templates tab to the GB Dashboard tabs.
*
* @param array $tabs The existing tabs.
*/
public function add_tab( $tabs ) {
$screen = get_current_screen();
$tabs['public-keys'] = array(
'name' => __( 'Cloud', 'generatecloud' ),
'url' => admin_url( 'admin.php?page=generateblocks-public-keys' ),
'class' => 'generateblocks_page_generateblocks-public-keys' === $screen->id ? 'active' : '',
);
return $tabs;
}
/**
* Enqueue our scripts.
*/
public function enqueue_scripts() {
$this->enqueue_public_key_scripts();
$this->enqueue_licensing_scripts();
}
/**
* Enqueue our public key scripts.
*/
public function enqueue_public_key_scripts() {
$screen = get_current_screen();
if ( 'generateblocks_page_generateblocks-public-keys' !== $screen->id ) {
return;
}
$assets_file = GENERATECLOUD_DIR . 'dist/dashboard.asset.php';
$compiled_assets = file_exists( $assets_file )
? require $assets_file
: false;
$assets =
isset( $compiled_assets['dependencies'] ) &&
isset( $compiled_assets['version'] )
? $compiled_assets
: [
'dependencies' => [],
'version' => GENERATECLOUD_VERSION,
];
wp_enqueue_script(
'generatecloud-dashboard',
GENERATECLOUD_URL . 'dist/dashboard.js',
$assets['dependencies'],
$assets['version'],
true
);
$available_permissions = apply_filters(
'generatecloud_available_permissions',
array(
'patterns' => array(
'label' => array(
'singular' => __( 'Pattern', 'generatecloud' ),
'plural' => __( 'Patterns', 'generatecloud' ),
),
'type' => 'taxonomy',
'object' => 'gblocks_pattern_collections',
),
)
);
wp_localize_script(
'generatecloud-dashboard',
'generateCloud',
[
'connectionUrl' => site_url(),
'availablePermissions' => $available_permissions,
]
);
wp_set_script_translations(
'generatecloud-dashboard',
'generatecloud',
GENERATECLOUD_DIR . 'languages'
);
wp_enqueue_style(
'generatecloud-dashboard',
GENERATECLOUD_URL . 'dist/dashboard.css',
array( 'wp-components' ),
GENERATECLOUD_VERSION
);
}
/**
* Enqueue our licensing scripts.
*/
public function enqueue_licensing_scripts() {
$screen = get_current_screen();
if ( 'generateblocks_page_generateblocks-settings' !== $screen->id ) {
return;
}
$assets_file = GENERATECLOUD_DIR . 'dist/license-key.asset.php';
$compiled_assets = file_exists( $assets_file )
? require $assets_file
: false;
$assets =
isset( $compiled_assets['dependencies'] ) &&
isset( $compiled_assets['version'] )
? $compiled_assets
: [
'dependencies' => [],
'version' => GENERATECLOUD_VERSION,
];
wp_enqueue_script(
'generatecloud-licensing',
GENERATECLOUD_URL . 'dist/license-key.js',
$assets['dependencies'],
$assets['version'],
true
);
$license_key_data = get_option( 'generatecloud_licensing', [] );
$license_key = $license_key_data['key'] ?? '';
wp_localize_script(
'generatecloud-licensing',
'generateCloud',
[
'license' => [
'key' => $license_key ? '****************************' . substr( $license_key, -4 ) : '',
'status' => $license_key_data['status'] ?? '',
'beta' => $license_key_data['beta'] ?? false,
],
]
);
wp_set_script_translations(
'generatecloud-licensing',
'generatecloud',
GENERATECLOUD_DIR . 'languages'
);
wp_enqueue_style(
'generatecloud-licensing',
GENERATECLOUD_URL . 'dist/license-key.css',
array( 'wp-components' ),
GENERATECLOUD_VERSION
);
}
/**
* Add to our Dashboard pages.
*
* @since 1.0.0
* @param array $pages The existing pages.
*/
public function add_to_dashboard_pages( $pages ) {
$pages[] = 'generateblocks_page_generateblocks-public-keys';
return $pages;
}
/**
* Output our Dashboard HTML.
*
* @since 1.0.0
*/
public function public_keys() {
?>
<div class="wrap gblocks-dashboard-wrap">
<div class="generateblocks-settings-area generateblocks-public-keys-area">
<div id="gblocks-public-keys"></div>
</div>
</div>
<?php
}
/**
* Skip the trash when we delete public keys.
*
* @param int $post_id The post ID.
* @param WP_Post $post The post object.
*/
public function permanently_delete_keys( $post_id, $post ) {
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Check if it is an autosave or a revision.
if ( wp_is_post_autosave( $post_id ) || wp_is_post_revision( $post_id ) ) {
return;
}
if ( ! current_user_can( 'edit_post', $post_id ) ) {
return;
}
if ( 'trash' === $post->post_status ) {
wp_delete_post( $post_id, true );
}
}
/**
* Add license key container.
*
* @since 1.2.0
*/
public function add_license_key_area() {
echo '<div id="generatecloud-licensing"></div>';
}
}
@@ -0,0 +1,31 @@
<?php
/**
* The module class file.
*
* @package GenerateCloud\Modules
*/
namespace GenerateCloud\Modules;
use GenerateCloud\Utils\Singleton;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Module class.
*
* @since 1.0.0
*/
abstract class Module extends Singleton {
/**
* Loads the module actions and filters.
*
* @return void
*
* @since 1.0.0
*/
abstract public function load(): void;
}
@@ -0,0 +1,148 @@
<?php
/**
* The Public Keys post type class file.
*
* @package GenerateCloud
*/
namespace GenerateCloud\Modules;
use GenerateCloud\Modules\Module;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Public Keys post type.
*
* @since 1.0.0
*/
class Post_Type extends Module {
const POST_TYPE = 'gblocks_public_keys';
public function load(): void {
add_action( 'init', array( $this, 'register_post_type' ) );
}
/**
* Register the Cloud patterns CPT.
*
* @return void
*
* @since 1.0.0
*/
public function register_post_type(): void {
register_post_type(
self::POST_TYPE,
array(
'labels' => array(
'name' => _x( 'Public Keys', 'Post Type General Name', 'generatecloud' ),
'singular_name' => _x( 'Public Key', 'Post Type Singular Name', 'generatecloud' ),
'menu_name' => __( 'Public keys', 'generatecloud' ),
'parent_item_colon' => __( 'Parent Public Key', 'generatecloud' ),
'all_items' => __( 'Public Keys', 'generatecloud' ),
'view_item' => __( 'View Public Key', 'generatecloud' ),
'add_new_item' => __( 'Add New Public Key', 'generatecloud' ),
'add_new' => __( 'Add New Public Key', 'generatecloud' ),
'edit_item' => __( 'Edit Public Key', 'generatecloud' ),
'update_item' => __( 'Update Public Key', 'generatecloud' ),
'search_items' => __( 'Search Public Keys', 'generatecloud' ),
'not_found' => __( 'Not Found', 'generatecloud' ),
'not_found_in_trash' => __( 'Not found in Trash', 'generatecloud' ),
),
'public' => false,
'publicly_queryable' => false,
'has_archive' => false,
'show_ui' => false,
'exclude_from_search' => true,
'show_in_nav_menus' => false,
'rewrite' => false,
'hierarchical' => false,
'show_in_admin_bar' => false,
'show_in_rest' => true,
'supports' => array(),
'capabilities' => array(
'publish_posts' => 'manage_options',
'edit_posts' => 'manage_options',
'edit_others_posts' => 'manage_options',
'delete_posts' => 'manage_options',
'delete_others_posts' => 'manage_options',
'read_private_posts' => 'manage_options',
'edit_post' => 'manage_options',
'delete_post' => 'manage_options',
'read_post' => 'manage_options',
),
)
);
register_post_meta(
self::POST_TYPE,
'gb_public_key',
array(
'type' => 'string',
'default' => '',
'show_in_rest' => false,
)
);
register_rest_field(
self::POST_TYPE,
'public_key',
array(
'get_callback' => function( $data ) {
return get_post_meta( $data['id'], 'gb_public_key', true );
},
'update_callback' => function( $value, $post ) {
update_post_meta( $post->ID, 'gb_public_key', $value );
},
'schema' => array(
'type' => 'string',
'arg_options' => array(
'sanitize_callback' => function ( $value ) {
return sanitize_text_field( $value );
},
'validate_callback' => function ( $value ) {
return ! ! $value; // TODO: Add proper validation.
},
),
),
)
);
register_post_meta(
self::POST_TYPE,
'gb_permissions',
array(
'type' => 'object',
'default' => array(),
'show_in_rest' => false,
)
);
register_rest_field(
self::POST_TYPE,
'permissions',
array(
'get_callback' => function( $data ) {
return get_post_meta( $data['id'], 'gb_permissions', true );
},
'update_callback' => function( $value, $post ) {
update_post_meta( $post->ID, 'gb_permissions', $value );
},
'schema' => array(
'type' => 'object',
'arg_options' => array(
'sanitize_callback' => function ( $value ) {
return map_deep( $value, 'sanitize_text_field' );
},
'validate_callback' => function ( $value ) {
return is_array( $value );
},
),
),
)
);
}
}
@@ -0,0 +1,219 @@
<?php
/**
* The generative REST API endpoints for the proxy server.
*
* @package GenerateCloud
*/
namespace GenerateCloud\Modules;
use GenerateCloud\Modules\Module;
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
/**
* Public Keys post type.
*
* @since 1.0.0
*/
class Rest_Api extends Module {
/**
* Namespace.
*
* @var string
*/
protected $namespace = 'generatecloud/v';
/**
* Version.
*
* @var string
*/
protected $version = '1';
/**
* Load the module.
*/
public function load(): void {
add_action( 'rest_api_init', [ $this, 'register_routes' ] );
}
/**
* Register rest routes.
*/
public function register_routes() {
$namespace = $this->namespace . $this->version;
// Get list of Google fonts from the API or cached resource.
register_rest_route(
$namespace,
'/save-license-key/',
array(
'methods' => \WP_REST_Server::EDITABLE,
'callback' => array( $this, 'save_license_key' ),
'permission_callback' => function() {
return current_user_can( 'manage_options' );
},
)
);
}
/**
* Save the license key.
*
* @param \WP_REST_Request $request The request object.
*/
public function save_license_key( $request ) {
$new_license_data = $request->get_param( 'license' );
$license_settings = get_option( 'generatecloud_licensing', [] );
$old_license = $license_settings['key'] ?? '';
$old_status = $license_settings['status'] ?? '';
$new_license = trim( $new_license_data['key'] );
if ( $new_license ) {
$api_params = array(
'edd_action' => 'activate_license',
'license' => sanitize_key( $new_license ),
'item_name' => rawurlencode( 'GenerateCloud' ),
'url' => home_url(),
);
} elseif ( $old_license && 'valid' === $old_status ) {
$api_params = array(
'edd_action' => 'deactivate_license',
'license' => sanitize_key( $old_license ),
'item_name' => rawurlencode( 'GenerateCloud' ),
'url' => home_url(),
);
}
if ( isset( $api_params ) ) {
$response = wp_remote_post(
'https://generatepress.com',
array(
'timeout' => 15,
'sslverify' => false,
'body' => $api_params,
)
);
if ( is_wp_error( $response ) || 200 !== (int) wp_remote_retrieve_response_code( $response ) ) {
if ( is_wp_error( $response ) ) {
return $this->failed( $response->get_error_message() );
} else {
$message = __( 'An error occurred, please try again.', 'generatecloud' );
}
} else {
$license_data = json_decode( wp_remote_retrieve_body( $response ) );
if ( false === $license_data->success ) {
switch ( $license_data->error ) {
case 'expired':
$message = sprintf(
/* translators: License key expiration date. */
__( 'Your license key expired on %s.', 'generatecloud' ),
date_i18n( get_option( 'date_format' ), strtotime( $license_data->expires, current_time( 'timestamp' ) ) ) // phpcs:ignore
);
break;
case 'disabled':
case 'revoked':
$message = __( 'Your license key has been disabled.', 'generatecloud' );
break;
case 'missing':
$message = __( 'Invalid license.', 'generatecloud' );
break;
case 'invalid':
case 'site_inactive':
$message = __( 'Your license is not active for this URL.', 'generatecloud' );
break;
case 'item_name_mismatch':
$message = __( 'This appears to be an invalid license key for GenerateCloud.', 'generatecloud' );
break;
case 'no_activations_left':
$message = __( 'Your license key has reached its activation limit.', 'generatecloud' );
break;
default:
$message = __( 'An error occurred, please try again.', 'generatecloud' );
break;
}
}
}
$new_settings['status'] = esc_attr( $license_data->license );
}
$new_settings['key'] = sanitize_key( $new_license );
if ( is_array( $new_settings ) ) {
update_option( 'generatecloud_licensing', array_merge( $license_settings, $new_settings ) );
if ( ! isset( $api_params ) ) {
return $this->success( $license_data );
}
}
if ( ! empty( $message ) ) {
return $this->failed( $message );
}
return $this->success( $license_data );
}
/**
* Success rest.
*
* @param mixed $response response data.
* @return mixed
*/
public function success( $response ) {
return new \WP_REST_Response(
array(
'success' => true,
'response' => $response,
),
200
);
}
/**
* Failed rest.
*
* @param mixed $response response data.
* @return mixed
*/
public function failed( $response ) {
return new \WP_REST_Response(
array(
'success' => false,
'response' => $response,
),
200
);
}
/**
* Error rest.
*
* @param mixed $code error code.
* @param mixed $response response data.
* @return mixed
*/
public function error( $code, $response ) {
return new \WP_REST_Response(
array(
'error' => true,
'success' => false,
'error_code' => $code,
'response' => $response,
),
401
);
}
}