| 1 |
<?php |
| 2 |
|
| 3 |
// Prevent direct access to the file |
| 4 |
if ( ! defined( 'ABSPATH' ) ) { |
| 5 |
exit; // Exit if accessed directly |
| 6 |
} |
| 7 |
|
| 8 |
// Hook scripts function into block assets hook (iframe editor canvas). |
| 9 |
add_action( 'enqueue_block_assets', 'ctl_gutenberg_scripts' ); |
| 10 |
|
| 11 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound |
| 12 |
function ctl_gutenberg_scripts() { |
| 13 |
if ( ! is_admin() ) { |
| 14 |
return; |
| 15 |
} |
| 16 |
|
| 17 |
$blockPath = '/dist/block.js'; |
| 18 |
$stylePath = '/dist/block.css'; |
| 19 |
|
| 20 |
// Enqueue the bundled block JS file |
| 21 |
// phpcs:ignore WordPress.WP.EnqueuedResourceParameters.NotInFooter |
| 22 |
wp_enqueue_script( |
| 23 |
'ctl-block-js', |
| 24 |
plugins_url( $blockPath, __FILE__ ), |
| 25 |
array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-components', 'wp-block-editor', 'wp-data', 'wp-api' ), |
| 26 |
filemtime( plugin_dir_path( __FILE__ ) . $blockPath ) |
| 27 |
); |
| 28 |
|
| 29 |
// Enqueue frontend and editor block styles |
| 30 |
wp_enqueue_style( |
| 31 |
'ctl-block-css', |
| 32 |
plugins_url( $stylePath, __FILE__ ), |
| 33 |
array( 'wp-block-editor' ), |
| 34 |
filemtime( plugin_dir_path( __FILE__ ) . $stylePath ) |
| 35 |
); |
| 36 |
|
| 37 |
// Localize script for safe URL usage |
| 38 |
wp_localize_script( 'ctl-block-js', 'ctlUrl', array( esc_url( CTL_PLUGIN_URL ) ) ); // Escape URL |
| 39 |
} |
| 40 |
|
| 41 |
/** |
| 42 |
* Block Initializer. |
| 43 |
*/ |
| 44 |
add_action( |
| 45 |
'plugins_loaded', |
| 46 |
function () { |
| 47 |
if ( function_exists( 'register_block_type' ) ) { |
| 48 |
// Hook server side rendering into render callback |
| 49 |
register_block_type( |
| 50 |
'cool-timleine/shortcode-block', |
| 51 |
array( |
| 52 |
'api_version' => 3, |
| 53 |
'render_callback' => 'ctl_block_callback', |
| 54 |
'attributes' => array( |
| 55 |
'layout' => array( |
| 56 |
'type' => 'string', |
| 57 |
'default' => 'default', |
| 58 |
), |
| 59 |
'skin' => array( |
| 60 |
'type' => 'string', |
| 61 |
'default' => 'default', |
| 62 |
), |
| 63 |
'dateformat' => array( |
| 64 |
'type' => 'string', |
| 65 |
'default' => 'F j', |
| 66 |
), |
| 67 |
'postperpage' => array( |
| 68 |
'type' => 'string', |
| 69 |
'default' => 10, |
| 70 |
), |
| 71 |
'slideToShow' => array( |
| 72 |
'type' => 'string', |
| 73 |
'default' => '', |
| 74 |
), |
| 75 |
'animation' => array( |
| 76 |
'type' => 'string', |
| 77 |
'default' => 'none', |
| 78 |
), |
| 79 |
'icons' => array( |
| 80 |
'type' => 'string', |
| 81 |
'default' => 'NO', |
| 82 |
), |
| 83 |
'order' => array( |
| 84 |
'type' => 'string', |
| 85 |
'default' => 'DESC', |
| 86 |
), |
| 87 |
'storycontent' => array( |
| 88 |
'type' => 'string', |
| 89 |
'default' => 'short', |
| 90 |
), |
| 91 |
), |
| 92 |
) |
| 93 |
); |
| 94 |
} |
| 95 |
} |
| 96 |
); |
| 97 |
|
| 98 |
/** |
| 99 |
* Block Output. |
| 100 |
*/ |
| 101 |
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound |
| 102 |
function ctl_block_callback( $attr ) { |
| 103 |
$layout = isset( $attr['layout'] ) ? sanitize_text_field( $attr['layout'] ) : 'default'; |
| 104 |
$skin = isset( $attr['skin'] ) ? sanitize_text_field( $attr['skin'] ) : 'default'; |
| 105 |
$dateformat = isset( $attr['dateformat'] ) ? sanitize_text_field( $attr['dateformat'] ) : 'F j'; |
| 106 |
$postperpage = isset( $attr['postperpage'] ) && '-1' === trim( (string) $attr['postperpage'] ) ? '-1' : (string) max( 1, absint( isset( $attr['postperpage'] ) ? $attr['postperpage'] : 10 ) ); |
| 107 |
$slide_to_show = isset( $attr['slideToShow'] ) && '' !== $attr['slideToShow'] ? (string) max( 1, absint( $attr['slideToShow'] ) ) : ''; |
| 108 |
$animation = isset( $attr['animation'] ) ? sanitize_text_field( $attr['animation'] ) : 'none'; |
| 109 |
$icons = isset( $attr['icons'] ) ? sanitize_text_field( $attr['icons'] ) : 'NO'; |
| 110 |
$order = isset( $attr['order'] ) ? sanitize_text_field( $attr['order'] ) : 'DESC'; |
| 111 |
$storycontent = isset( $attr['storycontent'] ) ? sanitize_text_field( $attr['storycontent'] ) : 'short'; |
| 112 |
$shortcode_string = '[cool-timeline layout="%s" skin="%s" |
| 113 |
show-posts="%s" date-format="%s" icons="%s" animation="%s" order="%s" story-content="%s" items="%s"]'; |
| 114 |
|
| 115 |
return sprintf( |
| 116 |
$shortcode_string, |
| 117 |
esc_attr( $layout ), |
| 118 |
esc_attr( $skin ), |
| 119 |
esc_attr( $postperpage ), |
| 120 |
esc_attr( $dateformat ), |
| 121 |
esc_attr( $icons ), |
| 122 |
esc_attr( $animation ), |
| 123 |
esc_attr( $order ), |
| 124 |
esc_attr( $storycontent ), |
| 125 |
esc_attr( $slide_to_show ) |
| 126 |
); |
| 127 |
} |
| 128 |
|