| 1 |
<?php |
| 2 |
/** |
| 3 |
* Enqueue front end and editor JavaScript |
| 4 |
*/ |
| 5 |
function ays_poll_gutenberg_scripts() { |
| 6 |
global $current_screen; |
| 7 |
global $wp_version; |
| 8 |
$version1 = $wp_version; |
| 9 |
$operator = '>='; |
| 10 |
$version2 = '5.3.12'; |
| 11 |
$versionCompare = aysPollMakerVersionCompare($version1, $operator, $version2); |
| 12 |
|
| 13 |
if( ! $current_screen ){ |
| 14 |
return null; |
| 15 |
} |
| 16 |
|
| 17 |
if( ! $current_screen->is_block_editor ){ |
| 18 |
return null; |
| 19 |
} |
| 20 |
|
| 21 |
wp_enqueue_script("jquery-effects-core"); |
| 22 |
wp_enqueue_script('ays_block_select2js', esc_url(POLL_MAKER_AYS_ADMIN_URL) . '/js/select2.min.js', array('jquery'), '4.0.6', true); |
| 23 |
wp_enqueue_script(POLL_MAKER_AYS_NAME . '-autosize', esc_url(POLL_MAKER_AYS_PUBLIC_URL) . '/js/poll-maker-autosize.js', array( 'jquery' ), POLL_MAKER_AYS_VERSION, false ); |
| 24 |
wp_enqueue_script(POLL_MAKER_AYS_NAME, esc_url(POLL_MAKER_AYS_PUBLIC_URL) . '/js/poll-maker-ays-public.js', array('jquery'), POLL_MAKER_AYS_VERSION, false); |
| 25 |
wp_localize_script(POLL_MAKER_AYS_NAME . '-ajax-public', 'poll_maker_ajax_public', array('ajax_url' => admin_url('admin-ajax.php'))); |
| 26 |
|
| 27 |
// Enqueue the bundled block JS file |
| 28 |
$dependencies = array( 'jquery', 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-components' ); |
| 29 |
|
| 30 |
if ( ! ( defined( 'WP_CUSTOMIZE_WIDGETS' ) && WP_CUSTOMIZE_WIDGETS ) && ! did_action( 'enqueue_block_editor_assets' ) ) { |
| 31 |
$dependencies[] = 'wp-editor'; |
| 32 |
} |
| 33 |
|
| 34 |
wp_enqueue_script( |
| 35 |
'poll-maker-block-js', |
| 36 |
POLL_MAKER_AYS_BASE_URL . ($versionCompare ? "/poll/poll-maker-block-new.js" : "/poll/poll-maker-block.js"), |
| 37 |
$dependencies, |
| 38 |
POLL_MAKER_AYS_VERSION, |
| 39 |
true |
| 40 |
); |
| 41 |
|
| 42 |
// if( $versionCompare ){ |
| 43 |
// wp_enqueue_script( |
| 44 |
// 'poll-maker-block-js', |
| 45 |
// POLL_MAKER_AYS_BASE_URL ."/poll/poll-maker-block-new.js", |
| 46 |
// array( 'jquery', 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-components', 'wp-editor' ), |
| 47 |
// POLL_MAKER_AYS_VERSION, true |
| 48 |
// ); |
| 49 |
// } |
| 50 |
// else{ |
| 51 |
// wp_enqueue_script( |
| 52 |
// 'poll-maker-block-js', |
| 53 |
// POLL_MAKER_AYS_BASE_URL ."/poll/poll-maker-block.js", |
| 54 |
// array( 'jquery', 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-components', 'wp-editor' ), |
| 55 |
// POLL_MAKER_AYS_VERSION, true |
| 56 |
// ); |
| 57 |
// } |
| 58 |
|
| 59 |
wp_localize_script('ays-poll-gutenberg-block-js', 'ays_poll_block_ajax', array('aysDoShortCode' => admin_url('admin-ajax.php'))); |
| 60 |
|
| 61 |
wp_enqueue_style( POLL_MAKER_AYS_NAME . '-font-awesome', esc_url(POLL_MAKER_AYS_ADMIN_URL) . '/css/poll-maker-font-awesome-all.css', array(), POLL_MAKER_AYS_VERSION, 'all'); |
| 62 |
wp_enqueue_style('ays-block-animate', esc_url(POLL_MAKER_AYS_ADMIN_URL) . '/css/animate.min.css', array(), '2.0.6', 'all'); |
| 63 |
wp_enqueue_style('ays-block-select2', esc_url(POLL_MAKER_AYS_ADMIN_URL) . '/css/select2.min.css', array(), '4.0.6', 'all'); |
| 64 |
wp_enqueue_style(POLL_MAKER_AYS_NAME, esc_url(POLL_MAKER_AYS_PUBLIC_URL) . '/css/poll-maker-ays-public.css', array(), POLL_MAKER_AYS_VERSION, 'all'); |
| 65 |
|
| 66 |
// Enqueue the bundled block CSS file |
| 67 |
if( $versionCompare ){ |
| 68 |
wp_enqueue_style( |
| 69 |
'poll-maker-block-css', |
| 70 |
POLL_MAKER_AYS_BASE_URL ."/poll/poll-maker-block-new.css", |
| 71 |
array(), |
| 72 |
POLL_MAKER_AYS_VERSION, 'all' |
| 73 |
); |
| 74 |
} |
| 75 |
else{ |
| 76 |
wp_enqueue_style( |
| 77 |
'poll-maker-block-css', |
| 78 |
POLL_MAKER_AYS_BASE_URL ."/poll/poll-maker-block.css", |
| 79 |
array(), |
| 80 |
POLL_MAKER_AYS_VERSION, 'all' |
| 81 |
); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
function ays_poll_gutenberg_block_register() { |
| 86 |
|
| 87 |
global $wpdb; |
| 88 |
$block_name = 'poll'; |
| 89 |
$block_namespace = 'poll-maker/' . $block_name; |
| 90 |
|
| 91 |
$sql = "SELECT id, title FROM ". $wpdb->prefix . "ayspoll_polls ORDER BY id DESC"; |
| 92 |
$results = $wpdb->get_results($sql, "ARRAY_A"); |
| 93 |
|
| 94 |
register_block_type( |
| 95 |
$block_namespace, |
| 96 |
array( |
| 97 |
'render_callback' => 'pollmaker_render_callback', |
| 98 |
'editor_script' => 'poll-maker-block-js', // The block script slug |
| 99 |
'style' => 'poll-maker-block-css', |
| 100 |
'attributes' => array( |
| 101 |
'idner' => $results, |
| 102 |
'metaFieldValue' => array( |
| 103 |
'type' => 'integer', |
| 104 |
), |
| 105 |
'shortcode' => array( |
| 106 |
'type' => 'string', |
| 107 |
), |
| 108 |
'className' => array( |
| 109 |
'type' => 'string', |
| 110 |
), |
| 111 |
'openPopupId' => array( |
| 112 |
'type' => 'string', |
| 113 |
), |
| 114 |
), |
| 115 |
) |
| 116 |
); |
| 117 |
} |
| 118 |
|
| 119 |
function pollmaker_render_callback($attributes) { |
| 120 |
$ays_html = "<div class='ays-poll-render-callback-box'></div>"; |
| 121 |
if(isset($attributes["metaFieldValue"]) && $attributes["metaFieldValue"] === 0) { |
| 122 |
return $ays_html; |
| 123 |
} |
| 124 |
|
| 125 |
if(isset($attributes["shortcode"]) && $attributes["shortcode"] != '') { |
| 126 |
$ays_html = do_shortcode( $attributes["shortcode"] ); |
| 127 |
} |
| 128 |
return $ays_html; |
| 129 |
} |
| 130 |
|
| 131 |
function aysPollMakerVersionCompare($version1, $operator, $version2) { |
| 132 |
|
| 133 |
$_fv = intval ( trim ( str_replace ( '.', '', $version1 ) ) ); |
| 134 |
$_sv = intval ( trim ( str_replace ( '.', '', $version2 ) ) ); |
| 135 |
|
| 136 |
if (strlen ( $_fv ) > strlen ( $_sv )) { |
| 137 |
$_sv = str_pad ( $_sv, strlen ( $_fv ), 0 ); |
| 138 |
} |
| 139 |
|
| 140 |
if (strlen ( $_fv ) < strlen ( $_sv )) { |
| 141 |
$_fv = str_pad ( $_fv, strlen ( $_sv ), 0 ); |
| 142 |
} |
| 143 |
|
| 144 |
return version_compare ( ( string ) $_fv, ( string ) $_sv, $operator ); |
| 145 |
} |
| 146 |
|
| 147 |
if (function_exists("register_block_type")) { |
| 148 |
global $wp_version; |
| 149 |
|
| 150 |
$version1 = $wp_version; |
| 151 |
$operator = '>='; |
| 152 |
$version2 = '5.2'; |
| 153 |
$versionCompare = aysPollMakerVersionCompare($version1, $operator, $version2); |
| 154 |
|
| 155 |
if ( $versionCompare ) { |
| 156 |
// Hook scripts function into block editor hook |
| 157 |
add_action('enqueue_block_editor_assets', 'ays_poll_gutenberg_scripts'); |
| 158 |
|
| 159 |
if ( version_compare( get_bloginfo( 'version' ), '5.9', '>=' ) ) { |
| 160 |
add_action( 'enqueue_block_assets', 'ays_poll_gutenberg_scripts' ); |
| 161 |
} |
| 162 |
|
| 163 |
add_action('init', 'ays_poll_gutenberg_block_register'); |
| 164 |
} |
| 165 |
} |