| 1 |
<?php |
| 2 |
/** |
| 3 |
* Manage Gutenberg |
| 4 |
* |
| 5 |
* @package Tutor |
| 6 |
* @author Themeum <support@themeum.com> |
| 7 |
* @link https://themeum.com |
| 8 |
* @since 1.0.0 |
| 9 |
*/ |
| 10 |
|
| 11 |
namespace TUTOR; |
| 12 |
|
| 13 |
if ( ! defined( 'ABSPATH' ) ) { |
| 14 |
exit; |
| 15 |
} |
| 16 |
|
| 17 |
/** |
| 18 |
* Gutenberg class |
| 19 |
* |
| 20 |
* @since 1.0.0 |
| 21 |
*/ |
| 22 |
class Gutenberg { |
| 23 |
/** |
| 24 |
* Constructor |
| 25 |
* |
| 26 |
* @since 1.0.0 |
| 27 |
* @return void|null |
| 28 |
*/ |
| 29 |
public function __construct() { |
| 30 |
if ( ! function_exists( 'register_block_type' ) ) { |
| 31 |
return; |
| 32 |
} |
| 33 |
|
| 34 |
add_action( 'init', array( $this, 'register_blocks' ) ); |
| 35 |
add_filter( 'block_categories_all', array( $this, 'registering_new_block_category' ), 10, 2 ); |
| 36 |
add_action( 'wp_ajax_render_block_tutor', array( $this, 'render_block_tutor' ) ); |
| 37 |
add_filter( 'rest_user_query', array( $this, 'author_list_dropdown' ), 10, 2 ); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* Bind author list on gutenberg editor mode |
| 42 |
* |
| 43 |
* @since 2.1.0 |
| 44 |
* |
| 45 |
* @param array $prepared_args arguments. |
| 46 |
* @param WP_REST_Request $request WP REST request object. |
| 47 |
* |
| 48 |
* @return array |
| 49 |
*/ |
| 50 |
public function author_list_dropdown( $prepared_args, $request ) { |
| 51 |
$url = $request->get_header( 'referer' ); |
| 52 |
$parts = parse_url( $url ); |
| 53 |
if ( ! isset( $parts['query'] ) ) { |
| 54 |
return $prepared_args; |
| 55 |
} |
| 56 |
|
| 57 |
parse_str( $parts['query'], $query ); |
| 58 |
$post_id = isset( $query['post'] ) ? (int) $query['post'] : 0; |
| 59 |
|
| 60 |
if ( ! $post_id ) { |
| 61 |
return $prepared_args; |
| 62 |
} |
| 63 |
|
| 64 |
$post = get_post( $post_id ); |
| 65 |
|
| 66 |
if ( tutor()->course_post_type === $post->post_type && tutor_utils()->get_option( 'enable_gutenberg_course_edit' ) === true ) { |
| 67 |
// Modify the wp/v2/users endpoint request from gutenberg editor. |
| 68 |
unset( $prepared_args['who'] ); |
| 69 |
$prepared_args['role__in'] = array( 'administrator', 'tutor_instructor' ); |
| 70 |
} |
| 71 |
|
| 72 |
return $prepared_args; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Register blocks |
| 77 |
* |
| 78 |
* @since 1.0.0 |
| 79 |
* @return void |
| 80 |
*/ |
| 81 |
public function register_blocks() { |
| 82 |
global $pagenow; |
| 83 |
if ( 'widgets.php' !== $pagenow ) { |
| 84 |
wp_register_script( |
| 85 |
'tutor-student-registration-block', |
| 86 |
tutor()->url . 'assets/lib/gutenberg_blocks.js', |
| 87 |
array( 'wp-blocks', 'wp-i18n', 'wp-element', 'wp-components', 'wp-editor' ), |
| 88 |
TUTOR_VERSION |
| 89 |
); |
| 90 |
} |
| 91 |
|
| 92 |
register_block_type( |
| 93 |
'tutor-gutenberg/student-registration', |
| 94 |
array( |
| 95 |
'editor_script' => 'tutor-student-registration-block', |
| 96 |
'render_callback' => array( $this, 'render_block_student_registration' ), |
| 97 |
) |
| 98 |
); |
| 99 |
|
| 100 |
register_block_type( |
| 101 |
'tutor-gutenberg/instructor-registration', |
| 102 |
array( |
| 103 |
'editor_script' => 'tutor-student-registration-block', |
| 104 |
'render_callback' => array( $this, 'render_block_tutor_instructor_registration_form' ), |
| 105 |
) |
| 106 |
); |
| 107 |
|
| 108 |
// Check if WP version is equal to or greater than 5.9. |
| 109 |
global $wp_version; |
| 110 |
if ( version_compare( $wp_version, '5.9', '>=' ) && function_exists( 'wp_is_block_theme' ) && wp_is_block_theme() ) { |
| 111 |
wp_localize_script( |
| 112 |
'tutor-student-registration-block', |
| 113 |
'_tutor_gutenberg_block_data', |
| 114 |
array( |
| 115 |
'is_wp_version_5_9' => 'true', |
| 116 |
) |
| 117 |
); |
| 118 |
} else { |
| 119 |
wp_localize_script( |
| 120 |
'tutor-student-registration-block', |
| 121 |
'_tutor_gutenberg_block_data', |
| 122 |
array( |
| 123 |
'is_wp_version_5_9' => 'false', |
| 124 |
) |
| 125 |
); |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Register new category block |
| 131 |
* |
| 132 |
* @since 1.0.0 |
| 133 |
* |
| 134 |
* @param array $categories categories. |
| 135 |
* @param object $post post object. |
| 136 |
* |
| 137 |
* @return array |
| 138 |
*/ |
| 139 |
public function registering_new_block_category( $categories, $post ) { |
| 140 |
return array_merge( |
| 141 |
array( |
| 142 |
array( |
| 143 |
'slug' => 'tutor', |
| 144 |
'title' => __( 'Tutor LMS', 'tutor' ), |
| 145 |
), |
| 146 |
), |
| 147 |
$categories |
| 148 |
); |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* Render student registration block |
| 153 |
* |
| 154 |
* @since 1.0.0 |
| 155 |
* |
| 156 |
* @param array $args arguments. |
| 157 |
* @return mixed |
| 158 |
*/ |
| 159 |
public function render_block_student_registration( $args ) { |
| 160 |
return do_shortcode( '[tutor_student_registration_form]' ); |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Render dashboard block |
| 165 |
* |
| 166 |
* @since 1.0.0 |
| 167 |
* |
| 168 |
* @param array $args arguments. |
| 169 |
* @return mixed |
| 170 |
*/ |
| 171 |
public function render_block_tutor_dashboard( $args ) { |
| 172 |
return do_shortcode( '[tutor_dashboard]' ); |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Render instructor registration block |
| 177 |
* |
| 178 |
* @since 1.0.0 |
| 179 |
* |
| 180 |
* @param array $args arguments. |
| 181 |
* @return mixed |
| 182 |
*/ |
| 183 |
public function render_block_tutor_instructor_registration_form( $args ) { |
| 184 |
return do_shortcode( '[tutor_instructor_registration_form]' ); |
| 185 |
} |
| 186 |
|
| 187 |
/** |
| 188 |
* Render tutor block for editor |
| 189 |
* |
| 190 |
* @since 1.0.0 |
| 191 |
* @return void |
| 192 |
*/ |
| 193 |
public function render_block_tutor() { |
| 194 |
tutor_utils()->checking_nonce(); |
| 195 |
|
| 196 |
$shortcode = Input::post( 'shortcode' ); |
| 197 |
|
| 198 |
$allowed_shortcode = array( |
| 199 |
'tutor_instructor_registration_form', |
| 200 |
'tutor_student_registration_form', |
| 201 |
); |
| 202 |
|
| 203 |
if ( ! in_array( $shortcode, $allowed_shortcode ) ) { |
| 204 |
wp_send_json_error(); |
| 205 |
} |
| 206 |
|
| 207 |
wp_send_json_success( do_shortcode( "[{$shortcode}]" ) ); |
| 208 |
} |
| 209 |
|
| 210 |
} |
| 211 |
|