| 1 |
<?php |
| 2 |
namespace BBlocks\Inc\Templates; |
| 3 |
|
| 4 |
if ( !defined( 'ABSPATH' ) ) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
require_once B_BLOCKS_DIR_PATH . '/includes/Templates/Image.php'; |
| 9 |
|
| 10 |
class Templates { |
| 11 |
public function __construct() { |
| 12 |
add_action( 'wp_ajax_bblocks_templates_main', [$this, 'bblocks_templates_main'] ); |
| 13 |
add_action( 'wp_ajax_bblocks_templates', [$this, 'bblocks_templates'] ); |
| 14 |
add_action( 'wp_ajax_bblocks_template_import', [$this, 'bblocks_template_import'] ); |
| 15 |
} |
| 16 |
|
| 17 |
public function bblocks_templates_main(){ |
| 18 |
$nonce = sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ?? null ) ); |
| 19 |
$type = sanitize_text_field( $_POST['type'] ?? 'pages'); |
| 20 |
|
| 21 |
if( !wp_verify_nonce( $nonce, 'wp_ajax' )){ |
| 22 |
wp_send_json_error( 'Invalid Request' ); |
| 23 |
} |
| 24 |
|
| 25 |
$typeFilter = 'pages' === $type ? 'pages-category': 'patterns-category'; |
| 26 |
// $response = wp_remote_get('https://templates.bplugins.com/wp-json/gutenberg-templates/v1/taxonomy/taxonomies/plugin,type,'.$typeFilter.'-category'); |
| 27 |
$response = wp_remote_get("https://templates.bplugins.com/wp-json/gutenberg-templates/v1/taxonomy/taxonomies/plugin,type,{$typeFilter}"); |
| 28 |
|
| 29 |
$body = json_decode( wp_remote_retrieve_body( $response ) ); |
| 30 |
wp_send_json_success( $body ); |
| 31 |
} |
| 32 |
|
| 33 |
public function bblocks_templates(){ |
| 34 |
$nonce = sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ?? null ) ); |
| 35 |
|
| 36 |
if( !wp_verify_nonce( $nonce, 'wp_ajax' )){ |
| 37 |
wp_send_json_error( 'Invalid Request' ); |
| 38 |
} |
| 39 |
|
| 40 |
$type = sanitize_text_field( wp_unslash( $_POST['type'] ?? 'pages') ) ; |
| 41 |
$category = sanitize_text_field( wp_unslash( $_POST['category'] ?? 'all' ) ); |
| 42 |
$pageNumber = absint( wp_unslash( $_POST['pageNumber'] ) ) ?? 1; |
| 43 |
$start = $pageNumber - 1; |
| 44 |
$search = sanitize_text_field( wp_unslash( $_POST['search'] ) ) ?? ''; |
| 45 |
|
| 46 |
$typeFilter = 'pages' === $type ? 'pages': 'patterns'; |
| 47 |
|
| 48 |
try { |
| 49 |
$response = wp_remote_get( "https://templates.bplugins.com/wp-json/gutenberg-templates/v1/blocks?type=$typeFilter&start=$start&end=$pageNumber&category=$category&keywords=$search&fields=ID,category,keywords,original_content,thumbnail,title,type,url" ); |
| 50 |
|
| 51 |
// old |
| 52 |
// $response = wp_remote_get( "https://templates.bplugins.com/wp-json/gutenberg-templates/v1/$typeFilter?start=$start&end=$pageNumber&category=$category&keywords=$search&fields=ID,category,keywords,original_content,thumbnail,title,type,url" ); |
| 53 |
|
| 54 |
$body = json_decode( wp_remote_retrieve_body( $response ) ); |
| 55 |
wp_send_json_success( $body ); |
| 56 |
} catch (\Throwable $th) { |
| 57 |
wp_send_json_error( $th->getMessage() ); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
public function bblocks_template_import(){ |
| 62 |
$nonce = sanitize_text_field( wp_unslash( $_POST['_wpnonce'] ) ) ?? null; |
| 63 |
|
| 64 |
if( !wp_verify_nonce( $nonce, 'wp_ajax' )){ |
| 65 |
wp_send_json_error( 'Invalid Request' ); |
| 66 |
} |
| 67 |
|
| 68 |
try { |
| 69 |
$data = Image::instance()->maybe_import_images( wp_unslash( $_POST['original_content'] ) ?? '' ); |
| 70 |
wp_send_json_success( $data ); |
| 71 |
} catch ( \Throwable $th ) { |
| 72 |
wp_send_json_error( $th->getMessage() ); |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
new Templates(); |