@@ -10,34 +10,34 @@ |
||
| 10 | 10 | namespace Wordlift\Faq; |
| 11 | 11 | |
| 12 | 12 | class Faq_Content_Filter { |
| 13 | - /** |
|
| 14 | - * Constants used for replacing the tags in the html string. |
|
| 15 | - */ |
|
| 16 | - const FAQ_QUESTION_TAG_NAME = 'wl-faq-question'; |
|
| 17 | - const FAQ_ANSWER_TAG_NAME = 'wl-faq-answer'; |
|
| 18 | - /** |
|
| 19 | - * Replaces all the html tags inserted by Faq highlighting code in the front end |
|
| 20 | - * @param $content string Post content |
|
| 21 | - * @return string String after replacing all the opening and closing tags. |
|
| 22 | - */ |
|
| 23 | - public function remove_all_faq_question_and_answer_tags( $content ) { |
|
| 24 | - /** |
|
| 25 | - * Replace all the question tags. |
|
| 26 | - */ |
|
| 27 | - $faq_question_opening_tag = '<' . self::FAQ_QUESTION_TAG_NAME . '>'; |
|
| 28 | - $faq_question_closing_tag = '</' . self::FAQ_QUESTION_TAG_NAME . '>'; |
|
| 29 | - $content = preg_replace('/<wl-faq-question class=".+?">/m', '', $content ); |
|
| 30 | - $content = str_replace($faq_question_closing_tag, "", $content); |
|
| 31 | - /** |
|
| 32 | - * Replace all the answer tags. |
|
| 33 | - */ |
|
| 34 | - $faq_answer_opening_tag = '<' . self::FAQ_ANSWER_TAG_NAME . '>'; |
|
| 35 | - $faq_answer_closing_tag = '</' . self::FAQ_ANSWER_TAG_NAME . '>'; |
|
| 36 | - $content = preg_replace('/<wl-faq-answer class=".+?">/m', '', $content ); |
|
| 37 | - $content = str_replace($faq_answer_closing_tag, "", $content); |
|
| 13 | + /** |
|
| 14 | + * Constants used for replacing the tags in the html string. |
|
| 15 | + */ |
|
| 16 | + const FAQ_QUESTION_TAG_NAME = 'wl-faq-question'; |
|
| 17 | + const FAQ_ANSWER_TAG_NAME = 'wl-faq-answer'; |
|
| 18 | + /** |
|
| 19 | + * Replaces all the html tags inserted by Faq highlighting code in the front end |
|
| 20 | + * @param $content string Post content |
|
| 21 | + * @return string String after replacing all the opening and closing tags. |
|
| 22 | + */ |
|
| 23 | + public function remove_all_faq_question_and_answer_tags( $content ) { |
|
| 24 | + /** |
|
| 25 | + * Replace all the question tags. |
|
| 26 | + */ |
|
| 27 | + $faq_question_opening_tag = '<' . self::FAQ_QUESTION_TAG_NAME . '>'; |
|
| 28 | + $faq_question_closing_tag = '</' . self::FAQ_QUESTION_TAG_NAME . '>'; |
|
| 29 | + $content = preg_replace('/<wl-faq-question class=".+?">/m', '', $content ); |
|
| 30 | + $content = str_replace($faq_question_closing_tag, "", $content); |
|
| 31 | + /** |
|
| 32 | + * Replace all the answer tags. |
|
| 33 | + */ |
|
| 34 | + $faq_answer_opening_tag = '<' . self::FAQ_ANSWER_TAG_NAME . '>'; |
|
| 35 | + $faq_answer_closing_tag = '</' . self::FAQ_ANSWER_TAG_NAME . '>'; |
|
| 36 | + $content = preg_replace('/<wl-faq-answer class=".+?">/m', '', $content ); |
|
| 37 | + $content = str_replace($faq_answer_closing_tag, "", $content); |
|
| 38 | 38 | |
| 39 | - /** Return all the replaced content */ |
|
| 40 | - return $content; |
|
| 41 | - } |
|
| 39 | + /** Return all the replaced content */ |
|
| 40 | + return $content; |
|
| 41 | + } |
|
| 42 | 42 | |
| 43 | 43 | } |
| 44 | 44 | \ No newline at end of file |
@@ -20,20 +20,20 @@ |
||
| 20 | 20 | * @param $content string Post content |
| 21 | 21 | * @return string String after replacing all the opening and closing tags. |
| 22 | 22 | */ |
| 23 | - public function remove_all_faq_question_and_answer_tags( $content ) { |
|
| 23 | + public function remove_all_faq_question_and_answer_tags($content) { |
|
| 24 | 24 | /** |
| 25 | 25 | * Replace all the question tags. |
| 26 | 26 | */ |
| 27 | - $faq_question_opening_tag = '<' . self::FAQ_QUESTION_TAG_NAME . '>'; |
|
| 28 | - $faq_question_closing_tag = '</' . self::FAQ_QUESTION_TAG_NAME . '>'; |
|
| 29 | - $content = preg_replace('/<wl-faq-question class=".+?">/m', '', $content ); |
|
| 27 | + $faq_question_opening_tag = '<'.self::FAQ_QUESTION_TAG_NAME.'>'; |
|
| 28 | + $faq_question_closing_tag = '</'.self::FAQ_QUESTION_TAG_NAME.'>'; |
|
| 29 | + $content = preg_replace('/<wl-faq-question class=".+?">/m', '', $content); |
|
| 30 | 30 | $content = str_replace($faq_question_closing_tag, "", $content); |
| 31 | 31 | /** |
| 32 | 32 | * Replace all the answer tags. |
| 33 | 33 | */ |
| 34 | - $faq_answer_opening_tag = '<' . self::FAQ_ANSWER_TAG_NAME . '>'; |
|
| 35 | - $faq_answer_closing_tag = '</' . self::FAQ_ANSWER_TAG_NAME . '>'; |
|
| 36 | - $content = preg_replace('/<wl-faq-answer class=".+?">/m', '', $content ); |
|
| 34 | + $faq_answer_opening_tag = '<'.self::FAQ_ANSWER_TAG_NAME.'>'; |
|
| 35 | + $faq_answer_closing_tag = '</'.self::FAQ_ANSWER_TAG_NAME.'>'; |
|
| 36 | + $content = preg_replace('/<wl-faq-answer class=".+?">/m', '', $content); |
|
| 37 | 37 | $content = str_replace($faq_answer_closing_tag, "", $content); |
| 38 | 38 | |
| 39 | 39 | /** Return all the replaced content */ |
@@ -13,38 +13,38 @@ |
||
| 13 | 13 | |
| 14 | 14 | class Faq_Tinymce_Adapter { |
| 15 | 15 | |
| 16 | - const FAQ_TINYMCE_PLUGIN_NAME = "wl_faq_tinymce"; |
|
| 17 | - |
|
| 18 | - const FAQ_TINYMCE_ADD_BUTTON_ID = "wl-faq-toolbar-button"; |
|
| 19 | - |
|
| 20 | - /** |
|
| 21 | - * Add a list of custom tags which is to be used by our highlighting program. |
|
| 22 | - * |
|
| 23 | - * @param $init_array |
|
| 24 | - * |
|
| 25 | - * @return array |
|
| 26 | - */ |
|
| 27 | - public function register_custom_tags($init_array) { |
|
| 28 | - $opts = '~wl-faq-question,~wl-faq-answer'; |
|
| 29 | - $init_array['custom_elements'] = $opts; |
|
| 30 | - $init_array['extended_valid_elements'] = $opts; |
|
| 31 | - return $init_array; |
|
| 32 | - } |
|
| 33 | - public function register_faq_tinymce_plugin( $plugins ) { |
|
| 34 | - /** |
|
| 35 | - * Registering the tinymce plugin for FAQ here. |
|
| 36 | - * @since 3.26.0 |
|
| 37 | - */ |
|
| 38 | - $version = Wordlift::get_instance()->get_version(); |
|
| 39 | - $plugins[ self::FAQ_TINYMCE_PLUGIN_NAME ] = plugin_dir_url( dirname( __DIR__ ) ) . 'js/dist/tinymce-faq-plugin.full.js?ver=' . $version; |
|
| 40 | - |
|
| 41 | - return $plugins; |
|
| 42 | - } |
|
| 43 | - |
|
| 44 | - public function register_faq_toolbar_button( $buttons ) { |
|
| 45 | - array_push( $buttons, self::FAQ_TINYMCE_ADD_BUTTON_ID ); |
|
| 46 | - |
|
| 47 | - return $buttons; |
|
| 48 | - } |
|
| 16 | + const FAQ_TINYMCE_PLUGIN_NAME = "wl_faq_tinymce"; |
|
| 17 | + |
|
| 18 | + const FAQ_TINYMCE_ADD_BUTTON_ID = "wl-faq-toolbar-button"; |
|
| 19 | + |
|
| 20 | + /** |
|
| 21 | + * Add a list of custom tags which is to be used by our highlighting program. |
|
| 22 | + * |
|
| 23 | + * @param $init_array |
|
| 24 | + * |
|
| 25 | + * @return array |
|
| 26 | + */ |
|
| 27 | + public function register_custom_tags($init_array) { |
|
| 28 | + $opts = '~wl-faq-question,~wl-faq-answer'; |
|
| 29 | + $init_array['custom_elements'] = $opts; |
|
| 30 | + $init_array['extended_valid_elements'] = $opts; |
|
| 31 | + return $init_array; |
|
| 32 | + } |
|
| 33 | + public function register_faq_tinymce_plugin( $plugins ) { |
|
| 34 | + /** |
|
| 35 | + * Registering the tinymce plugin for FAQ here. |
|
| 36 | + * @since 3.26.0 |
|
| 37 | + */ |
|
| 38 | + $version = Wordlift::get_instance()->get_version(); |
|
| 39 | + $plugins[ self::FAQ_TINYMCE_PLUGIN_NAME ] = plugin_dir_url( dirname( __DIR__ ) ) . 'js/dist/tinymce-faq-plugin.full.js?ver=' . $version; |
|
| 40 | + |
|
| 41 | + return $plugins; |
|
| 42 | + } |
|
| 43 | + |
|
| 44 | + public function register_faq_toolbar_button( $buttons ) { |
|
| 45 | + array_push( $buttons, self::FAQ_TINYMCE_ADD_BUTTON_ID ); |
|
| 46 | + |
|
| 47 | + return $buttons; |
|
| 48 | + } |
|
| 49 | 49 | |
| 50 | 50 | } |
@@ -30,19 +30,19 @@ |
||
| 30 | 30 | $init_array['extended_valid_elements'] = $opts; |
| 31 | 31 | return $init_array; |
| 32 | 32 | } |
| 33 | - public function register_faq_tinymce_plugin( $plugins ) { |
|
| 33 | + public function register_faq_tinymce_plugin($plugins) { |
|
| 34 | 34 | /** |
| 35 | 35 | * Registering the tinymce plugin for FAQ here. |
| 36 | 36 | * @since 3.26.0 |
| 37 | 37 | */ |
| 38 | 38 | $version = Wordlift::get_instance()->get_version(); |
| 39 | - $plugins[ self::FAQ_TINYMCE_PLUGIN_NAME ] = plugin_dir_url( dirname( __DIR__ ) ) . 'js/dist/tinymce-faq-plugin.full.js?ver=' . $version; |
|
| 39 | + $plugins[self::FAQ_TINYMCE_PLUGIN_NAME] = plugin_dir_url(dirname(__DIR__)).'js/dist/tinymce-faq-plugin.full.js?ver='.$version; |
|
| 40 | 40 | |
| 41 | 41 | return $plugins; |
| 42 | 42 | } |
| 43 | 43 | |
| 44 | - public function register_faq_toolbar_button( $buttons ) { |
|
| 45 | - array_push( $buttons, self::FAQ_TINYMCE_ADD_BUTTON_ID ); |
|
| 44 | + public function register_faq_toolbar_button($buttons) { |
|
| 45 | + array_push($buttons, self::FAQ_TINYMCE_ADD_BUTTON_ID); |
|
| 46 | 46 | |
| 47 | 47 | return $buttons; |
| 48 | 48 | } |
@@ -17,232 +17,232 @@ |
||
| 17 | 17 | * |
| 18 | 18 | */ |
| 19 | 19 | class Faq_Rest_Controller { |
| 20 | - const FAQ_META_KEY = '_wl_faq'; |
|
| 21 | - /** |
|
| 22 | - * Enumerations to determine the field to be deleted on |
|
| 23 | - * the DELETE request in API. |
|
| 24 | - */ |
|
| 25 | - const QUESTION = 'question'; |
|
| 26 | - const ANSWER = 'answer'; |
|
| 27 | - |
|
| 28 | - public static function register_routes() { |
|
| 29 | - add_action( 'rest_api_init', 'Wordlift\FAQ\FAQ_Rest_Controller::register_route_callback' ); |
|
| 30 | - } |
|
| 31 | - |
|
| 32 | - public static function register_route_callback() { |
|
| 33 | - /** |
|
| 34 | - * Specifies the list of arguments to be present for update request. |
|
| 35 | - */ |
|
| 36 | - $post_id_validation_settings = array( |
|
| 37 | - 'required' => TRUE, |
|
| 38 | - 'validate_callback' => function ( $param, $request, $key ) { |
|
| 39 | - return is_numeric( $param ); |
|
| 40 | - } |
|
| 41 | - ); |
|
| 42 | - $faq_items_validation_settings = array( |
|
| 43 | - 'required' => TRUE, |
|
| 44 | - 'validate_callback' => function ( $param, $request, $key ) { |
|
| 45 | - return is_array( $param ) && count( $param ) > 0; |
|
| 46 | - } |
|
| 47 | - ); |
|
| 48 | - /** |
|
| 49 | - * Array of args to be present in order to |
|
| 50 | - * make create, update or delete request. |
|
| 51 | - */ |
|
| 52 | - $create_or_update_or_delete_args = array( |
|
| 53 | - 'post_id' => $post_id_validation_settings, |
|
| 54 | - 'faq_items' => $faq_items_validation_settings |
|
| 55 | - ); |
|
| 56 | - |
|
| 57 | - /** |
|
| 58 | - * Rest route for creating new faq item. |
|
| 59 | - */ |
|
| 60 | - register_rest_route( |
|
| 61 | - WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
| 62 | - '/faq', |
|
| 63 | - array( |
|
| 64 | - 'methods' => \WP_REST_Server::CREATABLE, |
|
| 65 | - 'callback' => 'Wordlift\Faq\Faq_Rest_Controller::add_faq_items', |
|
| 66 | - 'permission_callback' => function () { |
|
| 67 | - return current_user_can( 'publish_posts' ); |
|
| 68 | - }, |
|
| 69 | - 'args' => $create_or_update_or_delete_args |
|
| 70 | - ) ); |
|
| 71 | - /** |
|
| 72 | - * Rest route for updating faq items. |
|
| 73 | - */ |
|
| 74 | - register_rest_route( |
|
| 75 | - WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
| 76 | - '/faq', |
|
| 77 | - array( |
|
| 78 | - 'methods' => \WP_REST_Server::EDITABLE, |
|
| 79 | - 'callback' => 'Wordlift\Faq\Faq_Rest_Controller::update_faq_items', |
|
| 80 | - 'permission_callback' => function () { |
|
| 81 | - return current_user_can( 'publish_posts' ); |
|
| 82 | - }, |
|
| 83 | - 'args' => $create_or_update_or_delete_args |
|
| 84 | - ) |
|
| 85 | - ); |
|
| 86 | - /** |
|
| 87 | - * Rest route for getting the faq items. |
|
| 88 | - */ |
|
| 89 | - register_rest_route( |
|
| 90 | - WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
| 91 | - '/faq/(?P<post_id>\d+)', |
|
| 92 | - array( |
|
| 93 | - 'methods' => \WP_REST_Server::READABLE, |
|
| 94 | - 'callback' => 'Wordlift\Faq\Faq_Rest_Controller::get_faq_items', |
|
| 95 | - 'permission_callback' => function () { |
|
| 96 | - return current_user_can( 'publish_posts' ); |
|
| 97 | - }, |
|
| 98 | - 'args' => array( 'post_id' => $post_id_validation_settings ) |
|
| 99 | - ) |
|
| 100 | - ); |
|
| 101 | - /** |
|
| 102 | - * Rest route for deleting faq item. |
|
| 103 | - */ |
|
| 104 | - register_rest_route( |
|
| 105 | - WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
| 106 | - '/faq', |
|
| 107 | - array( |
|
| 108 | - 'methods' => \WP_REST_Server::DELETABLE, |
|
| 109 | - 'callback' => 'Wordlift\Faq\Faq_Rest_Controller::delete_faq_items', |
|
| 110 | - 'permission_callback' => function () { |
|
| 111 | - return current_user_can( 'publish_posts' ); |
|
| 112 | - }, |
|
| 113 | - 'args' => $create_or_update_or_delete_args |
|
| 114 | - ) |
|
| 115 | - ); |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * Get all FAQ items for a post id. |
|
| 120 | - * |
|
| 121 | - * @param $request $request WP_REST_Request $request {@link WP_REST_Request instance}. |
|
| 122 | - * |
|
| 123 | - * @return array Result array, if post id is not given then error array is returned. |
|
| 124 | - */ |
|
| 125 | - public static function get_faq_items( $request ) { |
|
| 126 | - $data = $request->get_params(); |
|
| 127 | - $post_id = (int) $data['post_id']; |
|
| 128 | - |
|
| 129 | - return get_post_meta( $post_id, self::FAQ_META_KEY ); |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * Update all FAQ items for a post id. |
|
| 134 | - * |
|
| 135 | - * @param $request $request WP_REST_Request $request {@link WP_REST_Request instance}. |
|
| 136 | - * |
|
| 137 | - * @return array Result array, if post id is not given then error array is returned. |
|
| 138 | - */ |
|
| 139 | - public static function update_faq_items( $request ) { |
|
| 140 | - $data = $request->get_params(); |
|
| 141 | - $post_id = (int) $data['post_id']; |
|
| 142 | - $faq_items = (array) $data['faq_items']; |
|
| 143 | - foreach ( $faq_items as $faq_item ) { |
|
| 144 | - $previous_value = array( |
|
| 145 | - 'question' => (string) $faq_item['previous_question_value'], |
|
| 146 | - 'answer' => (string) $faq_item['previous_answer_value'], |
|
| 147 | - 'id' => (int) $faq_item['id'] |
|
| 148 | - ); |
|
| 149 | - $new_value = array( |
|
| 150 | - 'question' => (string) $faq_item['question'], |
|
| 151 | - 'answer' => (string) $faq_item['answer'], |
|
| 152 | - 'id' => (int) $faq_item['id'] |
|
| 153 | - ); |
|
| 154 | - update_post_meta( $post_id, self::FAQ_META_KEY, $new_value, $previous_value ); |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - return array( |
|
| 158 | - 'status' => 'success', |
|
| 159 | - 'message' => __( 'Faq Items updated successfully' ) |
|
| 160 | - ); |
|
| 161 | - |
|
| 162 | - } |
|
| 163 | - |
|
| 164 | - /** |
|
| 165 | - * Delete Faq items. |
|
| 166 | - * |
|
| 167 | - * @param $request WP_REST_Request $request {@link WP_REST_Request instance}. |
|
| 168 | - * |
|
| 169 | - * @return array Associative array whether the faq item is deleted or not. |
|
| 170 | - */ |
|
| 171 | - public static function delete_faq_items( $request ) { |
|
| 172 | - $post_data = $request->get_params(); |
|
| 173 | - $post_id = $post_data['post_id']; |
|
| 174 | - $faq_items = $post_data['faq_items']; |
|
| 175 | - foreach ( $faq_items as $faq_item ) { |
|
| 176 | - /** |
|
| 177 | - * Note: the order of keys is important in order to delete it properly |
|
| 178 | - * If the order change, delete operation will fail since it is converted |
|
| 179 | - * in to a string when it was stored. |
|
| 180 | - * we cant rely on client to post it in correct order, so we create an array |
|
| 181 | - * in correct order. |
|
| 182 | - **/ |
|
| 183 | - $deleted_faq_item = array( |
|
| 184 | - 'question' => $faq_item['question'], |
|
| 185 | - 'answer' => $faq_item['answer'], |
|
| 186 | - 'id' => (int) $faq_item['id'] |
|
| 187 | - ); |
|
| 188 | - /** |
|
| 189 | - * If the field to be deleted is answer, then dont delete the faq item, |
|
| 190 | - * if it is question then delete the faq item. |
|
| 191 | - */ |
|
| 192 | - if ( $faq_item['field_to_be_deleted'] === self::ANSWER) { |
|
| 193 | - $previous_value = $deleted_faq_item; |
|
| 194 | - // then dont delete the faq item, set the answer as empty. |
|
| 195 | - $deleted_faq_item['answer'] = ''; |
|
| 196 | - $new_value = $deleted_faq_item; |
|
| 197 | - update_post_meta($post_id, self::FAQ_META_KEY, $new_value, $previous_value); |
|
| 198 | - } |
|
| 199 | - else if ( $faq_item['field_to_be_deleted'] === self::QUESTION) { |
|
| 200 | - /** |
|
| 201 | - * If the question is deleted, then delete the faq item. |
|
| 202 | - */ |
|
| 203 | - delete_post_meta( $post_id, self::FAQ_META_KEY, $deleted_faq_item ); |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - } |
|
| 207 | - |
|
| 208 | - /** |
|
| 209 | - * We are returning only the first item id, since the user can select only one text at a time. |
|
| 210 | - */ |
|
| 211 | - return array( |
|
| 212 | - 'status' => 'success', |
|
| 213 | - 'message' => __( 'Faq item successfully deleted.' ), |
|
| 214 | - ); |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - * Insert or update FAQ items. |
|
| 219 | - * |
|
| 220 | - * @param $request WP_REST_Request $request {@link WP_REST_Request instance}. |
|
| 221 | - * |
|
| 222 | - * @return array Associative array whether the faq item is inserted or not |
|
| 223 | - */ |
|
| 224 | - public static function add_faq_items( $request ) { |
|
| 225 | - $post_data = $request->get_params(); |
|
| 226 | - $post_id = $post_data['post_id']; |
|
| 227 | - $faq_items = $post_data['faq_items']; |
|
| 228 | - foreach ( $faq_items as &$faq_item ) { |
|
| 229 | - // Add an identifier id to the faq item, it helps to prevent duplication problem. |
|
| 230 | - /** |
|
| 231 | - * We are using time() and a random integer to prevent |
|
| 232 | - * duplication problem. |
|
| 233 | - */ |
|
| 234 | - $faq_item['id'] = time() + rand( 1, 100 ) + rand( 1, 100 ); |
|
| 235 | - add_post_meta( (int) $post_id, self::FAQ_META_KEY, $faq_item ); |
|
| 236 | - } |
|
| 237 | - |
|
| 238 | - /** |
|
| 239 | - * We are returning only the first item id, since the user can select only one text at a time. |
|
| 240 | - */ |
|
| 241 | - return array( |
|
| 242 | - 'status' => 'success', |
|
| 243 | - 'message' => __( 'Question successfully added.' ), |
|
| 244 | - 'id' => (int) $faq_items[0]['id'], |
|
| 245 | - ); |
|
| 246 | - |
|
| 247 | - } |
|
| 20 | + const FAQ_META_KEY = '_wl_faq'; |
|
| 21 | + /** |
|
| 22 | + * Enumerations to determine the field to be deleted on |
|
| 23 | + * the DELETE request in API. |
|
| 24 | + */ |
|
| 25 | + const QUESTION = 'question'; |
|
| 26 | + const ANSWER = 'answer'; |
|
| 27 | + |
|
| 28 | + public static function register_routes() { |
|
| 29 | + add_action( 'rest_api_init', 'Wordlift\FAQ\FAQ_Rest_Controller::register_route_callback' ); |
|
| 30 | + } |
|
| 31 | + |
|
| 32 | + public static function register_route_callback() { |
|
| 33 | + /** |
|
| 34 | + * Specifies the list of arguments to be present for update request. |
|
| 35 | + */ |
|
| 36 | + $post_id_validation_settings = array( |
|
| 37 | + 'required' => TRUE, |
|
| 38 | + 'validate_callback' => function ( $param, $request, $key ) { |
|
| 39 | + return is_numeric( $param ); |
|
| 40 | + } |
|
| 41 | + ); |
|
| 42 | + $faq_items_validation_settings = array( |
|
| 43 | + 'required' => TRUE, |
|
| 44 | + 'validate_callback' => function ( $param, $request, $key ) { |
|
| 45 | + return is_array( $param ) && count( $param ) > 0; |
|
| 46 | + } |
|
| 47 | + ); |
|
| 48 | + /** |
|
| 49 | + * Array of args to be present in order to |
|
| 50 | + * make create, update or delete request. |
|
| 51 | + */ |
|
| 52 | + $create_or_update_or_delete_args = array( |
|
| 53 | + 'post_id' => $post_id_validation_settings, |
|
| 54 | + 'faq_items' => $faq_items_validation_settings |
|
| 55 | + ); |
|
| 56 | + |
|
| 57 | + /** |
|
| 58 | + * Rest route for creating new faq item. |
|
| 59 | + */ |
|
| 60 | + register_rest_route( |
|
| 61 | + WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
| 62 | + '/faq', |
|
| 63 | + array( |
|
| 64 | + 'methods' => \WP_REST_Server::CREATABLE, |
|
| 65 | + 'callback' => 'Wordlift\Faq\Faq_Rest_Controller::add_faq_items', |
|
| 66 | + 'permission_callback' => function () { |
|
| 67 | + return current_user_can( 'publish_posts' ); |
|
| 68 | + }, |
|
| 69 | + 'args' => $create_or_update_or_delete_args |
|
| 70 | + ) ); |
|
| 71 | + /** |
|
| 72 | + * Rest route for updating faq items. |
|
| 73 | + */ |
|
| 74 | + register_rest_route( |
|
| 75 | + WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
| 76 | + '/faq', |
|
| 77 | + array( |
|
| 78 | + 'methods' => \WP_REST_Server::EDITABLE, |
|
| 79 | + 'callback' => 'Wordlift\Faq\Faq_Rest_Controller::update_faq_items', |
|
| 80 | + 'permission_callback' => function () { |
|
| 81 | + return current_user_can( 'publish_posts' ); |
|
| 82 | + }, |
|
| 83 | + 'args' => $create_or_update_or_delete_args |
|
| 84 | + ) |
|
| 85 | + ); |
|
| 86 | + /** |
|
| 87 | + * Rest route for getting the faq items. |
|
| 88 | + */ |
|
| 89 | + register_rest_route( |
|
| 90 | + WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
| 91 | + '/faq/(?P<post_id>\d+)', |
|
| 92 | + array( |
|
| 93 | + 'methods' => \WP_REST_Server::READABLE, |
|
| 94 | + 'callback' => 'Wordlift\Faq\Faq_Rest_Controller::get_faq_items', |
|
| 95 | + 'permission_callback' => function () { |
|
| 96 | + return current_user_can( 'publish_posts' ); |
|
| 97 | + }, |
|
| 98 | + 'args' => array( 'post_id' => $post_id_validation_settings ) |
|
| 99 | + ) |
|
| 100 | + ); |
|
| 101 | + /** |
|
| 102 | + * Rest route for deleting faq item. |
|
| 103 | + */ |
|
| 104 | + register_rest_route( |
|
| 105 | + WL_REST_ROUTE_DEFAULT_NAMESPACE, |
|
| 106 | + '/faq', |
|
| 107 | + array( |
|
| 108 | + 'methods' => \WP_REST_Server::DELETABLE, |
|
| 109 | + 'callback' => 'Wordlift\Faq\Faq_Rest_Controller::delete_faq_items', |
|
| 110 | + 'permission_callback' => function () { |
|
| 111 | + return current_user_can( 'publish_posts' ); |
|
| 112 | + }, |
|
| 113 | + 'args' => $create_or_update_or_delete_args |
|
| 114 | + ) |
|
| 115 | + ); |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * Get all FAQ items for a post id. |
|
| 120 | + * |
|
| 121 | + * @param $request $request WP_REST_Request $request {@link WP_REST_Request instance}. |
|
| 122 | + * |
|
| 123 | + * @return array Result array, if post id is not given then error array is returned. |
|
| 124 | + */ |
|
| 125 | + public static function get_faq_items( $request ) { |
|
| 126 | + $data = $request->get_params(); |
|
| 127 | + $post_id = (int) $data['post_id']; |
|
| 128 | + |
|
| 129 | + return get_post_meta( $post_id, self::FAQ_META_KEY ); |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * Update all FAQ items for a post id. |
|
| 134 | + * |
|
| 135 | + * @param $request $request WP_REST_Request $request {@link WP_REST_Request instance}. |
|
| 136 | + * |
|
| 137 | + * @return array Result array, if post id is not given then error array is returned. |
|
| 138 | + */ |
|
| 139 | + public static function update_faq_items( $request ) { |
|
| 140 | + $data = $request->get_params(); |
|
| 141 | + $post_id = (int) $data['post_id']; |
|
| 142 | + $faq_items = (array) $data['faq_items']; |
|
| 143 | + foreach ( $faq_items as $faq_item ) { |
|
| 144 | + $previous_value = array( |
|
| 145 | + 'question' => (string) $faq_item['previous_question_value'], |
|
| 146 | + 'answer' => (string) $faq_item['previous_answer_value'], |
|
| 147 | + 'id' => (int) $faq_item['id'] |
|
| 148 | + ); |
|
| 149 | + $new_value = array( |
|
| 150 | + 'question' => (string) $faq_item['question'], |
|
| 151 | + 'answer' => (string) $faq_item['answer'], |
|
| 152 | + 'id' => (int) $faq_item['id'] |
|
| 153 | + ); |
|
| 154 | + update_post_meta( $post_id, self::FAQ_META_KEY, $new_value, $previous_value ); |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + return array( |
|
| 158 | + 'status' => 'success', |
|
| 159 | + 'message' => __( 'Faq Items updated successfully' ) |
|
| 160 | + ); |
|
| 161 | + |
|
| 162 | + } |
|
| 163 | + |
|
| 164 | + /** |
|
| 165 | + * Delete Faq items. |
|
| 166 | + * |
|
| 167 | + * @param $request WP_REST_Request $request {@link WP_REST_Request instance}. |
|
| 168 | + * |
|
| 169 | + * @return array Associative array whether the faq item is deleted or not. |
|
| 170 | + */ |
|
| 171 | + public static function delete_faq_items( $request ) { |
|
| 172 | + $post_data = $request->get_params(); |
|
| 173 | + $post_id = $post_data['post_id']; |
|
| 174 | + $faq_items = $post_data['faq_items']; |
|
| 175 | + foreach ( $faq_items as $faq_item ) { |
|
| 176 | + /** |
|
| 177 | + * Note: the order of keys is important in order to delete it properly |
|
| 178 | + * If the order change, delete operation will fail since it is converted |
|
| 179 | + * in to a string when it was stored. |
|
| 180 | + * we cant rely on client to post it in correct order, so we create an array |
|
| 181 | + * in correct order. |
|
| 182 | + **/ |
|
| 183 | + $deleted_faq_item = array( |
|
| 184 | + 'question' => $faq_item['question'], |
|
| 185 | + 'answer' => $faq_item['answer'], |
|
| 186 | + 'id' => (int) $faq_item['id'] |
|
| 187 | + ); |
|
| 188 | + /** |
|
| 189 | + * If the field to be deleted is answer, then dont delete the faq item, |
|
| 190 | + * if it is question then delete the faq item. |
|
| 191 | + */ |
|
| 192 | + if ( $faq_item['field_to_be_deleted'] === self::ANSWER) { |
|
| 193 | + $previous_value = $deleted_faq_item; |
|
| 194 | + // then dont delete the faq item, set the answer as empty. |
|
| 195 | + $deleted_faq_item['answer'] = ''; |
|
| 196 | + $new_value = $deleted_faq_item; |
|
| 197 | + update_post_meta($post_id, self::FAQ_META_KEY, $new_value, $previous_value); |
|
| 198 | + } |
|
| 199 | + else if ( $faq_item['field_to_be_deleted'] === self::QUESTION) { |
|
| 200 | + /** |
|
| 201 | + * If the question is deleted, then delete the faq item. |
|
| 202 | + */ |
|
| 203 | + delete_post_meta( $post_id, self::FAQ_META_KEY, $deleted_faq_item ); |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + } |
|
| 207 | + |
|
| 208 | + /** |
|
| 209 | + * We are returning only the first item id, since the user can select only one text at a time. |
|
| 210 | + */ |
|
| 211 | + return array( |
|
| 212 | + 'status' => 'success', |
|
| 213 | + 'message' => __( 'Faq item successfully deleted.' ), |
|
| 214 | + ); |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + * Insert or update FAQ items. |
|
| 219 | + * |
|
| 220 | + * @param $request WP_REST_Request $request {@link WP_REST_Request instance}. |
|
| 221 | + * |
|
| 222 | + * @return array Associative array whether the faq item is inserted or not |
|
| 223 | + */ |
|
| 224 | + public static function add_faq_items( $request ) { |
|
| 225 | + $post_data = $request->get_params(); |
|
| 226 | + $post_id = $post_data['post_id']; |
|
| 227 | + $faq_items = $post_data['faq_items']; |
|
| 228 | + foreach ( $faq_items as &$faq_item ) { |
|
| 229 | + // Add an identifier id to the faq item, it helps to prevent duplication problem. |
|
| 230 | + /** |
|
| 231 | + * We are using time() and a random integer to prevent |
|
| 232 | + * duplication problem. |
|
| 233 | + */ |
|
| 234 | + $faq_item['id'] = time() + rand( 1, 100 ) + rand( 1, 100 ); |
|
| 235 | + add_post_meta( (int) $post_id, self::FAQ_META_KEY, $faq_item ); |
|
| 236 | + } |
|
| 237 | + |
|
| 238 | + /** |
|
| 239 | + * We are returning only the first item id, since the user can select only one text at a time. |
|
| 240 | + */ |
|
| 241 | + return array( |
|
| 242 | + 'status' => 'success', |
|
| 243 | + 'message' => __( 'Question successfully added.' ), |
|
| 244 | + 'id' => (int) $faq_items[0]['id'], |
|
| 245 | + ); |
|
| 246 | + |
|
| 247 | + } |
|
| 248 | 248 | } |
@@ -26,23 +26,23 @@ discard block |
||
| 26 | 26 | const ANSWER = 'answer'; |
| 27 | 27 | |
| 28 | 28 | public static function register_routes() { |
| 29 | - add_action( 'rest_api_init', 'Wordlift\FAQ\FAQ_Rest_Controller::register_route_callback' ); |
|
| 29 | + add_action('rest_api_init', 'Wordlift\FAQ\FAQ_Rest_Controller::register_route_callback'); |
|
| 30 | 30 | } |
| 31 | 31 | |
| 32 | 32 | public static function register_route_callback() { |
| 33 | 33 | /** |
| 34 | 34 | * Specifies the list of arguments to be present for update request. |
| 35 | 35 | */ |
| 36 | - $post_id_validation_settings = array( |
|
| 36 | + $post_id_validation_settings = array( |
|
| 37 | 37 | 'required' => TRUE, |
| 38 | - 'validate_callback' => function ( $param, $request, $key ) { |
|
| 39 | - return is_numeric( $param ); |
|
| 38 | + 'validate_callback' => function($param, $request, $key) { |
|
| 39 | + return is_numeric($param); |
|
| 40 | 40 | } |
| 41 | 41 | ); |
| 42 | - $faq_items_validation_settings = array( |
|
| 42 | + $faq_items_validation_settings = array( |
|
| 43 | 43 | 'required' => TRUE, |
| 44 | - 'validate_callback' => function ( $param, $request, $key ) { |
|
| 45 | - return is_array( $param ) && count( $param ) > 0; |
|
| 44 | + 'validate_callback' => function($param, $request, $key) { |
|
| 45 | + return is_array($param) && count($param) > 0; |
|
| 46 | 46 | } |
| 47 | 47 | ); |
| 48 | 48 | /** |
@@ -63,8 +63,8 @@ discard block |
||
| 63 | 63 | array( |
| 64 | 64 | 'methods' => \WP_REST_Server::CREATABLE, |
| 65 | 65 | 'callback' => 'Wordlift\Faq\Faq_Rest_Controller::add_faq_items', |
| 66 | - 'permission_callback' => function () { |
|
| 67 | - return current_user_can( 'publish_posts' ); |
|
| 66 | + 'permission_callback' => function() { |
|
| 67 | + return current_user_can('publish_posts'); |
|
| 68 | 68 | }, |
| 69 | 69 | 'args' => $create_or_update_or_delete_args |
| 70 | 70 | ) ); |
@@ -77,8 +77,8 @@ discard block |
||
| 77 | 77 | array( |
| 78 | 78 | 'methods' => \WP_REST_Server::EDITABLE, |
| 79 | 79 | 'callback' => 'Wordlift\Faq\Faq_Rest_Controller::update_faq_items', |
| 80 | - 'permission_callback' => function () { |
|
| 81 | - return current_user_can( 'publish_posts' ); |
|
| 80 | + 'permission_callback' => function() { |
|
| 81 | + return current_user_can('publish_posts'); |
|
| 82 | 82 | }, |
| 83 | 83 | 'args' => $create_or_update_or_delete_args |
| 84 | 84 | ) |
@@ -92,10 +92,10 @@ discard block |
||
| 92 | 92 | array( |
| 93 | 93 | 'methods' => \WP_REST_Server::READABLE, |
| 94 | 94 | 'callback' => 'Wordlift\Faq\Faq_Rest_Controller::get_faq_items', |
| 95 | - 'permission_callback' => function () { |
|
| 96 | - return current_user_can( 'publish_posts' ); |
|
| 95 | + 'permission_callback' => function() { |
|
| 96 | + return current_user_can('publish_posts'); |
|
| 97 | 97 | }, |
| 98 | - 'args' => array( 'post_id' => $post_id_validation_settings ) |
|
| 98 | + 'args' => array('post_id' => $post_id_validation_settings) |
|
| 99 | 99 | ) |
| 100 | 100 | ); |
| 101 | 101 | /** |
@@ -107,8 +107,8 @@ discard block |
||
| 107 | 107 | array( |
| 108 | 108 | 'methods' => \WP_REST_Server::DELETABLE, |
| 109 | 109 | 'callback' => 'Wordlift\Faq\Faq_Rest_Controller::delete_faq_items', |
| 110 | - 'permission_callback' => function () { |
|
| 111 | - return current_user_can( 'publish_posts' ); |
|
| 110 | + 'permission_callback' => function() { |
|
| 111 | + return current_user_can('publish_posts'); |
|
| 112 | 112 | }, |
| 113 | 113 | 'args' => $create_or_update_or_delete_args |
| 114 | 114 | ) |
@@ -122,11 +122,11 @@ discard block |
||
| 122 | 122 | * |
| 123 | 123 | * @return array Result array, if post id is not given then error array is returned. |
| 124 | 124 | */ |
| 125 | - public static function get_faq_items( $request ) { |
|
| 125 | + public static function get_faq_items($request) { |
|
| 126 | 126 | $data = $request->get_params(); |
| 127 | 127 | $post_id = (int) $data['post_id']; |
| 128 | 128 | |
| 129 | - return get_post_meta( $post_id, self::FAQ_META_KEY ); |
|
| 129 | + return get_post_meta($post_id, self::FAQ_META_KEY); |
|
| 130 | 130 | } |
| 131 | 131 | |
| 132 | 132 | /** |
@@ -136,27 +136,27 @@ discard block |
||
| 136 | 136 | * |
| 137 | 137 | * @return array Result array, if post id is not given then error array is returned. |
| 138 | 138 | */ |
| 139 | - public static function update_faq_items( $request ) { |
|
| 139 | + public static function update_faq_items($request) { |
|
| 140 | 140 | $data = $request->get_params(); |
| 141 | 141 | $post_id = (int) $data['post_id']; |
| 142 | 142 | $faq_items = (array) $data['faq_items']; |
| 143 | - foreach ( $faq_items as $faq_item ) { |
|
| 143 | + foreach ($faq_items as $faq_item) { |
|
| 144 | 144 | $previous_value = array( |
| 145 | 145 | 'question' => (string) $faq_item['previous_question_value'], |
| 146 | 146 | 'answer' => (string) $faq_item['previous_answer_value'], |
| 147 | 147 | 'id' => (int) $faq_item['id'] |
| 148 | 148 | ); |
| 149 | - $new_value = array( |
|
| 149 | + $new_value = array( |
|
| 150 | 150 | 'question' => (string) $faq_item['question'], |
| 151 | 151 | 'answer' => (string) $faq_item['answer'], |
| 152 | 152 | 'id' => (int) $faq_item['id'] |
| 153 | 153 | ); |
| 154 | - update_post_meta( $post_id, self::FAQ_META_KEY, $new_value, $previous_value ); |
|
| 154 | + update_post_meta($post_id, self::FAQ_META_KEY, $new_value, $previous_value); |
|
| 155 | 155 | } |
| 156 | 156 | |
| 157 | 157 | return array( |
| 158 | 158 | 'status' => 'success', |
| 159 | - 'message' => __( 'Faq Items updated successfully' ) |
|
| 159 | + 'message' => __('Faq Items updated successfully') |
|
| 160 | 160 | ); |
| 161 | 161 | |
| 162 | 162 | } |
@@ -168,11 +168,11 @@ discard block |
||
| 168 | 168 | * |
| 169 | 169 | * @return array Associative array whether the faq item is deleted or not. |
| 170 | 170 | */ |
| 171 | - public static function delete_faq_items( $request ) { |
|
| 171 | + public static function delete_faq_items($request) { |
|
| 172 | 172 | $post_data = $request->get_params(); |
| 173 | 173 | $post_id = $post_data['post_id']; |
| 174 | 174 | $faq_items = $post_data['faq_items']; |
| 175 | - foreach ( $faq_items as $faq_item ) { |
|
| 175 | + foreach ($faq_items as $faq_item) { |
|
| 176 | 176 | /** |
| 177 | 177 | * Note: the order of keys is important in order to delete it properly |
| 178 | 178 | * If the order change, delete operation will fail since it is converted |
@@ -189,18 +189,18 @@ discard block |
||
| 189 | 189 | * If the field to be deleted is answer, then dont delete the faq item, |
| 190 | 190 | * if it is question then delete the faq item. |
| 191 | 191 | */ |
| 192 | - if ( $faq_item['field_to_be_deleted'] === self::ANSWER) { |
|
| 192 | + if ($faq_item['field_to_be_deleted'] === self::ANSWER) { |
|
| 193 | 193 | $previous_value = $deleted_faq_item; |
| 194 | 194 | // then dont delete the faq item, set the answer as empty. |
| 195 | 195 | $deleted_faq_item['answer'] = ''; |
| 196 | 196 | $new_value = $deleted_faq_item; |
| 197 | 197 | update_post_meta($post_id, self::FAQ_META_KEY, $new_value, $previous_value); |
| 198 | 198 | } |
| 199 | - else if ( $faq_item['field_to_be_deleted'] === self::QUESTION) { |
|
| 199 | + else if ($faq_item['field_to_be_deleted'] === self::QUESTION) { |
|
| 200 | 200 | /** |
| 201 | 201 | * If the question is deleted, then delete the faq item. |
| 202 | 202 | */ |
| 203 | - delete_post_meta( $post_id, self::FAQ_META_KEY, $deleted_faq_item ); |
|
| 203 | + delete_post_meta($post_id, self::FAQ_META_KEY, $deleted_faq_item); |
|
| 204 | 204 | } |
| 205 | 205 | |
| 206 | 206 | } |
@@ -210,7 +210,7 @@ discard block |
||
| 210 | 210 | */ |
| 211 | 211 | return array( |
| 212 | 212 | 'status' => 'success', |
| 213 | - 'message' => __( 'Faq item successfully deleted.' ), |
|
| 213 | + 'message' => __('Faq item successfully deleted.'), |
|
| 214 | 214 | ); |
| 215 | 215 | } |
| 216 | 216 | |
@@ -221,18 +221,18 @@ discard block |
||
| 221 | 221 | * |
| 222 | 222 | * @return array Associative array whether the faq item is inserted or not |
| 223 | 223 | */ |
| 224 | - public static function add_faq_items( $request ) { |
|
| 224 | + public static function add_faq_items($request) { |
|
| 225 | 225 | $post_data = $request->get_params(); |
| 226 | 226 | $post_id = $post_data['post_id']; |
| 227 | 227 | $faq_items = $post_data['faq_items']; |
| 228 | - foreach ( $faq_items as &$faq_item ) { |
|
| 228 | + foreach ($faq_items as &$faq_item) { |
|
| 229 | 229 | // Add an identifier id to the faq item, it helps to prevent duplication problem. |
| 230 | 230 | /** |
| 231 | 231 | * We are using time() and a random integer to prevent |
| 232 | 232 | * duplication problem. |
| 233 | 233 | */ |
| 234 | - $faq_item['id'] = time() + rand( 1, 100 ) + rand( 1, 100 ); |
|
| 235 | - add_post_meta( (int) $post_id, self::FAQ_META_KEY, $faq_item ); |
|
| 234 | + $faq_item['id'] = time() + rand(1, 100) + rand(1, 100); |
|
| 235 | + add_post_meta((int) $post_id, self::FAQ_META_KEY, $faq_item); |
|
| 236 | 236 | } |
| 237 | 237 | |
| 238 | 238 | /** |
@@ -240,7 +240,7 @@ discard block |
||
| 240 | 240 | */ |
| 241 | 241 | return array( |
| 242 | 242 | 'status' => 'success', |
| 243 | - 'message' => __( 'Question successfully added.' ), |
|
| 243 | + 'message' => __('Question successfully added.'), |
|
| 244 | 244 | 'id' => (int) $faq_items[0]['id'], |
| 245 | 245 | ); |
| 246 | 246 | |
@@ -195,8 +195,7 @@ |
||
| 195 | 195 | $deleted_faq_item['answer'] = ''; |
| 196 | 196 | $new_value = $deleted_faq_item; |
| 197 | 197 | update_post_meta($post_id, self::FAQ_META_KEY, $new_value, $previous_value); |
| 198 | - } |
|
| 199 | - else if ( $faq_item['field_to_be_deleted'] === self::QUESTION) { |
|
| 198 | + } else if ( $faq_item['field_to_be_deleted'] === self::QUESTION) { |
|
| 200 | 199 | /** |
| 201 | 200 | * If the question is deleted, then delete the faq item. |
| 202 | 201 | */ |
@@ -20,128 +20,128 @@ discard block |
||
| 20 | 20 | */ |
| 21 | 21 | class Wordlift_Admin_Post_Edit_Page { |
| 22 | 22 | |
| 23 | - /** |
|
| 24 | - * Constants to be used instead of text inside FAQ |
|
| 25 | - * helper methods. |
|
| 26 | - */ |
|
| 27 | - const GUTENBERG = 'gutenberg'; |
|
| 28 | - const TINY_MCE = 'tiny_mce'; |
|
| 29 | - const FAQ_LIST_BOX_ID = 'wl-faq-meta-list-box'; |
|
| 30 | - |
|
| 31 | - /** Constant to be used for translation domain */ |
|
| 32 | - const WORDLIFT_TEXT_DOMAIN = 'wordlift'; |
|
| 33 | - |
|
| 34 | - /** |
|
| 35 | - * The {@link Wordlift} plugin instance. |
|
| 36 | - * |
|
| 37 | - * @since 3.11.0 |
|
| 38 | - * |
|
| 39 | - * @var \Wordlift $plugin The {@link Wordlift} plugin instance. |
|
| 40 | - */ |
|
| 41 | - private $plugin; |
|
| 42 | - |
|
| 43 | - /** |
|
| 44 | - * A {@link Wordlift_Log_Service} instance. |
|
| 45 | - * |
|
| 46 | - * @since 3.15.4 |
|
| 47 | - * |
|
| 48 | - * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 49 | - */ |
|
| 50 | - private $log; |
|
| 51 | - |
|
| 52 | - /** |
|
| 53 | - * Create the {@link Wordlift_Admin_Post_Edit_Page} instance. |
|
| 54 | - * |
|
| 55 | - * @param \Wordlift $plugin The {@link Wordlift} plugin instance. |
|
| 56 | - * |
|
| 57 | - * @since 3.11.0 |
|
| 58 | - * |
|
| 59 | - */ |
|
| 60 | - function __construct( $plugin ) { |
|
| 61 | - |
|
| 62 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 63 | - |
|
| 64 | - add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_scripts_gutenberg', ) ); |
|
| 65 | - |
|
| 66 | - // Bail out if we're in the UX Builder editor. |
|
| 67 | - if ( $this->is_ux_builder_editor() ) { |
|
| 68 | - $this->log->info( 'WordLift will not show, since we are in UX Builder editor.' ); |
|
| 69 | - |
|
| 70 | - return; |
|
| 71 | - } |
|
| 72 | - |
|
| 73 | - // Define the callbacks. |
|
| 74 | - $callback = array( $this, 'enqueue_scripts', ); |
|
| 75 | - |
|
| 76 | - // Set a hook to enqueue scripts only when the edit page is displayed. |
|
| 77 | - add_action( 'admin_print_scripts-post.php', $callback ); |
|
| 78 | - add_action( 'admin_print_scripts-post-new.php', $callback ); |
|
| 79 | - |
|
| 80 | - $this->plugin = $plugin; |
|
| 81 | - } |
|
| 82 | - |
|
| 83 | - /** |
|
| 84 | - * Check whether the current post opens with G'berg or not. |
|
| 85 | - * |
|
| 86 | - * @return bool True if G'berg is used otherwise false. |
|
| 87 | - * @since 3.22.3 |
|
| 88 | - */ |
|
| 89 | - function is_gutenberg_page() { |
|
| 90 | - if ( function_exists( 'is_gutenberg_page' ) && is_gutenberg_page() ) { |
|
| 91 | - // The Gutenberg plugin is on. |
|
| 92 | - return TRUE; |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - $current_screen = get_current_screen(); |
|
| 96 | - if ( method_exists( $current_screen, 'is_block_editor' ) && $current_screen->is_block_editor() ) { |
|
| 97 | - // Gutenberg page on 5+. |
|
| 98 | - return TRUE; |
|
| 99 | - } |
|
| 100 | - |
|
| 101 | - return FALSE; |
|
| 102 | - } |
|
| 103 | - |
|
| 104 | - /** |
|
| 105 | - * Check if we're in UX builder. |
|
| 106 | - * |
|
| 107 | - * @see https://github.com/insideout10/wordlift-plugin/issues/691 |
|
| 108 | - * |
|
| 109 | - * @since 3.15.4 |
|
| 110 | - * |
|
| 111 | - * @return bool True if we're in UX builder, otherwise false. |
|
| 112 | - */ |
|
| 113 | - private function is_ux_builder_editor() { |
|
| 114 | - |
|
| 115 | - return function_exists( 'ux_builder_is_editor' ) |
|
| 116 | - && ux_builder_is_editor(); |
|
| 117 | - } |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * Enqueue scripts and styles for the edit page. |
|
| 121 | - * |
|
| 122 | - * @since 3.11.0 |
|
| 123 | - */ |
|
| 124 | - public function enqueue_scripts() { |
|
| 125 | - |
|
| 126 | - // Bail out if this is G'berg. |
|
| 127 | - if ( $this->is_gutenberg_page() ) { |
|
| 128 | - return; |
|
| 129 | - } |
|
| 130 | - |
|
| 131 | - // Dequeue potentially conflicting ontrapages angular scripts which any *are not* used on the edit screen. |
|
| 132 | - // |
|
| 133 | - // @see https://github.com/insideout10/wordlift-plugin/issues/832 |
|
| 134 | - wp_dequeue_script( 'ontrapagesAngular' ); |
|
| 135 | - wp_dequeue_script( 'ontrapagesApp' ); |
|
| 136 | - wp_dequeue_script( 'ontrapagesController' ); |
|
| 137 | - |
|
| 138 | - // If Gutenberg is enabled for the post, do not load the legacy edit.js. |
|
| 139 | - if ( function_exists( 'use_block_editor_for_post' ) && use_block_editor_for_post( get_post() ) ) { |
|
| 140 | - return; |
|
| 141 | - } |
|
| 142 | - |
|
| 143 | - |
|
| 144 | - /* |
|
| 23 | + /** |
|
| 24 | + * Constants to be used instead of text inside FAQ |
|
| 25 | + * helper methods. |
|
| 26 | + */ |
|
| 27 | + const GUTENBERG = 'gutenberg'; |
|
| 28 | + const TINY_MCE = 'tiny_mce'; |
|
| 29 | + const FAQ_LIST_BOX_ID = 'wl-faq-meta-list-box'; |
|
| 30 | + |
|
| 31 | + /** Constant to be used for translation domain */ |
|
| 32 | + const WORDLIFT_TEXT_DOMAIN = 'wordlift'; |
|
| 33 | + |
|
| 34 | + /** |
|
| 35 | + * The {@link Wordlift} plugin instance. |
|
| 36 | + * |
|
| 37 | + * @since 3.11.0 |
|
| 38 | + * |
|
| 39 | + * @var \Wordlift $plugin The {@link Wordlift} plugin instance. |
|
| 40 | + */ |
|
| 41 | + private $plugin; |
|
| 42 | + |
|
| 43 | + /** |
|
| 44 | + * A {@link Wordlift_Log_Service} instance. |
|
| 45 | + * |
|
| 46 | + * @since 3.15.4 |
|
| 47 | + * |
|
| 48 | + * @var \Wordlift_Log_Service $log A {@link Wordlift_Log_Service} instance. |
|
| 49 | + */ |
|
| 50 | + private $log; |
|
| 51 | + |
|
| 52 | + /** |
|
| 53 | + * Create the {@link Wordlift_Admin_Post_Edit_Page} instance. |
|
| 54 | + * |
|
| 55 | + * @param \Wordlift $plugin The {@link Wordlift} plugin instance. |
|
| 56 | + * |
|
| 57 | + * @since 3.11.0 |
|
| 58 | + * |
|
| 59 | + */ |
|
| 60 | + function __construct( $plugin ) { |
|
| 61 | + |
|
| 62 | + $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 63 | + |
|
| 64 | + add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_scripts_gutenberg', ) ); |
|
| 65 | + |
|
| 66 | + // Bail out if we're in the UX Builder editor. |
|
| 67 | + if ( $this->is_ux_builder_editor() ) { |
|
| 68 | + $this->log->info( 'WordLift will not show, since we are in UX Builder editor.' ); |
|
| 69 | + |
|
| 70 | + return; |
|
| 71 | + } |
|
| 72 | + |
|
| 73 | + // Define the callbacks. |
|
| 74 | + $callback = array( $this, 'enqueue_scripts', ); |
|
| 75 | + |
|
| 76 | + // Set a hook to enqueue scripts only when the edit page is displayed. |
|
| 77 | + add_action( 'admin_print_scripts-post.php', $callback ); |
|
| 78 | + add_action( 'admin_print_scripts-post-new.php', $callback ); |
|
| 79 | + |
|
| 80 | + $this->plugin = $plugin; |
|
| 81 | + } |
|
| 82 | + |
|
| 83 | + /** |
|
| 84 | + * Check whether the current post opens with G'berg or not. |
|
| 85 | + * |
|
| 86 | + * @return bool True if G'berg is used otherwise false. |
|
| 87 | + * @since 3.22.3 |
|
| 88 | + */ |
|
| 89 | + function is_gutenberg_page() { |
|
| 90 | + if ( function_exists( 'is_gutenberg_page' ) && is_gutenberg_page() ) { |
|
| 91 | + // The Gutenberg plugin is on. |
|
| 92 | + return TRUE; |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + $current_screen = get_current_screen(); |
|
| 96 | + if ( method_exists( $current_screen, 'is_block_editor' ) && $current_screen->is_block_editor() ) { |
|
| 97 | + // Gutenberg page on 5+. |
|
| 98 | + return TRUE; |
|
| 99 | + } |
|
| 100 | + |
|
| 101 | + return FALSE; |
|
| 102 | + } |
|
| 103 | + |
|
| 104 | + /** |
|
| 105 | + * Check if we're in UX builder. |
|
| 106 | + * |
|
| 107 | + * @see https://github.com/insideout10/wordlift-plugin/issues/691 |
|
| 108 | + * |
|
| 109 | + * @since 3.15.4 |
|
| 110 | + * |
|
| 111 | + * @return bool True if we're in UX builder, otherwise false. |
|
| 112 | + */ |
|
| 113 | + private function is_ux_builder_editor() { |
|
| 114 | + |
|
| 115 | + return function_exists( 'ux_builder_is_editor' ) |
|
| 116 | + && ux_builder_is_editor(); |
|
| 117 | + } |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * Enqueue scripts and styles for the edit page. |
|
| 121 | + * |
|
| 122 | + * @since 3.11.0 |
|
| 123 | + */ |
|
| 124 | + public function enqueue_scripts() { |
|
| 125 | + |
|
| 126 | + // Bail out if this is G'berg. |
|
| 127 | + if ( $this->is_gutenberg_page() ) { |
|
| 128 | + return; |
|
| 129 | + } |
|
| 130 | + |
|
| 131 | + // Dequeue potentially conflicting ontrapages angular scripts which any *are not* used on the edit screen. |
|
| 132 | + // |
|
| 133 | + // @see https://github.com/insideout10/wordlift-plugin/issues/832 |
|
| 134 | + wp_dequeue_script( 'ontrapagesAngular' ); |
|
| 135 | + wp_dequeue_script( 'ontrapagesApp' ); |
|
| 136 | + wp_dequeue_script( 'ontrapagesController' ); |
|
| 137 | + |
|
| 138 | + // If Gutenberg is enabled for the post, do not load the legacy edit.js. |
|
| 139 | + if ( function_exists( 'use_block_editor_for_post' ) && use_block_editor_for_post( get_post() ) ) { |
|
| 140 | + return; |
|
| 141 | + } |
|
| 142 | + |
|
| 143 | + |
|
| 144 | + /* |
|
| 145 | 145 | * Enqueue the edit screen JavaScript. The `wordlift-admin.bundle.js` file |
| 146 | 146 | * is scheduled to replace the older `wordlift-admin.min.js` once client-side |
| 147 | 147 | * code is properly refactored. |
@@ -150,169 +150,169 @@ discard block |
||
| 150 | 150 | * |
| 151 | 151 | * @since 3.20.0 edit.js has been migrated to the new webpack configuration. |
| 152 | 152 | */ |
| 153 | - $script_name = plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/edit'; |
|
| 154 | - |
|
| 155 | - /** |
|
| 156 | - * Scripts_Helper introduced. |
|
| 157 | - * |
|
| 158 | - * @since 3.25.0 Scripts are loaded using script helper to ensure WP 4.4 compatibiility. |
|
| 159 | - * @since 3.25.1 The handle is used to hook the wp_localize_script for the _wlEntityTypes global object. |
|
| 160 | - */ |
|
| 161 | - Scripts_Helper::enqueue_based_on_wordpress_version( |
|
| 162 | - 'wl-classic-editor', |
|
| 163 | - $script_name, |
|
| 164 | - array( |
|
| 165 | - $this->plugin->get_plugin_name(), |
|
| 166 | - 'jquery', |
|
| 167 | - // Require wp.ajax. |
|
| 168 | - 'wp-util', |
|
| 169 | - // @@todo: provide the following dependencies when we're in WP < 5.0 (i.e. when these dependencies aren't already defined). |
|
| 170 | - 'react', |
|
| 171 | - 'react-dom', |
|
| 172 | - 'wp-element', |
|
| 173 | - 'wp-polyfill', |
|
| 174 | - /* |
|
| 153 | + $script_name = plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/edit'; |
|
| 154 | + |
|
| 155 | + /** |
|
| 156 | + * Scripts_Helper introduced. |
|
| 157 | + * |
|
| 158 | + * @since 3.25.0 Scripts are loaded using script helper to ensure WP 4.4 compatibiility. |
|
| 159 | + * @since 3.25.1 The handle is used to hook the wp_localize_script for the _wlEntityTypes global object. |
|
| 160 | + */ |
|
| 161 | + Scripts_Helper::enqueue_based_on_wordpress_version( |
|
| 162 | + 'wl-classic-editor', |
|
| 163 | + $script_name, |
|
| 164 | + array( |
|
| 165 | + $this->plugin->get_plugin_name(), |
|
| 166 | + 'jquery', |
|
| 167 | + // Require wp.ajax. |
|
| 168 | + 'wp-util', |
|
| 169 | + // @@todo: provide the following dependencies when we're in WP < 5.0 (i.e. when these dependencies aren't already defined). |
|
| 170 | + 'react', |
|
| 171 | + 'react-dom', |
|
| 172 | + 'wp-element', |
|
| 173 | + 'wp-polyfill', |
|
| 174 | + /* |
|
| 175 | 175 | * Angular isn't loaded anymore remotely, but it is loaded within wordlift-reloaded.js. |
| 176 | 176 | * |
| 177 | 177 | * See https://github.com/insideout10/wordlift-plugin/issues/865. |
| 178 | 178 | * |
| 179 | 179 | * @since 3.19.6 |
| 180 | 180 | */ |
| 181 | - // // Require Angular. |
|
| 182 | - // 'wl-angular', |
|
| 183 | - // 'wl-angular-geolocation', |
|
| 184 | - // 'wl-angular-touch', |
|
| 185 | - // 'wl-angular-animate', |
|
| 186 | - /** |
|
| 187 | - * We need the `wp.hooks` global to allow the edit.js script to send actions. |
|
| 188 | - * |
|
| 189 | - * @since 3.23.0 |
|
| 190 | - */ |
|
| 191 | - 'wp-hooks', |
|
| 192 | - ) |
|
| 193 | - ); |
|
| 194 | - |
|
| 195 | - wp_enqueue_style( 'wl-classic-editor', "$script_name.css", array(), $this->plugin->get_version() ); |
|
| 196 | - $this->load_faq_scripts_and_styles(); |
|
| 197 | - $this->load_faq_settings( self::TINY_MCE ); |
|
| 198 | - } |
|
| 199 | - |
|
| 200 | - /** |
|
| 201 | - * Enqueue the scripts and styles needed for FAQ |
|
| 202 | - */ |
|
| 203 | - private function load_faq_scripts_and_styles() { |
|
| 204 | - wp_enqueue_style( |
|
| 205 | - 'wl-faq-metabox-style', |
|
| 206 | - plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/faq.css', |
|
| 207 | - array() |
|
| 208 | - ); |
|
| 209 | - Scripts_Helper::enqueue_based_on_wordpress_version( |
|
| 210 | - 'wl-faq-metabox-script', |
|
| 211 | - plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/faq', |
|
| 212 | - array( 'wp-polyfill' ), |
|
| 213 | - true |
|
| 214 | - ); |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - /** |
|
| 218 | - * Get FAQ settings array |
|
| 219 | - * |
|
| 220 | - * @return array |
|
| 221 | - */ |
|
| 222 | - public function get_faq_settings() { |
|
| 223 | - return array( |
|
| 224 | - 'restUrl' => get_rest_url( NULL, WL_REST_ROUTE_DEFAULT_NAMESPACE . '/faq' ), |
|
| 225 | - 'listBoxId' => self::FAQ_LIST_BOX_ID, |
|
| 226 | - 'nonce' => wp_create_nonce( 'wp_rest' ), |
|
| 227 | - 'postId' => get_the_ID(), |
|
| 228 | - // Translation for warning, error message. |
|
| 229 | - 'invalidTagMessage' => sprintf( __( 'Invalid tags %s is present in answer', self::WORDLIFT_TEXT_DOMAIN ), "{INVALID_TAGS}" ), |
|
| 230 | - 'invalidWordCountMessage' => sprintf( __( 'Answer word count must not exceed %s words', self::WORDLIFT_TEXT_DOMAIN ), "{ANSWER_WORD_COUNT_WARNING_LIMIT}" ), |
|
| 231 | - 'questionText' => __( 'Question', self::WORDLIFT_TEXT_DOMAIN ), |
|
| 232 | - 'answerText' => __( 'Answer', self::WORDLIFT_TEXT_DOMAIN ), |
|
| 233 | - 'addQuestionOrAnswerText' => __( 'Add Question / Answer', self::WORDLIFT_TEXT_DOMAIN ), |
|
| 234 | - 'addQuestionText' => __( 'Add Question', self::WORDLIFT_TEXT_DOMAIN ), |
|
| 235 | - 'addAnswerText' => __( 'Add Answer', self::WORDLIFT_TEXT_DOMAIN ), |
|
| 236 | - 'noFaqItemsText' => __( 'No Questions or answers present', self::WORDLIFT_TEXT_DOMAIN ), |
|
| 237 | - 'updatingText' => __( 'Updating...', self::WORDLIFT_TEXT_DOMAIN ) |
|
| 238 | - ); |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - /** |
|
| 242 | - * Load FAQ settings to the add/edit post page |
|
| 243 | - * |
|
| 244 | - * @param $editor string specifying which text editor needed to be used. |
|
| 245 | - */ |
|
| 246 | - private function load_faq_settings( $editor ) { |
|
| 247 | - // This script also provides translations to gutenberg. |
|
| 248 | - wp_localize_script( 'wl-faq-metabox-script', '_wlFaqSettings', $this->get_faq_settings() ); |
|
| 249 | - |
|
| 250 | - // Enqueue the FAQ style |
|
| 251 | - if ( $editor === self::GUTENBERG ) { |
|
| 252 | - Scripts_Helper::enqueue_based_on_wordpress_version( |
|
| 253 | - 'wl-faq-gutenberg-plugin', |
|
| 254 | - plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/block-editor-faq-plugin', |
|
| 255 | - array( 'wp-polyfill' ), |
|
| 256 | - TRUE |
|
| 257 | - ); |
|
| 258 | - } |
|
| 259 | - } |
|
| 260 | - |
|
| 261 | - /** |
|
| 262 | - * Enqueue scripts and styles for the gutenberg edit page. |
|
| 263 | - * |
|
| 264 | - * @since 3.21.0 |
|
| 265 | - */ |
|
| 266 | - public function enqueue_scripts_gutenberg() { |
|
| 267 | - // Load FAQ settings. |
|
| 268 | - $this->load_faq_scripts_and_styles(); |
|
| 269 | - $this->load_faq_settings( self::GUTENBERG ); |
|
| 270 | - |
|
| 271 | - wp_register_script( |
|
| 272 | - 'wl-block-editor', |
|
| 273 | - plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/block-editor.js', |
|
| 274 | - array( |
|
| 275 | - 'react', |
|
| 276 | - 'wordlift', |
|
| 277 | - 'wp-hooks', |
|
| 278 | - 'wp-data', |
|
| 279 | - 'wp-rich-text', |
|
| 280 | - 'wp-blocks', |
|
| 281 | - 'wp-plugins', |
|
| 282 | - 'wp-edit-post', |
|
| 283 | - ), |
|
| 284 | - $this->plugin->get_version() |
|
| 285 | - ); |
|
| 286 | - wp_localize_script( 'wl-block-editor', '_wlBlockEditorSettings', array( |
|
| 287 | - 'root' => esc_url_raw( rest_url() ), |
|
| 288 | - 'nonce' => wp_create_nonce( 'wp_rest' ) |
|
| 289 | - ) ); |
|
| 290 | - |
|
| 291 | - /* |
|
| 181 | + // // Require Angular. |
|
| 182 | + // 'wl-angular', |
|
| 183 | + // 'wl-angular-geolocation', |
|
| 184 | + // 'wl-angular-touch', |
|
| 185 | + // 'wl-angular-animate', |
|
| 186 | + /** |
|
| 187 | + * We need the `wp.hooks` global to allow the edit.js script to send actions. |
|
| 188 | + * |
|
| 189 | + * @since 3.23.0 |
|
| 190 | + */ |
|
| 191 | + 'wp-hooks', |
|
| 192 | + ) |
|
| 193 | + ); |
|
| 194 | + |
|
| 195 | + wp_enqueue_style( 'wl-classic-editor', "$script_name.css", array(), $this->plugin->get_version() ); |
|
| 196 | + $this->load_faq_scripts_and_styles(); |
|
| 197 | + $this->load_faq_settings( self::TINY_MCE ); |
|
| 198 | + } |
|
| 199 | + |
|
| 200 | + /** |
|
| 201 | + * Enqueue the scripts and styles needed for FAQ |
|
| 202 | + */ |
|
| 203 | + private function load_faq_scripts_and_styles() { |
|
| 204 | + wp_enqueue_style( |
|
| 205 | + 'wl-faq-metabox-style', |
|
| 206 | + plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/faq.css', |
|
| 207 | + array() |
|
| 208 | + ); |
|
| 209 | + Scripts_Helper::enqueue_based_on_wordpress_version( |
|
| 210 | + 'wl-faq-metabox-script', |
|
| 211 | + plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/faq', |
|
| 212 | + array( 'wp-polyfill' ), |
|
| 213 | + true |
|
| 214 | + ); |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + /** |
|
| 218 | + * Get FAQ settings array |
|
| 219 | + * |
|
| 220 | + * @return array |
|
| 221 | + */ |
|
| 222 | + public function get_faq_settings() { |
|
| 223 | + return array( |
|
| 224 | + 'restUrl' => get_rest_url( NULL, WL_REST_ROUTE_DEFAULT_NAMESPACE . '/faq' ), |
|
| 225 | + 'listBoxId' => self::FAQ_LIST_BOX_ID, |
|
| 226 | + 'nonce' => wp_create_nonce( 'wp_rest' ), |
|
| 227 | + 'postId' => get_the_ID(), |
|
| 228 | + // Translation for warning, error message. |
|
| 229 | + 'invalidTagMessage' => sprintf( __( 'Invalid tags %s is present in answer', self::WORDLIFT_TEXT_DOMAIN ), "{INVALID_TAGS}" ), |
|
| 230 | + 'invalidWordCountMessage' => sprintf( __( 'Answer word count must not exceed %s words', self::WORDLIFT_TEXT_DOMAIN ), "{ANSWER_WORD_COUNT_WARNING_LIMIT}" ), |
|
| 231 | + 'questionText' => __( 'Question', self::WORDLIFT_TEXT_DOMAIN ), |
|
| 232 | + 'answerText' => __( 'Answer', self::WORDLIFT_TEXT_DOMAIN ), |
|
| 233 | + 'addQuestionOrAnswerText' => __( 'Add Question / Answer', self::WORDLIFT_TEXT_DOMAIN ), |
|
| 234 | + 'addQuestionText' => __( 'Add Question', self::WORDLIFT_TEXT_DOMAIN ), |
|
| 235 | + 'addAnswerText' => __( 'Add Answer', self::WORDLIFT_TEXT_DOMAIN ), |
|
| 236 | + 'noFaqItemsText' => __( 'No Questions or answers present', self::WORDLIFT_TEXT_DOMAIN ), |
|
| 237 | + 'updatingText' => __( 'Updating...', self::WORDLIFT_TEXT_DOMAIN ) |
|
| 238 | + ); |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + /** |
|
| 242 | + * Load FAQ settings to the add/edit post page |
|
| 243 | + * |
|
| 244 | + * @param $editor string specifying which text editor needed to be used. |
|
| 245 | + */ |
|
| 246 | + private function load_faq_settings( $editor ) { |
|
| 247 | + // This script also provides translations to gutenberg. |
|
| 248 | + wp_localize_script( 'wl-faq-metabox-script', '_wlFaqSettings', $this->get_faq_settings() ); |
|
| 249 | + |
|
| 250 | + // Enqueue the FAQ style |
|
| 251 | + if ( $editor === self::GUTENBERG ) { |
|
| 252 | + Scripts_Helper::enqueue_based_on_wordpress_version( |
|
| 253 | + 'wl-faq-gutenberg-plugin', |
|
| 254 | + plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/block-editor-faq-plugin', |
|
| 255 | + array( 'wp-polyfill' ), |
|
| 256 | + TRUE |
|
| 257 | + ); |
|
| 258 | + } |
|
| 259 | + } |
|
| 260 | + |
|
| 261 | + /** |
|
| 262 | + * Enqueue scripts and styles for the gutenberg edit page. |
|
| 263 | + * |
|
| 264 | + * @since 3.21.0 |
|
| 265 | + */ |
|
| 266 | + public function enqueue_scripts_gutenberg() { |
|
| 267 | + // Load FAQ settings. |
|
| 268 | + $this->load_faq_scripts_and_styles(); |
|
| 269 | + $this->load_faq_settings( self::GUTENBERG ); |
|
| 270 | + |
|
| 271 | + wp_register_script( |
|
| 272 | + 'wl-block-editor', |
|
| 273 | + plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/block-editor.js', |
|
| 274 | + array( |
|
| 275 | + 'react', |
|
| 276 | + 'wordlift', |
|
| 277 | + 'wp-hooks', |
|
| 278 | + 'wp-data', |
|
| 279 | + 'wp-rich-text', |
|
| 280 | + 'wp-blocks', |
|
| 281 | + 'wp-plugins', |
|
| 282 | + 'wp-edit-post', |
|
| 283 | + ), |
|
| 284 | + $this->plugin->get_version() |
|
| 285 | + ); |
|
| 286 | + wp_localize_script( 'wl-block-editor', '_wlBlockEditorSettings', array( |
|
| 287 | + 'root' => esc_url_raw( rest_url() ), |
|
| 288 | + 'nonce' => wp_create_nonce( 'wp_rest' ) |
|
| 289 | + ) ); |
|
| 290 | + |
|
| 291 | + /* |
|
| 292 | 292 | * @since 3.25.1 The hook is used by the wp_localize_script to register the _wlEntityTypes global object. |
| 293 | 293 | */ |
| 294 | - wp_enqueue_style( |
|
| 295 | - 'wl-block-editor', |
|
| 296 | - plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/block-editor.css', |
|
| 297 | - array(), |
|
| 298 | - $this->plugin->get_version() |
|
| 299 | - ); |
|
| 300 | - |
|
| 301 | - wp_enqueue_script( |
|
| 302 | - 'wl-autocomplete-select', |
|
| 303 | - plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/autocomplete-select.js', |
|
| 304 | - array(), |
|
| 305 | - $this->plugin->get_version(), |
|
| 306 | - TRUE |
|
| 307 | - ); |
|
| 308 | - |
|
| 309 | - wp_enqueue_style( |
|
| 310 | - 'wl-autocomplete-select', |
|
| 311 | - plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/autocomplete-select.css', |
|
| 312 | - array(), |
|
| 313 | - $this->plugin->get_version() |
|
| 314 | - ); |
|
| 315 | - |
|
| 316 | - } |
|
| 294 | + wp_enqueue_style( |
|
| 295 | + 'wl-block-editor', |
|
| 296 | + plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/block-editor.css', |
|
| 297 | + array(), |
|
| 298 | + $this->plugin->get_version() |
|
| 299 | + ); |
|
| 300 | + |
|
| 301 | + wp_enqueue_script( |
|
| 302 | + 'wl-autocomplete-select', |
|
| 303 | + plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/autocomplete-select.js', |
|
| 304 | + array(), |
|
| 305 | + $this->plugin->get_version(), |
|
| 306 | + TRUE |
|
| 307 | + ); |
|
| 308 | + |
|
| 309 | + wp_enqueue_style( |
|
| 310 | + 'wl-autocomplete-select', |
|
| 311 | + plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/autocomplete-select.css', |
|
| 312 | + array(), |
|
| 313 | + $this->plugin->get_version() |
|
| 314 | + ); |
|
| 315 | + |
|
| 316 | + } |
|
| 317 | 317 | |
| 318 | 318 | } |
@@ -57,25 +57,25 @@ discard block |
||
| 57 | 57 | * @since 3.11.0 |
| 58 | 58 | * |
| 59 | 59 | */ |
| 60 | - function __construct( $plugin ) { |
|
| 60 | + function __construct($plugin) { |
|
| 61 | 61 | |
| 62 | - $this->log = Wordlift_Log_Service::get_logger( get_class() ); |
|
| 62 | + $this->log = Wordlift_Log_Service::get_logger(get_class()); |
|
| 63 | 63 | |
| 64 | - add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_scripts_gutenberg', ) ); |
|
| 64 | + add_action('enqueue_block_editor_assets', array($this, 'enqueue_scripts_gutenberg',)); |
|
| 65 | 65 | |
| 66 | 66 | // Bail out if we're in the UX Builder editor. |
| 67 | - if ( $this->is_ux_builder_editor() ) { |
|
| 68 | - $this->log->info( 'WordLift will not show, since we are in UX Builder editor.' ); |
|
| 67 | + if ($this->is_ux_builder_editor()) { |
|
| 68 | + $this->log->info('WordLift will not show, since we are in UX Builder editor.'); |
|
| 69 | 69 | |
| 70 | 70 | return; |
| 71 | 71 | } |
| 72 | 72 | |
| 73 | 73 | // Define the callbacks. |
| 74 | - $callback = array( $this, 'enqueue_scripts', ); |
|
| 74 | + $callback = array($this, 'enqueue_scripts',); |
|
| 75 | 75 | |
| 76 | 76 | // Set a hook to enqueue scripts only when the edit page is displayed. |
| 77 | - add_action( 'admin_print_scripts-post.php', $callback ); |
|
| 78 | - add_action( 'admin_print_scripts-post-new.php', $callback ); |
|
| 77 | + add_action('admin_print_scripts-post.php', $callback); |
|
| 78 | + add_action('admin_print_scripts-post-new.php', $callback); |
|
| 79 | 79 | |
| 80 | 80 | $this->plugin = $plugin; |
| 81 | 81 | } |
@@ -87,13 +87,13 @@ discard block |
||
| 87 | 87 | * @since 3.22.3 |
| 88 | 88 | */ |
| 89 | 89 | function is_gutenberg_page() { |
| 90 | - if ( function_exists( 'is_gutenberg_page' ) && is_gutenberg_page() ) { |
|
| 90 | + if (function_exists('is_gutenberg_page') && is_gutenberg_page()) { |
|
| 91 | 91 | // The Gutenberg plugin is on. |
| 92 | 92 | return TRUE; |
| 93 | 93 | } |
| 94 | 94 | |
| 95 | 95 | $current_screen = get_current_screen(); |
| 96 | - if ( method_exists( $current_screen, 'is_block_editor' ) && $current_screen->is_block_editor() ) { |
|
| 96 | + if (method_exists($current_screen, 'is_block_editor') && $current_screen->is_block_editor()) { |
|
| 97 | 97 | // Gutenberg page on 5+. |
| 98 | 98 | return TRUE; |
| 99 | 99 | } |
@@ -112,7 +112,7 @@ discard block |
||
| 112 | 112 | */ |
| 113 | 113 | private function is_ux_builder_editor() { |
| 114 | 114 | |
| 115 | - return function_exists( 'ux_builder_is_editor' ) |
|
| 115 | + return function_exists('ux_builder_is_editor') |
|
| 116 | 116 | && ux_builder_is_editor(); |
| 117 | 117 | } |
| 118 | 118 | |
@@ -124,19 +124,19 @@ discard block |
||
| 124 | 124 | public function enqueue_scripts() { |
| 125 | 125 | |
| 126 | 126 | // Bail out if this is G'berg. |
| 127 | - if ( $this->is_gutenberg_page() ) { |
|
| 127 | + if ($this->is_gutenberg_page()) { |
|
| 128 | 128 | return; |
| 129 | 129 | } |
| 130 | 130 | |
| 131 | 131 | // Dequeue potentially conflicting ontrapages angular scripts which any *are not* used on the edit screen. |
| 132 | 132 | // |
| 133 | 133 | // @see https://github.com/insideout10/wordlift-plugin/issues/832 |
| 134 | - wp_dequeue_script( 'ontrapagesAngular' ); |
|
| 135 | - wp_dequeue_script( 'ontrapagesApp' ); |
|
| 136 | - wp_dequeue_script( 'ontrapagesController' ); |
|
| 134 | + wp_dequeue_script('ontrapagesAngular'); |
|
| 135 | + wp_dequeue_script('ontrapagesApp'); |
|
| 136 | + wp_dequeue_script('ontrapagesController'); |
|
| 137 | 137 | |
| 138 | 138 | // If Gutenberg is enabled for the post, do not load the legacy edit.js. |
| 139 | - if ( function_exists( 'use_block_editor_for_post' ) && use_block_editor_for_post( get_post() ) ) { |
|
| 139 | + if (function_exists('use_block_editor_for_post') && use_block_editor_for_post(get_post())) { |
|
| 140 | 140 | return; |
| 141 | 141 | } |
| 142 | 142 | |
@@ -150,7 +150,7 @@ discard block |
||
| 150 | 150 | * |
| 151 | 151 | * @since 3.20.0 edit.js has been migrated to the new webpack configuration. |
| 152 | 152 | */ |
| 153 | - $script_name = plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/edit'; |
|
| 153 | + $script_name = plugin_dir_url(dirname(__FILE__)).'js/dist/edit'; |
|
| 154 | 154 | |
| 155 | 155 | /** |
| 156 | 156 | * Scripts_Helper introduced. |
@@ -192,9 +192,9 @@ discard block |
||
| 192 | 192 | ) |
| 193 | 193 | ); |
| 194 | 194 | |
| 195 | - wp_enqueue_style( 'wl-classic-editor', "$script_name.css", array(), $this->plugin->get_version() ); |
|
| 195 | + wp_enqueue_style('wl-classic-editor', "$script_name.css", array(), $this->plugin->get_version()); |
|
| 196 | 196 | $this->load_faq_scripts_and_styles(); |
| 197 | - $this->load_faq_settings( self::TINY_MCE ); |
|
| 197 | + $this->load_faq_settings(self::TINY_MCE); |
|
| 198 | 198 | } |
| 199 | 199 | |
| 200 | 200 | /** |
@@ -203,13 +203,13 @@ discard block |
||
| 203 | 203 | private function load_faq_scripts_and_styles() { |
| 204 | 204 | wp_enqueue_style( |
| 205 | 205 | 'wl-faq-metabox-style', |
| 206 | - plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/faq.css', |
|
| 206 | + plugin_dir_url(dirname(__FILE__)).'js/dist/faq.css', |
|
| 207 | 207 | array() |
| 208 | 208 | ); |
| 209 | 209 | Scripts_Helper::enqueue_based_on_wordpress_version( |
| 210 | 210 | 'wl-faq-metabox-script', |
| 211 | - plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/faq', |
|
| 212 | - array( 'wp-polyfill' ), |
|
| 211 | + plugin_dir_url(dirname(__FILE__)).'js/dist/faq', |
|
| 212 | + array('wp-polyfill'), |
|
| 213 | 213 | true |
| 214 | 214 | ); |
| 215 | 215 | } |
@@ -221,20 +221,20 @@ discard block |
||
| 221 | 221 | */ |
| 222 | 222 | public function get_faq_settings() { |
| 223 | 223 | return array( |
| 224 | - 'restUrl' => get_rest_url( NULL, WL_REST_ROUTE_DEFAULT_NAMESPACE . '/faq' ), |
|
| 224 | + 'restUrl' => get_rest_url(NULL, WL_REST_ROUTE_DEFAULT_NAMESPACE.'/faq'), |
|
| 225 | 225 | 'listBoxId' => self::FAQ_LIST_BOX_ID, |
| 226 | - 'nonce' => wp_create_nonce( 'wp_rest' ), |
|
| 226 | + 'nonce' => wp_create_nonce('wp_rest'), |
|
| 227 | 227 | 'postId' => get_the_ID(), |
| 228 | 228 | // Translation for warning, error message. |
| 229 | - 'invalidTagMessage' => sprintf( __( 'Invalid tags %s is present in answer', self::WORDLIFT_TEXT_DOMAIN ), "{INVALID_TAGS}" ), |
|
| 230 | - 'invalidWordCountMessage' => sprintf( __( 'Answer word count must not exceed %s words', self::WORDLIFT_TEXT_DOMAIN ), "{ANSWER_WORD_COUNT_WARNING_LIMIT}" ), |
|
| 231 | - 'questionText' => __( 'Question', self::WORDLIFT_TEXT_DOMAIN ), |
|
| 232 | - 'answerText' => __( 'Answer', self::WORDLIFT_TEXT_DOMAIN ), |
|
| 233 | - 'addQuestionOrAnswerText' => __( 'Add Question / Answer', self::WORDLIFT_TEXT_DOMAIN ), |
|
| 234 | - 'addQuestionText' => __( 'Add Question', self::WORDLIFT_TEXT_DOMAIN ), |
|
| 235 | - 'addAnswerText' => __( 'Add Answer', self::WORDLIFT_TEXT_DOMAIN ), |
|
| 236 | - 'noFaqItemsText' => __( 'No Questions or answers present', self::WORDLIFT_TEXT_DOMAIN ), |
|
| 237 | - 'updatingText' => __( 'Updating...', self::WORDLIFT_TEXT_DOMAIN ) |
|
| 229 | + 'invalidTagMessage' => sprintf(__('Invalid tags %s is present in answer', self::WORDLIFT_TEXT_DOMAIN), "{INVALID_TAGS}"), |
|
| 230 | + 'invalidWordCountMessage' => sprintf(__('Answer word count must not exceed %s words', self::WORDLIFT_TEXT_DOMAIN), "{ANSWER_WORD_COUNT_WARNING_LIMIT}"), |
|
| 231 | + 'questionText' => __('Question', self::WORDLIFT_TEXT_DOMAIN), |
|
| 232 | + 'answerText' => __('Answer', self::WORDLIFT_TEXT_DOMAIN), |
|
| 233 | + 'addQuestionOrAnswerText' => __('Add Question / Answer', self::WORDLIFT_TEXT_DOMAIN), |
|
| 234 | + 'addQuestionText' => __('Add Question', self::WORDLIFT_TEXT_DOMAIN), |
|
| 235 | + 'addAnswerText' => __('Add Answer', self::WORDLIFT_TEXT_DOMAIN), |
|
| 236 | + 'noFaqItemsText' => __('No Questions or answers present', self::WORDLIFT_TEXT_DOMAIN), |
|
| 237 | + 'updatingText' => __('Updating...', self::WORDLIFT_TEXT_DOMAIN) |
|
| 238 | 238 | ); |
| 239 | 239 | } |
| 240 | 240 | |
@@ -243,16 +243,16 @@ discard block |
||
| 243 | 243 | * |
| 244 | 244 | * @param $editor string specifying which text editor needed to be used. |
| 245 | 245 | */ |
| 246 | - private function load_faq_settings( $editor ) { |
|
| 246 | + private function load_faq_settings($editor) { |
|
| 247 | 247 | // This script also provides translations to gutenberg. |
| 248 | - wp_localize_script( 'wl-faq-metabox-script', '_wlFaqSettings', $this->get_faq_settings() ); |
|
| 248 | + wp_localize_script('wl-faq-metabox-script', '_wlFaqSettings', $this->get_faq_settings()); |
|
| 249 | 249 | |
| 250 | 250 | // Enqueue the FAQ style |
| 251 | - if ( $editor === self::GUTENBERG ) { |
|
| 251 | + if ($editor === self::GUTENBERG) { |
|
| 252 | 252 | Scripts_Helper::enqueue_based_on_wordpress_version( |
| 253 | 253 | 'wl-faq-gutenberg-plugin', |
| 254 | - plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/block-editor-faq-plugin', |
|
| 255 | - array( 'wp-polyfill' ), |
|
| 254 | + plugin_dir_url(dirname(__FILE__)).'js/dist/block-editor-faq-plugin', |
|
| 255 | + array('wp-polyfill'), |
|
| 256 | 256 | TRUE |
| 257 | 257 | ); |
| 258 | 258 | } |
@@ -266,11 +266,11 @@ discard block |
||
| 266 | 266 | public function enqueue_scripts_gutenberg() { |
| 267 | 267 | // Load FAQ settings. |
| 268 | 268 | $this->load_faq_scripts_and_styles(); |
| 269 | - $this->load_faq_settings( self::GUTENBERG ); |
|
| 269 | + $this->load_faq_settings(self::GUTENBERG); |
|
| 270 | 270 | |
| 271 | 271 | wp_register_script( |
| 272 | 272 | 'wl-block-editor', |
| 273 | - plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/block-editor.js', |
|
| 273 | + plugin_dir_url(dirname(__FILE__)).'js/dist/block-editor.js', |
|
| 274 | 274 | array( |
| 275 | 275 | 'react', |
| 276 | 276 | 'wordlift', |
@@ -283,24 +283,24 @@ discard block |
||
| 283 | 283 | ), |
| 284 | 284 | $this->plugin->get_version() |
| 285 | 285 | ); |
| 286 | - wp_localize_script( 'wl-block-editor', '_wlBlockEditorSettings', array( |
|
| 287 | - 'root' => esc_url_raw( rest_url() ), |
|
| 288 | - 'nonce' => wp_create_nonce( 'wp_rest' ) |
|
| 289 | - ) ); |
|
| 286 | + wp_localize_script('wl-block-editor', '_wlBlockEditorSettings', array( |
|
| 287 | + 'root' => esc_url_raw(rest_url()), |
|
| 288 | + 'nonce' => wp_create_nonce('wp_rest') |
|
| 289 | + )); |
|
| 290 | 290 | |
| 291 | 291 | /* |
| 292 | 292 | * @since 3.25.1 The hook is used by the wp_localize_script to register the _wlEntityTypes global object. |
| 293 | 293 | */ |
| 294 | 294 | wp_enqueue_style( |
| 295 | 295 | 'wl-block-editor', |
| 296 | - plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/block-editor.css', |
|
| 296 | + plugin_dir_url(dirname(__FILE__)).'js/dist/block-editor.css', |
|
| 297 | 297 | array(), |
| 298 | 298 | $this->plugin->get_version() |
| 299 | 299 | ); |
| 300 | 300 | |
| 301 | 301 | wp_enqueue_script( |
| 302 | 302 | 'wl-autocomplete-select', |
| 303 | - plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/autocomplete-select.js', |
|
| 303 | + plugin_dir_url(dirname(__FILE__)).'js/dist/autocomplete-select.js', |
|
| 304 | 304 | array(), |
| 305 | 305 | $this->plugin->get_version(), |
| 306 | 306 | TRUE |
@@ -308,7 +308,7 @@ discard block |
||
| 308 | 308 | |
| 309 | 309 | wp_enqueue_style( |
| 310 | 310 | 'wl-autocomplete-select', |
| 311 | - plugin_dir_url( dirname( __FILE__ ) ) . 'js/dist/autocomplete-select.css', |
|
| 311 | + plugin_dir_url(dirname(__FILE__)).'js/dist/autocomplete-select.css', |
|
| 312 | 312 | array(), |
| 313 | 313 | $this->plugin->get_version() |
| 314 | 314 | ); |