| 1 |
<?php |
| 2 |
/** |
| 3 |
* Gutenberg class |
| 4 |
* |
| 5 |
* @author: themeum |
| 6 |
* @author_uri: https://themeum.com |
| 7 |
* @package Tutor |
| 8 |
* @since v.1.0.0 |
| 9 |
*/ |
| 10 |
|
| 11 |
|
| 12 |
namespace TUTOR; |
| 13 |
|
| 14 |
|
| 15 |
if ( ! defined( 'ABSPATH' ) ) |
| 16 |
exit; |
| 17 |
|
| 18 |
class Gutenberg { |
| 19 |
|
| 20 |
public function __construct() { |
| 21 |
if ( ! function_exists('register_block_type')){ |
| 22 |
return; |
| 23 |
} |
| 24 |
|
| 25 |
add_action( 'init', array($this, 'register_blocks') ); |
| 26 |
add_filter('block_categories_all', array($this, 'registering_new_block_category'), 10, 2); |
| 27 |
add_action('wp_ajax_render_block_tutor', array($this, 'render_block_tutor')); |
| 28 |
} |
| 29 |
|
| 30 |
function register_blocks() { |
| 31 |
global $pagenow; |
| 32 |
if ( 'widgets.php' !== $pagenow ) { |
| 33 |
wp_register_script( |
| 34 |
'tutor-student-registration-block', tutor()->url . 'assets/js/lib/gutenberg_blocks.js', array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-components', 'wp-editor' ) |
| 35 |
); |
| 36 |
} |
| 37 |
|
| 38 |
register_block_type( 'tutor-gutenberg/student-registration', array( |
| 39 |
'editor_script' => 'tutor-student-registration-block', |
| 40 |
'render_callback' => array($this, 'render_block_student_registration'), |
| 41 |
) ); |
| 42 |
/* |
| 43 |
register_block_type( 'tutor-gutenberg/student-dashboard', array( |
| 44 |
'editor_script' => 'tutor-student-registration-block', |
| 45 |
'render_callback' => array($this, 'render_block_tutor_dashboard'), |
| 46 |
) );*/ |
| 47 |
register_block_type( 'tutor-gutenberg/instructor-registration', array( |
| 48 |
'editor_script' => 'tutor-student-registration-block', |
| 49 |
'render_callback' => array($this, 'render_block_tutor_instructor_registration_form'), |
| 50 |
) ); |
| 51 |
|
| 52 |
// Check if WP version is equal to or greater than 5.9. |
| 53 |
global $wp_version; |
| 54 |
if ( version_compare( $wp_version, '5.9', '>=' ) && function_exists( 'wp_is_block_theme' ) && wp_is_block_theme() ) { |
| 55 |
wp_localize_script( 'tutor-student-registration-block', '_tutor_gutenberg_block_data', array( |
| 56 |
'is_wp_version_5_9' => 'true', |
| 57 |
) ); |
| 58 |
|
| 59 |
register_block_type( 'tutor-gutenberg/dashboard-menu', array( |
| 60 |
'editor_script' => 'tutor-student-registration-block', |
| 61 |
'render_callback' => array( $this, 'render_block_tutor_dashboard_menu' ), |
| 62 |
) ); |
| 63 |
} else { |
| 64 |
wp_localize_script( 'tutor-student-registration-block', '_tutor_gutenberg_block_data', array( |
| 65 |
'is_wp_version_5_9' => 'false', |
| 66 |
) ); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
public function registering_new_block_category($categories, $post ){ |
| 71 |
return array_merge( |
| 72 |
array( |
| 73 |
array( |
| 74 |
'slug' => 'tutor', |
| 75 |
'title' => __( 'Tutor LMS', 'tutor' ), |
| 76 |
), |
| 77 |
), |
| 78 |
$categories |
| 79 |
); |
| 80 |
} |
| 81 |
|
| 82 |
public function render_block_student_registration($args){ |
| 83 |
return do_shortcode("[tutor_student_registration_form]"); |
| 84 |
} |
| 85 |
public function render_block_tutor_dashboard($args){ |
| 86 |
return do_shortcode("[tutor_dashboard]"); |
| 87 |
} |
| 88 |
public function render_block_tutor_instructor_registration_form($args){ |
| 89 |
return do_shortcode("[tutor_instructor_registration_form]"); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Dashboard Menu for Block Based Themes and WP 5.9 |
| 94 |
*/ |
| 95 |
public function render_block_tutor_dashboard_menu( $args ) { |
| 96 |
return do_shortcode("[tutor_dashboard_menu]"); |
| 97 |
} |
| 98 |
|
| 99 |
//For editor |
| 100 |
public function render_block_tutor(){ |
| 101 |
tutor_utils()->checking_nonce(); |
| 102 |
|
| 103 |
$shortcode = sanitize_text_field($_POST['shortcode']); |
| 104 |
|
| 105 |
$allowed_shortcode = array( |
| 106 |
'tutor_instructor_registration_form', |
| 107 |
'tutor_student_registration_form', |
| 108 |
'tutor_dashboard_menu' |
| 109 |
); |
| 110 |
|
| 111 |
if(!in_array($shortcode, $allowed_shortcode)) { |
| 112 |
wp_send_json_error( ); |
| 113 |
} |
| 114 |
|
| 115 |
wp_send_json_success(do_shortcode("[{$shortcode}]")); |
| 116 |
} |
| 117 |
|
| 118 |
} |