| 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 |
function blockspare_get_templates() { |
| 142 |
$registry = Component_Registry::instance(); |
| 143 |
return $registry::templates(); |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
function blockspare_import_images_replace_url($content =''){ |
| 148 |
|
| 149 |
preg_match_all( '#\bhttps?://[^,\s()<>]+(?:\([\w\d]+\)|([^,[:punct:]\s]|/))#', $content, $match ); |
| 150 |
$all_links = array_unique( $match[0] ); |
| 151 |
|
| 152 |
|
| 153 |
if ( empty( $all_links ) ) { |
| 154 |
return $content; |
| 155 |
}; |
| 156 |
$link_mapping = array(); |
| 157 |
$image_links = array(); |
| 158 |
$other_links = array(); |
| 159 |
foreach ( $all_links as $key => $link ) { |
| 160 |
if ( blockspare_block_templates_is_valid_image( $link ) ) { |
| 161 |
if ( |
| 162 |
false === strpos( $link, '-150x' ) && |
| 163 |
false === strpos( $link, '-300x' ) && |
| 164 |
false === strpos( $link, '-1024x' ) |
| 165 |
) { |
| 166 |
$image_links[] = $link; |
| 167 |
} |
| 168 |
} else { |
| 169 |
|
| 170 |
|
| 171 |
$other_links[] = $link; |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
if ( ! empty( $image_links ) ) { |
| 177 |
foreach ( $image_links as $key => $image_url ) { |
| 178 |
// Download remote image. |
| 179 |
$image = array( |
| 180 |
'url' => $image_url, |
| 181 |
'id' => 0, |
| 182 |
); |
| 183 |
$downloaded_image = Blockspare_Design_Image_Importer::get_instance()->blockspare_import( $image ); |
| 184 |
|
| 185 |
|
| 186 |
$link_mapping[ $image_url ] = $downloaded_image['url']; |
| 187 |
} |
| 188 |
} |
| 189 |
foreach ( $link_mapping as $old_url => $new_url ) { |
| 190 |
$content = str_replace( $old_url, $new_url, $content ); |
| 191 |
|
| 192 |
// Replace the slashed URLs if any exist. |
| 193 |
$old_url = str_replace( '/', '/\\', $old_url ); |
| 194 |
$new_url = str_replace( '/', '/\\', $new_url ); |
| 195 |
$content = str_replace( $old_url, $new_url, $content ); |
| 196 |
} |
| 197 |
|
| 198 |
return $content; |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
} |
| 204 |
|
| 205 |
function blockspare_block_templates_is_valid_image( $link = '' ) { |
| 206 |
return preg_match( '/^((https?:\/\/)|(www\.))([a-z0-9-].?)+(:[0-9]+)?\/[\w\-]+\.(jpg|png|gif|jpeg)\/?$/i', $link ); |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|