PluginProbe
BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites / 2.7.0
BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites v2.7.0
4.1.0 trunk 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.1.0 1.1.1 1.1.2 1.2.0 1.2.1 1.2.2 1.2.3 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 2.1.2 2.5.0 2.5.3 2.6.1 All 38 releases
blockspare / inc / layout / functions.php

functions.php in BlockSpare – Gutenberg Blocks for News, Magazine, Blog & Business Websites 2.7.0, at inc/layout/functions.php

205 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Layout block related functions.
4 *
5 * @package Blockspare
6 */
7
8 use Blockspare\Layouts\Component_Registry;
9
10 /**
11 * Registers layout components with the Component Registry
12 * for use in the Layouts block.
13 *
14 * @param array $data The component data.
15 *
16 * @return bool|WP_Error
17 */
18 function blockspare_register_layout_component( array $data ) {
19
20
21 $registry = Component_Registry::instance();
22
23 try {
24 $registry::add( $data );
25 return true;
26 } catch ( Exception $exception ) {
27 return new WP_Error( esc_html( $exception->getMessage() ) );
28 }
29 }
30
31 /**
32 * Unregisters the specified layout component from the Component Registry
33 * for use in the Layouts block.
34 *
35 * @return mixed Boolean true if component unregistered. WP_Error object if an error occurs.
36 * @param string $type The component type to be unregistered.
37 * @param string $key The unique layout key to be unregistered.
38 */
39 function blockspare_unregister_layout_component( $type, $key ) {
40 $registry = Component_Registry::instance();
41 try {
42 $registry::remove( $type, $key );
43 return true;
44 } catch ( Exception $exception ) {
45 return new WP_Error( esc_html( $exception->getMessage() ) );
46 }
47 }
48
49 /**
50 * Retrieves the specified layout component.
51 *
52 * @param string $type The layout component type.
53 * @param string $key The layout component's unique key.
54 *
55 * @return mixed|WP_Error
56 */
57 function blockspare_get_layout_component( $type, $key ) {
58
59 if ( empty( $type ) ) {
60 return new WP_Error( esc_html__( 'You must supply a type to retrieve a layout component.', 'blockspare' ) );
61 }
62
63 if ( empty( $key ) ) {
64 return new WP_Error( esc_html__( 'You must supply a key to retrieve a layout component.', 'blockspare' ) );
65 }
66
67 $type = sanitize_key( $type );
68
69 $key = sanitize_key( $key );
70
71 $registry = Component_Registry::instance();
72
73 try {
74 return $registry::get( $type, $key );
75 } catch ( Exception $exception ) {
76 return new WP_Error( esc_html( $exception->getMessage() ) );
77 }
78 }
79
80 /**
81 * Gets the registered layouts.
82 *
83 * @return array Array of registered layouts.
84 */
85 function blockspare_get_layouts() {
86 $registry = Component_Registry::instance();
87 return $registry::layouts();
88 }
89
90 /**
91 * Gets the registered sections.
92 *
93 * @return array Array of registered sections.
94 */
95 function blockspare_get_sections() {
96 $registry = Component_Registry::instance();
97 return $registry::sections();
98 }
99
100
101 /**
102 * Gets the registered blocks.
103 *
104 * @return array Array of registered blocks.
105 */
106 function blockspare_get_blocks() {
107 $registry = Component_Registry::instance();
108 return $registry::blocks();
109 }
110
111 /**
112 * Gets the registered pages.
113 *
114 * @return array Array of registered pages.
115 */
116 function blockspare_get_pages() {
117 $registry = Component_Registry::instance();
118 return $registry::pages();
119 }
120
121 /**
122 * Gets the registered headers.
123 *
124 * @return array Array of registered headers.
125 */
126 function blockspare_get_headers() {
127 $registry = Component_Registry::instance();
128 return $registry::headers();
129 }
130
131 /**
132 * Gets the registered footers.
133 *
134 * @return array Array of registered footers.
135 */
136 function blockspare_get_footers() {
137 $registry = Component_Registry::instance();
138 return $registry::footers();
139 }
140
141
142 function blockspare_import_images_replace_url($content =''){
143
144 preg_match_all( '#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $content, $match );
145 $all_links = array_unique( $match[0] );
146
147
148 if ( empty( $all_links ) ) {
149 return $content;
150 };
151 $link_mapping = array();
152 $image_links = array();
153 $other_links = array();
154 foreach ( $all_links as $key => $link ) {
155 if ( blockspare_block_templates_is_valid_image( $link ) ) {
156 if (
157 false === strpos( $link, '-150x' ) &&
158 false === strpos( $link, '-300x' ) &&
159 false === strpos( $link, '-1024x' )
160 ) {
161 $image_links[] = $link;
162 }
163 } else {
164
165
166 $other_links[] = $link;
167 }
168 }
169
170
171 if ( ! empty( $image_links ) ) {
172 foreach ( $image_links as $key => $image_url ) {
173 // Download remote image.
174 $image = array(
175 'url' => $image_url,
176 'id' => 0,
177 );
178 $downloaded_image = Blockspare_Design_Image_Importer::get_instance()->blockspare_import( $image );
179
180
181 $link_mapping[ $image_url ] = $downloaded_image['url'];
182 }
183 }
184 foreach ( $link_mapping as $old_url => $new_url ) {
185 $content = str_replace( $old_url, $new_url, $content );
186
187 // Replace the slashed URLs if any exist.
188 $old_url = str_replace( '/', '/\\', $old_url );
189 $new_url = str_replace( '/', '/\\', $new_url );
190 $content = str_replace( $old_url, $new_url, $content );
191 }
192
193 return $content;
194
195
196
197
198 }
199
200 function blockspare_block_templates_is_valid_image( $link = '' ) {
201 return preg_match( '/^((https?:\/\/)|(www\.))([a-z0-9-].?)+(:[0-9]+)?\/[\w\-]+\.(jpg|png|gif|jpeg)\/?$/i', $link );
202 }
203
204
205