PluginProbe
Qi Blocks / trunk
Qi Blocks vtrunk
1.5.3 1.5.2 1.5.1 trunk 1.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1 1.1.1 1.1.2 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 All 45 releases
qi-blocks / inc / media / class-qi-blocks-media.php

class-qi-blocks-media.php in Qi Blocks trunk, at inc/media/class-qi-blocks-media.php

172 lines 5.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 // Exit if accessed directly.
5 exit;
6 }
7
8 if ( ! class_exists( 'Qi_Blocks_Media' ) ) {
9 class Qi_Blocks_Media {
10 private static $instance;
11
12 public function __construct() {
13 // Create global options.
14 add_action( 'init', array( $this, 'add_option' ) );
15
16 // Add new media sizes.
17 add_action( 'init', array( $this, 'set_image_support' ) );
18
19 // Extend main rest api routes with new case.
20 add_filter( 'qi_blocks_filter_rest_api_routes', array( $this, 'add_rest_api_routes' ) );
21 }
22
23 /**
24 * Module class instance
25 *
26 * @return Qi_Blocks_Media
27 */
28 public static function get_instance() {
29 if ( is_null( self::$instance ) ) {
30 self::$instance = new self();
31 }
32
33 return self::$instance;
34 }
35
36 public function add_option() {
37 if ( ! get_option( 'qi_blocks_cropped_images' ) ) {
38 add_option(
39 'qi_blocks_cropped_images',
40 array()
41 );
42 }
43 }
44
45 public function get_image_sizes() {
46 $image_sizes = array();
47
48 $image_sizes[] = array(
49 'slug' => 'qi_blocks_image_size_square',
50 'label' => esc_html__( 'Qi Square Size', 'qi-blocks' ),
51 'label_simple' => esc_html__( 'Square', 'qi-blocks' ),
52 'default_crop' => true,
53 'default_width' => 650,
54 'default_height' => 650,
55 );
56
57 $image_sizes[] = array(
58 'slug' => 'qi_blocks_image_size_landscape',
59 'label' => esc_html__( 'Qi Landscape Size', 'qi-blocks' ),
60 'label_simple' => esc_html__( 'Landscape', 'qi-blocks' ),
61 'default_crop' => true,
62 'default_width' => 1300,
63 'default_height' => 650,
64 );
65
66 $image_sizes[] = array(
67 'slug' => 'qi_blocks_image_size_portrait',
68 'label' => esc_html__( 'Qi Portrait Size', 'qi-blocks' ),
69 'label_simple' => esc_html__( 'Portrait', 'qi-blocks' ),
70 'default_crop' => true,
71 'default_width' => 650,
72 'default_height' => 1300,
73 );
74
75 $image_sizes[] = array(
76 'slug' => 'qi_blocks_image_size_huge_square',
77 'label' => esc_html__( 'Qi Huge Square Size', 'qi-blocks' ),
78 'label_simple' => esc_html__( 'Huge Square', 'qi-blocks' ),
79 'default_crop' => true,
80 'default_width' => 1300,
81 'default_height' => 1300,
82 );
83
84 return apply_filters( 'qi_blocks_filter_set_image_sizes', $image_sizes );
85 }
86
87 public function set_image_support() {
88 $image_sizes = $this->get_image_sizes();
89
90 if ( ! empty( $image_sizes ) ) {
91 foreach ( $image_sizes as $size ) {
92 $image_size_name = $size['slug'];
93 $width_field_name = $image_size_name . '_w';
94 $height_field_name = $image_size_name . '_h';
95 $crop_field_name = $image_size_name . '_crop';
96 $width_value = is_string( get_option( $width_field_name ) ) ? get_option( $width_field_name ) : $size['default_width'];
97 $height_value = is_string( get_option( $height_field_name ) ) ? get_option( $height_field_name ) : $size['default_height'];
98 $crop_value = get_option( $crop_field_name ) !== false ? get_option( $crop_field_name ) : $size['default_crop'];
99
100 add_image_size( $image_size_name, $width_value, $height_value, $crop_value );
101 }
102 }
103 }
104
105 public function add_rest_api_routes( $routes ) {
106 $routes['resize-image'] = array(
107 'route' => 'resize-image',
108 'methods' => WP_REST_Server::CREATABLE,
109 'callback' => array( $this, 'resize_image_callback' ),
110 'permission_callback' => function () {
111 return current_user_can( 'edit_posts' );
112 },
113 'args' => array(
114 'image_id' => array(
115 'required' => true,
116 'validate_callback' => function ( $param ) {
117 return intval( $param );
118 },
119 ),
120 'custom_size' => array(
121 'required' => true,
122 'validate_callback' => function ( $param ) {
123 return is_array( $param ) ? $param : (array) $param;
124 },
125 ),
126 ),
127 );
128
129 return $routes;
130 }
131
132 public function resize_image_callback( $response ) {
133
134 if ( ! isset( $response ) || empty( $response->get_body() ) ) {
135 qi_blocks_get_ajax_status( 'error', esc_html__( 'Rest is invalid', 'qi-blocks' ), array() );
136 } elseif ( ! current_user_can( 'upload_files' ) ) {
137 qi_blocks_get_ajax_status( 'error', esc_html__( 'You are not authorized.', 'qi-blocks' ), array() );
138 } else {
139
140 $response_data = json_decode( $response->get_body() );
141 $image_id = isset( $response_data->image_id ) && ! empty( $response_data->image_id ) ? intval( $response_data->image_id ) : 0;
142 $image_custom_size = isset( $response_data->custom_size ) && is_object( $response_data->custom_size ) ? (array) $response_data->custom_size : array();
143
144 if ( ! current_user_can( 'edit_post', $image_id ) ) {
145 qi_blocks_get_ajax_status( 'error', esc_html__( 'Sorry, you are not allowed to edit this post.', 'qi-blocks' ), array() );
146 } elseif ( ! empty( $image_id ) && ! empty( $image_custom_size ) ) {
147 $editor_supports = array(
148 'mime_type' => get_post_mime_type( $image_id ),
149 'methods' => array( 'rotate' ),
150 );
151
152 if ( ! wp_image_editor_supports( $editor_supports ) ) {
153 qi_blocks_get_ajax_status( 'error', esc_html__( 'Image rotation is not supported by your web host.', 'qi-blocks' ), array() );
154 }
155
156 $resize_image = qi_blocks_resize_image( $image_id, $image_custom_size );
157
158 if ( ! empty( $resize_image ) ) {
159 qi_blocks_get_ajax_status( 'success', esc_html__( 'Image is resized', 'qi-blocks' ), $resize_image );
160 } else {
161 qi_blocks_get_ajax_status( 'error', esc_html__( 'Image can not be resized', 'qi-blocks' ), array() );
162 }
163 } else {
164 qi_blocks_get_ajax_status( 'error', esc_html__( 'Parameters are invalid', 'qi-blocks' ), array() );
165 }
166 }
167 }
168 }
169
170 Qi_Blocks_Media::get_instance();
171 }
172