| 1 |
<?php |
| 2 |
|
| 3 |
add_action( 'init', 'wpgetapi_init_block_editor_assets', 10, 0 ); |
| 4 |
|
| 5 |
function wpgetapi_init_block_editor_assets() { |
| 6 |
|
| 7 |
$assets = []; |
| 8 |
|
| 9 |
$asset_file = WPGETAPIDIR . 'includes/block-editor/index.asset.php'; |
| 10 |
|
| 11 |
if ( file_exists( $asset_file ) ) { |
| 12 |
$assets = include $asset_file; |
| 13 |
} |
| 14 |
|
| 15 |
$assets = wp_parse_args( $assets, [ |
| 16 |
'dependencies' => [ |
| 17 |
'wp-api-fetch', |
| 18 |
'wp-block-editor', |
| 19 |
'wp-blocks', |
| 20 |
'wp-components', |
| 21 |
'wp-element', |
| 22 |
'wp-i18n', |
| 23 |
'wp-url' |
| 24 |
], |
| 25 |
'version' => WPGETAPIVERSION |
| 26 |
] ); |
| 27 |
|
| 28 |
wp_register_script( |
| 29 |
'wpgetapi-block-editor', |
| 30 |
WPGETAPIURL . 'includes/block-editor/index.js', |
| 31 |
$assets['dependencies'], |
| 32 |
$assets['version'] . time() |
| 33 |
); |
| 34 |
|
| 35 |
wp_set_script_translations( |
| 36 |
'wpgetapi-block-editor', |
| 37 |
'wpgetapi' |
| 38 |
); |
| 39 |
|
| 40 |
$attributes = apply_filters( |
| 41 |
'wpgetapi_gutenberg_attributes', |
| 42 |
array( |
| 43 |
'api_id' => array( 'type' => 'string' ), |
| 44 |
'endpoint_id' => array( 'type' => 'string' ), |
| 45 |
'debug' => array( 'type' => 'string' ) |
| 46 |
) |
| 47 |
); |
| 48 |
|
| 49 |
$select_options = apply_filters( |
| 50 |
'wpgetapi_gutenberg_select_options', |
| 51 |
array( |
| 52 |
'format' => array( |
| 53 |
array( 'label' => 'Available in PRO plugin', 'value' => '' ), |
| 54 |
), |
| 55 |
'html_tags' => array( |
| 56 |
array( 'label' => 'Available in PRO plugin', 'value' => '' ), |
| 57 |
), |
| 58 |
'html_labels' => array( |
| 59 |
array( 'label' => 'Available in PRO plugin', 'value' => '' ), |
| 60 |
) |
| 61 |
) |
| 62 |
); |
| 63 |
|
| 64 |
register_block_type( |
| 65 |
WPGETAPIDIR . 'includes/block-editor', |
| 66 |
array( |
| 67 |
'editor_script' => 'wpgetapi-block-editor', |
| 68 |
'render_callback' => 'wpgetapi_get_gutenberg_html', |
| 69 |
'attributes' => $attributes, |
| 70 |
) |
| 71 |
); |
| 72 |
|
| 73 |
$data = []; |
| 74 |
$count = 0; |
| 75 |
$apis = get_option( 'wpgetapi_setup' ); |
| 76 |
|
| 77 |
if ( $apis ) { |
| 78 |
foreach ( $apis as $i => $api ) { |
| 79 |
if ( $api ) { |
| 80 |
foreach ( $api as $i2 => $item ) { |
| 81 |
$endpoints = get_option( 'wpgetapi_' . $item['id'] ); |
| 82 |
if ( isset( $endpoints['endpoints'] ) ) { |
| 83 |
foreach ( $endpoints['endpoints'] as $i3 => $endpoint ) { |
| 84 |
$data[$count]['id'] = $item['id'] . '_' . $endpoint['id']; |
| 85 |
$data[$count]['api_id'] = $item['id']; |
| 86 |
$data[$count]['endpoint_id'] = $endpoint['id']; |
| 87 |
$data[$count]['endpoint'] = $endpoint['endpoint']; |
| 88 |
$count++; |
| 89 |
} |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
wp_localize_script( |
| 97 |
'wpgetapi-block-editor', |
| 98 |
'wpgetapi_block_editor', |
| 99 |
array( |
| 100 |
'logo_url' => WPGETAPIURL . 'assets/img/wpgetapi-icon.png', |
| 101 |
'endpoints' => $data, |
| 102 |
'attributes' => $attributes, |
| 103 |
'select_options' => $select_options |
| 104 |
), |
| 105 |
'before' |
| 106 |
); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Get form HTML to display in a WPGetAPI Gutenberg block. |
| 111 |
* |
| 112 |
* @since 2.0.0 |
| 113 |
* |
| 114 |
* @param array $attr Attributes passed by WPGetAPI Gutenberg block. |
| 115 |
* |
| 116 |
* @return string |
| 117 |
*/ |
| 118 |
function wpgetapi_get_gutenberg_html( $attr, $content ) { |
| 119 |
|
| 120 |
$api_id = ! empty( $attr['api_id'] ) ? $attr['api_id'] : 0; |
| 121 |
$endpoint_id = ! empty( $attr['endpoint_id'] ) ? $attr['endpoint_id'] : 0; |
| 122 |
|
| 123 |
if ( empty( $api_id ) || empty( $endpoint_id ) ) { |
| 124 |
return 'no'; |
| 125 |
} |
| 126 |
|
| 127 |
ob_start(); |
| 128 |
|
| 129 |
/** |
| 130 |
* Fires before Gutenberg block output. |
| 131 |
* |
| 132 |
* @since 1.5.8.2 |
| 133 |
*/ |
| 134 |
do_action( 'wpgetapi_gutenberg_block_before' ); |
| 135 |
|
| 136 |
$debug = ! empty( $attr['debug'] ) ? $attr['debug'] : 'false'; |
| 137 |
$format = ! empty( $attr['format'] ) ? $attr['format'] : ''; |
| 138 |
$html_tag = ! empty( $attr['html_tag'] ) ? $attr['html_tag'] : ''; |
| 139 |
$html_labels = ! empty( $attr['html_labels'] ) ? $attr['html_labels'] : ''; |
| 140 |
$keys = ! empty( $attr['keys'] ) ? $attr['keys'] : ''; |
| 141 |
$query_variables = ! empty( $attr['query_variables'] ) ? $attr['query_variables'] : ''; |
| 142 |
$endpoint_variables = ! empty( $attr['endpoint_variables'] ) ? $attr['endpoint_variables'] : ''; |
| 143 |
|
| 144 |
$shortcode = sprintf( '[wpgetapi_endpoint |
| 145 |
api_id="' . $api_id . '" |
| 146 |
endpoint_id="' . $endpoint_id . '" |
| 147 |
debug="' . $debug . '" |
| 148 |
format="' . $format . '" |
| 149 |
html_tag="' . $html_tag . '" |
| 150 |
html_labels="' . $html_labels . '" |
| 151 |
keys="' . $keys . '" |
| 152 |
query_variables="' . $query_variables . '" |
| 153 |
endpoint_variables="' . $endpoint_variables . '" |
| 154 |
]' ); |
| 155 |
|
| 156 |
echo do_shortcode( shortcode_unautop( $shortcode ) ); |
| 157 |
|
| 158 |
/** |
| 159 |
* Fires after Gutenberg block output. |
| 160 |
* |
| 161 |
* @since 1.5.8.2 |
| 162 |
*/ |
| 163 |
do_action( 'wpgetapi_gutenberg_block_after' ); |
| 164 |
|
| 165 |
$content = ob_get_clean(); |
| 166 |
|
| 167 |
if ( empty( $content ) ) { |
| 168 |
$content = '<div class="components-placeholder"><div class="components-placeholder__label"></div>' . |
| 169 |
'<div class="components-placeholder__fieldset">' . |
| 170 |
esc_html__( 'The data cannot be displayed.', 'wpgetapi' ) . |
| 171 |
'</div></div>'; |
| 172 |
} |
| 173 |
|
| 174 |
/** |
| 175 |
* Filter Gutenberg block content. |
| 176 |
* |
| 177 |
* @since 1.5.8.2 |
| 178 |
* |
| 179 |
* @param string $content Block content. |
| 180 |
* @param int $id Form id. |
| 181 |
*/ |
| 182 |
return apply_filters( 'wpgetapi_gutenberg_block_content', $content, $api_id, $endpoint_id, $attr ); |
| 183 |
} |
| 184 |
|