| 1 |
<?php |
| 2 |
/** |
| 3 |
* Enqueue front end and editor JavaScript |
| 4 |
*/ |
| 5 |
|
| 6 |
function ays_survey_gutenberg_scripts() { |
| 7 |
global $current_screen; |
| 8 |
global $wp_version; |
| 9 |
$version1 = $wp_version; |
| 10 |
$operator = '>='; |
| 11 |
$version2 = '5.3'; |
| 12 |
$versionCompare = aysSurveyMakerVersionCompare($version1, $operator, $version2); |
| 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( SURVEY_MAKER_NAME . "-plugin", SURVEY_MAKER_PUBLIC_URL . '/js/survey-maker-public-plugin.js', array('jquery'), SURVEY_MAKER_VERSION, true); |
| 22 |
wp_enqueue_script( SURVEY_MAKER_NAME, SURVEY_MAKER_PUBLIC_URL . '/js/survey-maker-public.js', array('jquery'), SURVEY_MAKER_VERSION, true); |
| 23 |
|
| 24 |
// Enqueue the bundled block JS file |
| 25 |
if($versionCompare){ |
| 26 |
wp_enqueue_script( |
| 27 |
'survey-maker-block-js', |
| 28 |
SURVEY_MAKER_BASE_URL ."/survey/survey-maker-block-new.js", |
| 29 |
array( 'jquery', 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-components', 'wp-editor' ), |
| 30 |
SURVEY_MAKER_VERSION, true //( AYS_QUIZ_BASE_URL . 'quiz-maker-block.js' ) |
| 31 |
); |
| 32 |
} |
| 33 |
else{ |
| 34 |
wp_enqueue_script( |
| 35 |
'survey-maker-block-js', |
| 36 |
SURVEY_MAKER_BASE_URL ."/survey/survey-maker-block.js", |
| 37 |
array( 'jquery', 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-components', 'wp-editor' ), |
| 38 |
SURVEY_MAKER_VERSION, true //( AYS_QUIZ_BASE_URL . 'quiz-maker-block.js' ) |
| 39 |
); |
| 40 |
} |
| 41 |
// wp_enqueue_style( SURVEY_MAKER_NAME . '-select2', SURVEY_MAKER_PUBLIC_URL . '/css/survey-maker-select2.min.css', array(), SURVEY_MAKER_VERSION, 'all'); |
| 42 |
wp_enqueue_style( SURVEY_MAKER_NAME . "-dropdown", SURVEY_MAKER_PUBLIC_URL . '/css/dropdown.min.css', array(), SURVEY_MAKER_VERSION, 'all' ); |
| 43 |
wp_enqueue_style( SURVEY_MAKER_NAME . '-min', SURVEY_MAKER_PUBLIC_URL . '/css/survey-maker-public-min.css', array(), SURVEY_MAKER_VERSION, 'all'); |
| 44 |
|
| 45 |
// Enqueue the bundled block CSS file |
| 46 |
if(!$versionCompare){ |
| 47 |
wp_enqueue_style( |
| 48 |
'survey-maker-block-css', |
| 49 |
SURVEY_MAKER_BASE_URL ."/survey/survey-maker-block-new.css", |
| 50 |
array(), |
| 51 |
SURVEY_MAKER_VERSION, 'all' |
| 52 |
); |
| 53 |
} |
| 54 |
else{ |
| 55 |
wp_enqueue_style( |
| 56 |
'survey-maker-block-css', |
| 57 |
SURVEY_MAKER_BASE_URL ."/survey/survey-maker-block.css", |
| 58 |
array(), |
| 59 |
SURVEY_MAKER_VERSION, 'all' |
| 60 |
); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
function ays_survey_gutenberg_block_register() { |
| 65 |
|
| 66 |
global $wpdb; |
| 67 |
$block_name = 'survey'; |
| 68 |
$block_namespace = 'survey-maker/' . $block_name; |
| 69 |
|
| 70 |
$current_user = get_current_user_id(); |
| 71 |
$sql = "SELECT * FROM ". $wpdb->prefix . SURVEY_MAKER_DB_PREFIX . "surveys WHERE status != 'trashed' "; |
| 72 |
|
| 73 |
if( ! current_user_can( 'manage_options' ) ){ |
| 74 |
$sql .= " AND author_id = ". absint( $current_user ) ." "; |
| 75 |
} |
| 76 |
|
| 77 |
$sql .= "ORDER BY id DESC"; |
| 78 |
$results = $wpdb->get_results($sql, "ARRAY_A"); |
| 79 |
|
| 80 |
register_block_type( |
| 81 |
$block_namespace, |
| 82 |
array( |
| 83 |
'render_callback' => 'survey_maker_render_callback', |
| 84 |
'editor_script' => 'survey-maker-block-js', // The block script slug |
| 85 |
'style' => 'survey-maker-block-css', |
| 86 |
'attributes' => array( |
| 87 |
'idner' => $results, |
| 88 |
'metaFieldValue' => array( |
| 89 |
'type' => 'integer', |
| 90 |
), |
| 91 |
'shortcode' => array( |
| 92 |
'type' => 'string', |
| 93 |
), |
| 94 |
'className' => array( |
| 95 |
'type' => 'string', |
| 96 |
), |
| 97 |
'openPopupId' => array( |
| 98 |
'type' => 'string', |
| 99 |
), |
| 100 |
), |
| 101 |
) |
| 102 |
); |
| 103 |
} |
| 104 |
|
| 105 |
function survey_maker_render_callback( $attributes ) { |
| 106 |
global $current_screen; |
| 107 |
$is_front = true; |
| 108 |
|
| 109 |
if( ! empty( $current_screen ) ){ |
| 110 |
if( isset( $current_screen->is_block_editor ) && $current_screen->is_block_editor === true ){ |
| 111 |
$is_front = false; |
| 112 |
} |
| 113 |
}elseif ( wp_is_json_request() ) { |
| 114 |
$is_front = false; |
| 115 |
} |
| 116 |
|
| 117 |
$ays_html = "<div></div>"; |
| 118 |
|
| 119 |
if( ! empty( $attributes["shortcode"] ) ) { |
| 120 |
$ays_html = do_shortcode( $attributes["shortcode"] ); |
| 121 |
}else{ |
| 122 |
if( $is_front === true ){ |
| 123 |
$ays_html = ''; |
| 124 |
} |
| 125 |
} |
| 126 |
|
| 127 |
return $ays_html; |
| 128 |
} |
| 129 |
|
| 130 |
if(function_exists("register_block_type")){ |
| 131 |
// Hook scripts function into block editor hook |
| 132 |
add_action( 'enqueue_block_editor_assets', 'ays_survey_gutenberg_scripts' ); |
| 133 |
|
| 134 |
if ( version_compare( get_bloginfo( 'version' ), '5.9', '>=' ) ) { |
| 135 |
add_action( 'enqueue_block_assets', 'ays_survey_gutenberg_scripts' ); |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
add_action( 'init', 'ays_survey_gutenberg_block_register' ); |
| 140 |
} |
| 141 |
|
| 142 |
function aysSurveyMakerVersionCompare($version1, $operator, $version2) { |
| 143 |
|
| 144 |
$_fv = intval ( trim ( str_replace ( '.', '', $version1 ) ) ); |
| 145 |
$_sv = intval ( trim ( str_replace ( '.', '', $version2 ) ) ); |
| 146 |
|
| 147 |
if (strlen ( $_fv ) > strlen ( $_sv )) { |
| 148 |
$_sv = str_pad ( $_sv, strlen ( $_fv ), 0 ); |
| 149 |
} |
| 150 |
|
| 151 |
if (strlen ( $_fv ) < strlen ( $_sv )) { |
| 152 |
$_fv = str_pad ( $_fv, strlen ( $_sv ), 0 ); |
| 153 |
} |
| 154 |
|
| 155 |
return version_compare ( ( string ) $_fv, ( string ) $_sv, $operator ); |
| 156 |
} |
| 157 |
|