ACFV1.php
3 years ago
CountLayoutInstall.php
3 years ago
ElImport.php
3 years ago
FrontEndFilterV1.php
3 years ago
GetPostsV1.php
3 years ago
ImageSizeV1.php
3 years ago
RestApi.php
3 years ago
RttpgV1.php
3 years ago
RttpgV1.php
330 lines
| 1 | <?php |
| 2 | |
| 3 | namespace RT\ThePostGrid\Controllers\Api; |
| 4 | |
| 5 | use Exception; |
| 6 | use RT\ThePostGrid\Helpers\Fns; |
| 7 | use WP_REST_Request; |
| 8 | use WP_REST_Server; |
| 9 | |
| 10 | class RttpgV1 { |
| 11 | |
| 12 | public function register_routes() { |
| 13 | // For css file save |
| 14 | register_rest_route( |
| 15 | 'rttpg/v1', |
| 16 | '/block-save-css/', |
| 17 | [ |
| 18 | [ |
| 19 | 'methods' => 'POST', |
| 20 | 'callback' => [ $this, 'save_block_css' ], |
| 21 | 'permission_callback' => function () { |
| 22 | return current_user_can( 'edit_posts' ); |
| 23 | }, |
| 24 | 'args' => [], |
| 25 | ], |
| 26 | ] |
| 27 | ); |
| 28 | // Get the Content by ID |
| 29 | register_rest_route( |
| 30 | 'rttpg/v1', |
| 31 | '/get-post-content/', |
| 32 | [ |
| 33 | [ |
| 34 | 'methods' => WP_REST_Server::CREATABLE, |
| 35 | 'callback' => [ $this, 'get_post_content' ], |
| 36 | 'permission_callback' => function () { |
| 37 | return current_user_can( 'edit_posts' ); |
| 38 | }, |
| 39 | 'args' => [], |
| 40 | ], |
| 41 | ] |
| 42 | ); |
| 43 | // Append Block CSS |
| 44 | register_rest_route( |
| 45 | 'rttpg/v1', |
| 46 | '/block-append-css/', |
| 47 | [ |
| 48 | [ |
| 49 | 'methods' => 'POST', |
| 50 | 'callback' => [ $this, 'append_block_css_callback' ], |
| 51 | 'permission_callback' => function () { |
| 52 | return current_user_can( 'edit_posts' ); |
| 53 | }, |
| 54 | 'args' => [], |
| 55 | ], |
| 56 | ] |
| 57 | ); |
| 58 | |
| 59 | register_rest_route( |
| 60 | 'rttpg/v1', |
| 61 | '/block-append-reusable-css/', |
| 62 | [ |
| 63 | [ |
| 64 | 'methods' => 'POST', |
| 65 | 'callback' => [ $this, 'append_reusable_css_callback' ], |
| 66 | 'permission_callback' => function () { |
| 67 | return current_user_can( 'edit_posts' ); |
| 68 | }, |
| 69 | 'args' => [], |
| 70 | ], |
| 71 | ] |
| 72 | ); |
| 73 | } |
| 74 | |
| 75 | |
| 76 | /** |
| 77 | * Get post content |
| 78 | * |
| 79 | * @param WP_REST_Request $request Wp $request variable |
| 80 | * |
| 81 | * @return array|void |
| 82 | */ |
| 83 | public function get_post_content( WP_REST_Request $request ) { |
| 84 | $params = $request->get_params(); |
| 85 | try { |
| 86 | if ( isset( $params['postId'] ) ) { |
| 87 | return [ |
| 88 | 'success' => true, |
| 89 | 'data' => ! empty( $params['postId'] ) ? get_post( $params['postId'] )->post_content : '', |
| 90 | 'message' => 'Get Data Success!!', |
| 91 | ]; |
| 92 | } |
| 93 | } catch ( Exception $e ) { |
| 94 | return [ |
| 95 | 'success' => false, |
| 96 | 'message' => $e->getMessage(), |
| 97 | ]; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Save block css for each post in a css file and enqueue the file to the post page |
| 103 | * |
| 104 | * @param WP_REST_Request $request |
| 105 | * |
| 106 | * @return array |
| 107 | */ |
| 108 | public function save_block_css( WP_REST_Request $request ) { |
| 109 | try { |
| 110 | global $wp_filesystem; |
| 111 | if ( ! $wp_filesystem ) { |
| 112 | require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 113 | } |
| 114 | |
| 115 | $params = $request->get_params(); |
| 116 | $post_id = (int) sanitize_text_field( $params['post_id'] ); |
| 117 | $is_previewing = $params['isPreviewing']; |
| 118 | |
| 119 | if ( $params['is_remain'] ) { |
| 120 | $block_css = $params['block_css']; |
| 121 | $filename = "rttpg-block-{$post_id}.css"; |
| 122 | |
| 123 | $upload_dir = wp_upload_dir(); |
| 124 | $dir = trailingslashit( $upload_dir['basedir'] ) . 'rttpg/'; |
| 125 | |
| 126 | // Add Import in first |
| 127 | $import_first = $this->set_import_url_to_top_css( $block_css ); |
| 128 | |
| 129 | if ( true === $is_previewing ) { |
| 130 | $filename = 'rttpg-block-preview.css'; |
| 131 | } else { |
| 132 | update_post_meta( $post_id, '_rttpg_block_css', $import_first ); |
| 133 | } |
| 134 | |
| 135 | WP_Filesystem( false, $upload_dir['basedir'], true ); |
| 136 | |
| 137 | if ( ! $wp_filesystem->is_dir( $dir ) ) { |
| 138 | $wp_filesystem->mkdir( $dir ); |
| 139 | } |
| 140 | // If fail to save css in directory, then it will show a message to user |
| 141 | if ( ! $wp_filesystem->put_contents( $dir . $filename, $import_first ) ) { |
| 142 | throw new Exception( __( 'CSS can not be saved due to permission!!!', 'the-post-grid' ) ); |
| 143 | } |
| 144 | } else { |
| 145 | if ( false === $is_previewing ) { |
| 146 | delete_post_meta( $post_id, '_rttpg_block_css' ); |
| 147 | $this->delete_post_resource( $post_id ); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | $success_message = __( 'The Post Grid preview css file has been updated.', 'the-post-grid' ); |
| 152 | // set block meta |
| 153 | if ( false === $is_previewing ) { |
| 154 | // ignore: phpcs |
| 155 | update_post_meta( $post_id, '__rttpg_available_blocks', serialize( $params['available_blocks'] ) ); |
| 156 | $success_message = __( 'The Post Grid block css file has been updated.', 'the-post-grid' ); |
| 157 | } |
| 158 | |
| 159 | return [ |
| 160 | 'success' => true, |
| 161 | 'message' => $success_message, |
| 162 | 'data' => $params, |
| 163 | ]; |
| 164 | } catch ( Exception $e ) { |
| 165 | return [ |
| 166 | 'success' => false, |
| 167 | 'message' => $e->getMessage(), |
| 168 | ]; |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * @param string $get_css |
| 174 | * |
| 175 | * @return mixed|string |
| 176 | */ |
| 177 | public function set_import_url_to_top_css( $get_css = '' ) { |
| 178 | $css_url = "@import url('https://fonts.googleapis.com/css?family="; |
| 179 | $google_font_exists = substr_count( $get_css, $css_url ); |
| 180 | |
| 181 | if ( $google_font_exists ) { |
| 182 | $pattern = sprintf( |
| 183 | '/%s(.+?)%s/ims', |
| 184 | preg_quote( $css_url, '/' ), |
| 185 | preg_quote( "');", '/' ) |
| 186 | ); |
| 187 | |
| 188 | if ( preg_match_all( $pattern, $get_css, $matches ) ) { |
| 189 | $fonts = $matches[0]; |
| 190 | $get_css = str_replace( $fonts, '', $get_css ); |
| 191 | if ( preg_match_all( '/font-weight[ ]?:[ ]?[\d]{3}[ ]?;/', $get_css, $matche_weight ) ) { // short out font weight |
| 192 | $weight = array_map( |
| 193 | function ( $val ) { |
| 194 | $process = trim( str_replace( [ 'font-weight', ':', ';' ], '', $val ) ); |
| 195 | if ( is_numeric( $process ) ) { |
| 196 | return $process; |
| 197 | } |
| 198 | }, |
| 199 | $matche_weight[0] |
| 200 | ); |
| 201 | foreach ( $fonts as $key => $val ) { |
| 202 | $fonts[ $key ] = str_replace( "');", '', $val ) . ':' . implode( ',', $weight ) . "');"; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | // Multiple same fonts to single font |
| 207 | $fonts = array_unique( $fonts ); |
| 208 | $get_css = implode( '', $fonts ) . $get_css; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | return $get_css; |
| 213 | } |
| 214 | |
| 215 | |
| 216 | /** |
| 217 | * Delete post related data |
| 218 | * |
| 219 | * @delete post css file |
| 220 | */ |
| 221 | private function delete_post_resource( $post_id = '' ) { |
| 222 | $post_id = $post_id ? $post_id : $this->is_single(); |
| 223 | if ( $post_id ) { |
| 224 | $upload_dir = wp_upload_dir()['basedir'] . '/rttpg/'; |
| 225 | $css_path = $upload_dir . 'rttpg-block-' . $post_id . '.css'; |
| 226 | if ( file_exists( $css_path ) ) { |
| 227 | unlink( $css_path ); |
| 228 | } |
| 229 | } |
| 230 | } |
| 231 | |
| 232 | /** |
| 233 | * Determine if current single page is WP Page Builder Page |
| 234 | * |
| 235 | * @return bool|false|int |
| 236 | */ |
| 237 | private function is_single() { |
| 238 | $post_id = get_the_ID(); |
| 239 | |
| 240 | if ( ! $post_id ) { |
| 241 | return false; |
| 242 | } |
| 243 | |
| 244 | return $post_id; |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * @param WP_REST_Request $request |
| 249 | * |
| 250 | * @return void |
| 251 | */ |
| 252 | public function append_block_css_callback( WP_REST_Request $request ) { |
| 253 | try { |
| 254 | global $wp_filesystem; |
| 255 | if ( ! $wp_filesystem ) { |
| 256 | require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 257 | } |
| 258 | $params = $request->get_params(); |
| 259 | $css = $params['css']; |
| 260 | $post_id = (int) sanitize_text_field( $params['post_id'] ); |
| 261 | if ( $post_id ) { |
| 262 | $filename = "block-css-{$post_id}.css"; |
| 263 | $upload_dir = wp_upload_dir(); |
| 264 | $dir = trailingslashit( $upload_dir['basedir'] ) . 'rttpg/'; |
| 265 | if ( file_exists( $dir . $filename ) ) { |
| 266 | $file = fopen( $dir . $filename, 'a' ); |
| 267 | fwrite( $file, $css ); |
| 268 | fclose( $file ); |
| 269 | } |
| 270 | $get_data = get_post_meta( $post_id, '_rttpg_block_css', true ); |
| 271 | update_post_meta( $post_id, '_rttpg_block_css', $get_data . $css ); |
| 272 | |
| 273 | wp_send_json_success( |
| 274 | [ |
| 275 | 'success' => true, |
| 276 | 'message' => 'Update done' . $get_data, |
| 277 | ] |
| 278 | ); |
| 279 | } |
| 280 | } catch ( Exception $e ) { |
| 281 | wp_send_json_error( |
| 282 | [ |
| 283 | 'success' => false, |
| 284 | 'message' => $e->getMessage(), |
| 285 | ] |
| 286 | ); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | |
| 291 | /** |
| 292 | * @param WP_REST_Request $request |
| 293 | * |
| 294 | * @return void |
| 295 | */ |
| 296 | public function append_reusable_css_callback( $request ) { |
| 297 | try { |
| 298 | global $wp_filesystem; |
| 299 | if ( ! $wp_filesystem ) { |
| 300 | require_once ABSPATH . 'wp-admin/includes/file.php'; |
| 301 | } |
| 302 | $params = $request->get_params(); |
| 303 | $css = $params['css']; |
| 304 | |
| 305 | $filename = 'blocks-preview.css'; |
| 306 | $upload_dir = wp_upload_dir(); |
| 307 | $dir = trailingslashit( $upload_dir['basedir'] ) . 'rttpg/'; |
| 308 | if ( file_exists( $dir . $filename ) ) { |
| 309 | $file = fopen( $dir . $filename, 'a' ); |
| 310 | fwrite( $file, $css ); |
| 311 | fclose( $file ); |
| 312 | } |
| 313 | wp_send_json_success( |
| 314 | [ |
| 315 | 'success' => true, |
| 316 | 'message' => 'appended reusable css in preview file', |
| 317 | ] |
| 318 | ); |
| 319 | |
| 320 | } catch ( Exception $e ) { |
| 321 | wp_send_json_error( |
| 322 | [ |
| 323 | 'success' => false, |
| 324 | 'message' => $e->getMessage(), |
| 325 | ] |
| 326 | ); |
| 327 | } |
| 328 | } |
| 329 | |
| 330 | } |