Skip to content

Commit

Permalink
clean up image download
Browse files Browse the repository at this point in the history
  • Loading branch information
kadencewp committed Feb 16, 2024
1 parent 9eb8f61 commit 2cc0b38
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 24 deletions.
43 changes: 26 additions & 17 deletions includes/class-kadence-blocks-prebuilt-library-rest-api.php
Original file line number Diff line number Diff line change
Expand Up @@ -2392,19 +2392,31 @@ public function get_image_info( $images, $target_src ) {
*/
public function check_for_local_image( $image_data ) {
global $wpdb;

// Thanks BrainstormForce for this idea.
// Check if image is already local based on meta key and custom hex value.
$image_id = $wpdb->get_var(
$wpdb->prepare(
'SELECT `post_id` FROM `' . $wpdb->postmeta . '`
WHERE `meta_key` = \'_kadence_blocks_image_hash\'
AND `meta_value` = %s
;',
sha1( $image_data['url'] )
)
);
if ( $image_id ) {
$image_id = '';
if ( ! empty( $image_data['url'] ) && strpos( $image_data['url'], get_site_url() ) !== false ) {
$image_id = attachment_url_to_postid( $image_data['url'] );
if ( empty( $image_id ) ) {
// Get unsized version use Regular expression to find the pattern -numberxnumber
$pattern = "/-\d+x\d+/";
// Replace the pattern with an empty string.
$cleaned_url = preg_replace( $pattern, '', $image_data['url'] );
$image_id = attachment_url_to_postid( $cleaned_url );
}
}
if ( empty( $image_id ) ) {
// Thanks BrainstormForce for this idea.
// Check if image is already local based on meta key and custom hex value.
$image_id = $wpdb->get_var(
$wpdb->prepare(
'SELECT `post_id` FROM `' . $wpdb->postmeta . '`
WHERE `meta_key` = \'_kadence_blocks_image_hash\'
AND `meta_value` = %s
;',
sha1( $image_data['url'] )
)
);
}
if ( ! empty( $image_id ) ) {
$local_image = array(
'id' => $image_id,
'url' => wp_get_attachment_url( $image_id ),
Expand All @@ -2428,10 +2440,7 @@ public function check_for_image( $link = '' ) {
if ( empty( $link ) ) {
return false;
}
if ( strpos( $link, 'prophecyimg.fly.dev' ) !== false ) {
return true;
}
if ( strpos( $link, 'images.pexels.com' ) !== false ) {
if ( strpos( $link, 'https://images.pexels.com' ) !== false ) {
return true;
}
return preg_match( '/^((https?:\/\/)|(www\.))([a-z0-9-].?)+(:[0-9]+)?\/[\w\-]+\.(jpg|png|gif|webp|jpeg)\/?$/i', $link );
Expand Down
7 changes: 0 additions & 7 deletions src/plugins/prebuilt-library/pattern-library.js
Original file line number Diff line number Diff line change
Expand Up @@ -639,13 +639,6 @@ function PatternLibrary( {
}, [aIUserData]);
async function onInsertContent( pattern ) {
setIsImporting( true );
// const response = await getPattern( ( pattern?.type === 'page' ? 'pages' : 'section' ), ( pattern?.type ? pattern.type : 'pattern' ), ( pattern?.id ? pattern.id : '' ), ( pattern?.style ? pattern.style : 'light' ) );
// let patternBlocks = pattern?.content ? pattern.content : '';
// if ( response && ! patternBlocks ) {
// patternBlocks = parse( response, {
// __unstableSkipMigrationLogs: true
// });
// }
let patternBlocks = pattern?.content ? pattern.content : '';
processImportContent( patternBlocks );
}
Expand Down

0 comments on commit 2cc0b38

Please sign in to comment.