diff --git a/src/Core/System.php b/src/Core/System.php index d7214b81..e3a1b100 100644 --- a/src/Core/System.php +++ b/src/Core/System.php @@ -24,7 +24,7 @@ class System { public const ALIAS = 'system'; - public const ADVANCED = 'TypeRocketPro\Core\AdvancedSystem'; + public const ADVANCED = 'TypeRocket\Pro\Core\AdvancedSystem'; public const STATE = '_typerocket_site_state_changed'; protected $stash = []; diff --git a/src/Elements/Fields/Matrix.php b/src/Elements/Fields/Matrix.php index a1fd589a..11a18d25 100644 --- a/src/Elements/Fields/Matrix.php +++ b/src/Elements/Fields/Matrix.php @@ -94,8 +94,8 @@ public function enqueueScripts() wp_enqueue_script( 'jquery-ui-datepicker', [ 'jquery' ], false, true ); wp_enqueue_script( 'wp-color-picker' ); - if(class_exists('\TypeRocketPro\Core\AdvancedSystem')) { - call_user_func('\TypeRocketPro\Elements\Traits\EditorScripts::enqueueEditorScripts'); + if(class_exists('\TypeRocket\Pro\Core\AdvancedSystem')) { + call_user_func('\TypeRocket\Pro\Elements\Traits\EditorScripts::enqueueEditorScripts'); } } diff --git a/src/Http/Response.php b/src/Http/Response.php index e216c209..b01cbde4 100644 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -1064,8 +1064,8 @@ public function finish($forceResponseArray = false) $response->exitJson($statusCode); } - if(class_exists('\TypeRocketPro\Http\Download')) { - if( $returned instanceof \TypeRocketPro\Http\Download) { + if(class_exists('\TypeRocket\Pro\Http\Download')) { + if( $returned instanceof \TypeRocket\Pro\Http\Download) { $returned->send(); } } diff --git a/src/Services/ErrorService.php b/src/Services/ErrorService.php index 6820bd32..a0c67ef5 100644 --- a/src/Services/ErrorService.php +++ b/src/Services/ErrorService.php @@ -6,7 +6,7 @@ class ErrorService extends Service { public const ALIAS = 'tr-error'; - public const WHOOPS = 'TypeRocketPro\Features\Whoops'; + public const WHOOPS = 'TypeRocket\Pro\Features\Whoops'; /** * ErrorService constructor. diff --git a/src/Services/MailerService.php b/src/Services/MailerService.php index 1a0c34a5..fca14ace 100644 --- a/src/Services/MailerService.php +++ b/src/Services/MailerService.php @@ -29,11 +29,11 @@ public function register() : Service } /** - * @param \TypeRocketPro\Utility\Mailers\MailDriver|null $driver + * @param \TypeRocket\Pro\Utility\Mailers\MailDriver|null $driver * - * @return \TypeRocketPro\Utility\Mailers\MailDriver|null + * @return \TypeRocket\Pro\Utility\Mailers\MailDriver|null */ - public function driver(\TypeRocketPro\Utility\Mailers\MailDriver $driver = null) + public function driver(\TypeRocket\Pro\Utility\Mailers\MailDriver $driver = null) { if(func_num_args() == 0) { return $this->driver; diff --git a/src/Template/PostTypeModelComposer.php b/src/Template/PostTypeModelComposer.php index 700b41ae..4c66cb6d 100644 --- a/src/Template/PostTypeModelComposer.php +++ b/src/Template/PostTypeModelComposer.php @@ -63,7 +63,7 @@ public function publishedOn($d = 'F j, Y') { public function featuredImage($size = 'thumbnail', $classes = '', $from_cache = false) { if($from_cache && $this->data->meta->_thumbnail_id) { - $img = \TypeRocketPro\Utility\ImageCache::attachmentSrc($this->data->meta->_thumbnail_id, $size); + $img = \TypeRocket\Pro\Utility\ImageCache::attachmentSrc($this->data->meta->_thumbnail_id, $size); if($img) { return $img; diff --git a/src/Utility/Media.php b/src/Utility/Media.php new file mode 100644 index 00000000..6fb230c3 --- /dev/null +++ b/src/Utility/Media.php @@ -0,0 +1,90 @@ + + * $attachment_id = TypeRocket\Utility\Media::uploadFromUrl( "http://example.com/images/photo.png" ); + * echo wp_get_attachment_image( $attachment_id, 'large' ); + * + * @param string $url URL to be uploaded + * @param null|string $title If set, used as the post_title + * @param int $timeout How long to wait in seconds for download to complete + * + * @return int|null + */ + public static function uploadFromUrl(string $url, ?string $title = null, int $timeout = 300) : ?int + { + require_once( ABSPATH . "/wp-load.php"); + require_once( ABSPATH . "/wp-admin/includes/image.php"); + require_once( ABSPATH . "/wp-admin/includes/file.php"); + require_once( ABSPATH . "/wp-admin/includes/media.php"); + + $tmp = download_url( $url, $timeout ); + if ( is_wp_error( $tmp ) ) return null; + + $filename = pathinfo($url, PATHINFO_FILENAME); + $extension = pathinfo($url, PATHINFO_EXTENSION); + + if ( ! $extension ) { + $mime = mime_content_type( $tmp ); + $mime = is_string($mime) ? sanitize_mime_type( $mime ) : false; + + + $mime_extensions = array( + 'text/plain' => 'txt', + 'text/csv' => 'csv', + 'application/msword' => 'doc', + 'image/jpg' => 'jpg', + 'image/jpeg' => 'jpeg', + 'image/gif' => 'gif', + 'image/png' => 'png', + 'video/mp4' => 'mp4', + ); + + if ( isset( $mime_extensions[$mime] ) ) { + $extension = $mime_extensions[$mime]; + } else { + try { + unlink($tmp); + } catch (\Throwable $t) {} + return null; + } + } + + if(str_contains($extension, '?')) { + $extension = explode('?', $extension, 2)[0]; + } + + $allowed = get_allowed_mime_types(); + $extension = wp_check_filetype($extension, $allowed)['ext']; + + if(! in_array($extension, $allowed)) { + return null; + } + + // Upload by "sideloading": "the same way as an uploaded file is handled by media_handle_upload" + $args = [ + 'name' => "$filename.$extension", + 'tmp_name' => $tmp, + ]; + + // Do the upload + $attachment_id = media_handle_sideload($args, 0, $title); + + // Cleanup temp file + try { + unlink($tmp); + } catch (\Throwable $t) {} + + // Error uploading + if ( is_wp_error($attachment_id) ) return null; + + // Success, return attachment ID (int) + return (int) $attachment_id; + } +} \ No newline at end of file diff --git a/src/Utility/Str.php b/src/Utility/Str.php index 731b3191..747d9819 100644 --- a/src/Utility/Str.php +++ b/src/Utility/Str.php @@ -101,7 +101,7 @@ public static function ends($needle, $haystack) : bool */ public static function contains($needle, $haystack) : bool { - return str_contains($haystack, $needle); // ( mb_strpos( (string) $subject, (string) $needle ) !== false ); + return str_contains((string) $haystack, (string) $needle); } /** diff --git a/templates/Controllers/Template.txt b/templates/Controllers/Template.txt index 530643e1..43869f89 100644 --- a/templates/Controllers/Template.txt +++ b/templates/Controllers/Template.txt @@ -1,7 +1,7 @@