Changeset 3129664
- Timestamp:
- 08/01/2024 06:23:05 PM (7 weeks ago)
- Location:
- s3-image-optimizer
- Files:
-
- 8 edited
- 1 copied
Legend:
- Unmodified
- Added
- Removed
-
s3-image-optimizer/tags/2.6.0/changelog.txt
r2969432 r3129664 1 2 3 4 5 6 7 1 8 = 2.5.1 = 2 9 * changed: use updated WP coding standards -
s3-image-optimizer/tags/2.6.0/phpcs.ruleset.xml
r2969430 r3129664 21 21 <rule ref="WordPress.PHP.NoSilencedErrors"> 22 22 <properties> 23 <property name="custom _whitelist" type="array" value="ini_get,exif_read_data,ob_end_flush"/>23 <property name="customist" type="array" value="ini_get,exif_read_data,ob_end_flush"/> 24 24 </properties> 25 25 </rule> 26 26 <rule ref="WordPress.NamingConventions.ValidVariableName"> 27 27 <properties> 28 <property name="customPropertiesWhitelist" type="array" value="formatOutput,imgQuality,parentNode,nextSibling,documentElement,imagePath,webimagePath,thumbPath,fileName,Version,Name" /> 29 </properties> 30 <properties> 31 <property name="customVariablesWhitelist" type="array" value="formatOutput,parentNode,nextSibling,documentElement" /> 28 <property name="allowed_custom_properties" type="array" value="formatOutput,imgQuality,parentNode,nextSibling,documentElement,imagePath,webimagePath,thumbPath,fileName,Version,Name" /> 32 29 </properties> 33 30 </rule> 34 31 35 <config name="testVersion"> 7.3-</config>32 <config name="testVersion">-</config> 36 33 <rule ref="PHPCompatibilityWP"> 37 34 <severity>10</severity> -
s3-image-optimizer/tags/2.6.0/readme.txt
r2969432 r3129664 1 1 === S3 Image Optimizer === 2 2 Contributors: nosilver4u 3 Tags: amazon, s3, image, optimize, optimization, photo, picture, seo, compression, wp-cli4 Requires at least: 6. 15 Tested up to: 6. 36 Requires PHP: 7.37 Stable tag: 2. 5.13 Tags: , compression, wp-cli 4 Requires at least: 6. 5 Tested up to: 6. 6 Requires PHP: 7 Stable tag: 2. 8 8 License: GPLv3 9 9 … … 40 40 ` 41 41 42 43 44 45 46 47 48 49 50 51 52 42 53 [View the full list of region names.](https://docs.aws.amazon.com/general/latest/gr/rande.html#regional-endpoints) 43 54 … … 62 73 63 74 == Changelog == 75 76 77 78 79 80 81 64 82 65 83 = 2.5.1 = -
s3-image-optimizer/tags/2.6.0/s3-image-optimizer.php
r2969432 r3129664 12 12 Description: Reduce file sizes for images in S3 buckets using lossless and lossy optimization methods via the EWWW Image Optimizer. 13 13 Author: Exactly WWW 14 Version: 2.5.1 15 Requires at least: 6.1 16 Requires PHP: 7.3 14 Version: 2.6.0 15 Requires at least: 6.3 16 Requires PHP: 8.1 17 Requires Plugins: ewww-image-optimizer 17 18 Author URI: https://ewww.io/ 18 19 License: GPLv3 … … 22 23 * Constants 23 24 */ 24 define( 'S3IO_VERSION', 2 51);25 define( 'S3IO_VERSION', 2 ); 25 26 // This is the full path of the plugin file itself. 26 27 define( 'S3IO_PLUGIN_FILE', __FILE__ ); … … 75 76 add_action( 'admin_notices', 's3io_missing_ewww_plugin' ); 76 77 } 77 if ( ! function_exists( 'ewwwio' ) || ! function_exists( 'ewww_image_optimizer_filesize') ) {78 if ( ) ) { 78 79 add_action( 'network_admin_notices', 's3io_ewww_plugin_outdated' ); 79 80 add_action( 'admin_notices', 's3io_ewww_plugin_outdated' ); … … 194 195 if ( defined( 'S3_IMAGE_OPTIMIZER_ENDPOINT' ) && S3_IMAGE_OPTIMIZER_ENDPOINT ) { 195 196 $args['endpoint'] = S3_IMAGE_OPTIMIZER_ENDPOINT; 197 198 199 196 200 } 197 201 return $args; … … 636 640 if ( empty( $bucket_list ) ) { 637 641 global $s3io_amazon_web_services; 642 638 643 try { 639 644 $client = $s3io_amazon_web_services->get_client(); 640 645 } catch ( Exception $e ) { 641 646 $s3io_errors[] = $e->getMessage(); 642 }643 $bucket_list = array();647 648 644 649 try { 645 650 $buckets = $client->listBuckets(); … … 699 704 $bucket_list = s3io_get_selected_buckets(); 700 705 if ( ! empty( $s3io_errors ) ) { 701 /* translators: %s: AWS error message */702 706 if ( $wpcli ) { 703 707 return 0; … … 721 725 } 722 726 try { 723 $location = $client->getBucketLocation( 724 array( 725 'Bucket' => $bucket, 726 ) 727 ); 727 $location = $client->getBucketLocation( array( 'Bucket' => $bucket ) ); 728 728 } catch ( Exception $e ) { 729 729 $location = new WP_Error( 'exception', $e->getMessage() ); … … 1134 1134 $alternate = true; 1135 1135 foreach ( $already_optimized as $optimized_image ) { 1136 $file_size = size_format( $optimized_image['image_size'], 2);1136 $file_size = size_format( $optimized_image['image_size'], ); 1137 1137 $file_size = str_replace( '.00 B ', ' B', $file_size ); 1138 1138 ?> … … 1143 1143 <?php 1144 1144 /* translators: %s: size of image, in bytes */ 1145 echo esc_html( $optimized_image['results'] ) . ' <br>' . sprintf( esc_html__( 'Image Size: %s', 's3-image-optimizer' ), (int) $file_size);1145 echo esc_html( $optimized_image['results'] ) . ' <br>' . sprintf( esc_html__( 'Image Size: %s', 's3-image-optimizer' ), ); 1146 1146 ?> 1147 1147 <br><a class="removeimage" onclick="s3ioRemoveImage( <?php echo (int) $optimized_image['id']; ?> )"><?php esc_html_e( 'Remove from table', 's3-image-optimizer' ); ?></a> … … 1472 1472 die(); 1473 1473 } 1474 1475 1476 1474 1477 $new_size = ewww_image_optimizer_filesize( $filename ); 1475 1478 if ( $new_size < $fetch_result['ContentLength'] ) { … … 1485 1488 'Key' => $image_record['path'], 1486 1489 'SourceFile' => $filename, 1487 'ACL' => 'public-read',1488 1490 'ContentType' => $fetch_result['ContentType'], 1489 1491 'CacheControl' => 'max-age=31536000', … … 1491 1493 ) 1492 1494 ); 1495 1496 1497 1498 1499 1500 1501 1502 1503 1493 1504 } catch ( Exception $e ) { 1494 1505 if ( defined( 'WP_CLI' ) && WP_CLI ) { … … 1519 1530 'Key' => $image_record['path'] . '.webp', 1520 1531 'SourceFile' => $filename . '.webp', 1521 'ACL' => 'public-read',1522 1532 'ContentType' => 'image/webp', 1523 1533 'CacheControl' => 'max-age=31536000', … … 1525 1535 ) 1526 1536 ); 1537 1538 1539 1540 1541 1542 1543 1544 1545 1527 1546 } catch ( Exception $e ) { 1528 1547 if ( defined( 'WP_CLI' ) && WP_CLI ) { … … 1605 1624 function s3io_url_loop() { 1606 1625 s3io_debug_message( '<b>' . __FUNCTION__ . '()</b>' ); 1607 $output = array();1608 1626 // Verify that an authorized user has started the optimizer. 1609 1627 $permissions = apply_filters( 'ewww_image_optimizer_bulk_permissions', '' ); … … 1611 1629 die( wp_json_encode( array( 'error' => esc_html__( 'Access token has expired, please reload the page.', 's3-image-optimizer' ) ) ) ); 1612 1630 } 1613 $started = microtime( true ); 1631 global $s3io_errors; 1632 $s3io_errors = array(); 1633 $output = array(); 1634 $started = microtime( true ); 1614 1635 if ( 1615 1636 function_exists( 'ewww_image_optimizer_stl_check' ) && … … 1622 1643 if ( empty( $_REQUEST['s3io_url'] ) ) { 1623 1644 $output['error'] = esc_html__( 'No URL supplied', 's3-image-optimizer' ); 1624 echo wp_json_encode( $output ); 1625 die(); 1645 wp_die( wp_json_encode( $output ) ); 1626 1646 } 1627 1647 $url = esc_url_raw( wp_unslash( $_REQUEST['s3io_url'] ) ); … … 1629 1649 $url_args = s3io_get_args_from_url( $url ); 1630 1650 } 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1631 1664 if ( empty( $url ) || empty( $url_args ) ) { 1632 1665 $output['error'] = esc_html__( 'Invalid URL supplied', 's3-image-optimizer' ); 1633 echo wp_json_encode( $output ); 1634 die(); 1666 wp_die( wp_json_encode( $output ) ); 1635 1667 } 1636 1668 $url_args['path'] = ltrim( $url_args['path'], '/' ); … … 1638 1670 $upload_dir = s3io_make_upload_dir(); 1639 1671 if ( ! $upload_dir ) { 1640 die(1672 die( 1641 1673 wp_json_encode( 1642 1674 array( … … 1649 1681 s3io_debug_message( "stashing files in $upload_dir" ); 1650 1682 if ( false !== strpos( $upload_dir, 's3://' ) ) { 1651 /* translators: %s: path to uploads directory */ 1652 die( wp_json_encode( array( 'error' => sprintf( esc_html__( 'Received an unusable working directory: %s', 's3-image-optimizer' ), $upload_dir ) ) ) ); 1683 wp_die( 1684 wp_json_encode( 1685 array( 1686 /* translators: %s: path to uploads directory */ 1687 'error' => sprintf( esc_html__( 'Received an unusable working directory: %s', 's3-image-optimizer' ), $upload_dir ), 1688 ) 1689 ) 1690 ); 1653 1691 } 1654 1692 global $s3io_amazon_web_services; … … 1656 1694 $client = $s3io_amazon_web_services->get_client(); 1657 1695 } catch ( Exception $e ) { 1658 die( wp_json_encode( array( 'error' => wp_kses_post( $e->getMessage() ) ) ) ); 1659 } 1660 try { 1661 $location = $client->getBucketLocation( 1662 array( 1663 'Bucket' => $url_args['bucket'], 1696 wp_die( 1697 wp_json_encode( 1698 array( 1699 'error' => sprintf( 1700 /* translators: %s: AWS/S3 error message */ 1701 esc_html__( 'Error connecting to S3: %s', 's3-image-optimizer' ), 1702 wp_kses_post( $e->getMessage() ) 1703 ), 1704 ) 1664 1705 ) 1665 1706 ); 1666 } catch ( Exception $e ) {1667 $location = new WP_Error( 'exception', $e->getMessage() );1668 1707 } 1669 1708 $filename = $upload_dir . $url_args['path']; … … 1691 1730 die( wp_json_encode( $output ) ); 1692 1731 } 1732 1733 1734 1693 1735 $new_size = filesize( $filename ); 1694 1736 if ( $new_size < $fetch_result['ContentLength'] ) { 1695 1737 // Re-upload to S3. 1696 $client->putObject( 1697 array( 1698 'Bucket' => $url_args['bucket'], 1699 'Key' => $url_args['path'], 1700 'SourceFile' => $filename, 1701 'ACL' => 'public-read', 1702 'ContentType' => $fetch_result['ContentType'], 1703 'CacheControl' => 'max-age=31536000', 1704 'Expires' => gmdate( 'D, d M Y H:i:s O', time() + 31536000 ), 1705 ) 1706 ); 1738 try { 1739 $client->putObject( 1740 array( 1741 'Bucket' => $url_args['bucket'], 1742 'Key' => $url_args['path'], 1743 'SourceFile' => $filename, 1744 'ContentType' => $fetch_result['ContentType'], 1745 'CacheControl' => 'max-age=31536000', 1746 'Expires' => gmdate( 'D, d M Y H:i:s O', time() + 31536000 ), 1747 ) 1748 ); 1749 if ( ! $ownership_control_enforced ) { 1750 $client->putObjectAcl( 1751 array( 1752 'Bucket' => $url_args['bucket'], 1753 'Key' => $url_args['path'], 1754 'ACL' => 'public-read', 1755 ) 1756 ); 1757 } 1758 } catch ( Exception $e ) { 1759 $output['error'] = wp_kses_post( "Put failed for bucket: {$url_args['bucket']}, path: {$url_args['path']}, message:" . $e->getMessage() ); 1760 wp_die( wp_json_encode( $output ) ); 1761 } 1707 1762 } 1708 1763 unlink( $filename ); 1709 1764 $webp_size = ewww_image_optimizer_filesize( $filename . '.webp' ); 1710 1765 if ( $webp_size ) { 1711 if ( $verbose && defined( 'WP_CLI' ) && WP_CLI ) {1712 WP_CLI::line( "About to upload $filename.webp" );1713 }1714 1766 // Upload to S3. 1715 1767 try { … … 1719 1771 'Key' => $url_args['path'] . '.webp', 1720 1772 'SourceFile' => $filename . '.webp', 1721 'ACL' => 'public-read',1722 1773 'ContentType' => 'image/webp', 1723 1774 'CacheControl' => 'max-age=31536000', … … 1725 1776 ) 1726 1777 ); 1778 1779 1780 1781 1782 1783 1784 1785 1786 1727 1787 } catch ( Exception $e ) { 1728 if ( defined( 'WP_CLI' ) && WP_CLI ) { 1729 WP_CLI::error( "Put failed for bucket: {$image_record['bucket']}, path: {$image_record['path']}.webp, message:" . $e->getMessage() ); 1730 } else { 1731 $output['error'] = wp_kses_post( "Put failed for bucket: {$image_record['bucket']}, path: {$image_record['path']}.webp, message:" . $e->getMessage() ); 1732 echo wp_json_encode( $output ); 1733 } 1734 die(); 1735 } 1736 if ( $verbose && defined( 'WP_CLI' ) && WP_CLI ) { 1737 WP_CLI::line( "Finished upload of $filename.webp" ); 1788 $output['error'] = wp_kses_post( "Put failed for bucket: {$url_args['bucket']}, path: {$url_args['path']}.webp, message:" . $e->getMessage() ); 1789 wp_die( wp_json_encode( $output ) ); 1738 1790 } 1739 1791 unlink( $filename . '.webp' ); … … 1753 1805 1754 1806 die( wp_json_encode( $output ) ); 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1755 1834 } 1756 1835 … … 1786 1865 } 1787 1866 } 1788 global $s3io_amazon_web_services; 1789 try { 1790 $client = $s3io_amazon_web_services->get_client(); 1791 } catch ( Exception $e ) { 1792 s3io_debug_message( 'unable to initialize AWS client lib' ); 1793 return false; 1794 } 1795 try { 1796 $buckets = $client->listBuckets(); 1797 } catch ( Exception $e ) { 1798 $buckets = new WP_Error( 'exception', $e->getMessage() ); 1799 } 1800 1801 // If retrieving buckets from AWS failed, then we use the bucketlist option. 1802 if ( is_wp_error( $buckets ) ) { 1803 $bucket_list = get_option( 's3io_bucketlist' ); 1804 } else { 1805 $bucket_list = array(); 1806 foreach ( $buckets['Buckets'] as $aws_bucket ) { 1807 $bucket_list[] = $aws_bucket['Name']; 1808 } 1809 } 1867 1868 $bucket_list = s3io_get_selected_buckets(); 1810 1869 1811 1870 // If we don't have a list of buckets, we can't do much more here. … … 1835 1894 // Otherwise, we must have a custom domain, so lets do a quick search for the attachment in all buckets. 1836 1895 // Doing it in a separate foreach, in case there are performance implications of switching the region in accounts with lots of buckets. 1896 1897 1898 1899 1900 1901 1902 1837 1903 $key = ltrim( $urlinfo['path'], '/' ); 1838 1904 foreach ( $bucket_list as $aws_bucket ) { 1839 try {1840 $location = $client->getBucketLocation(1841 array(1842 'Bucket' => $aws_bucket,1843 )1844 );1845 } catch ( Exception $e ) {1846 $location = new WP_Error( 'exception', $e->getMessage() );1847 }1848 1905 try { 1849 1906 $exists = $client->headObject( -
s3-image-optimizer/trunk/changelog.txt
r2969432 r3129664 1 2 3 4 5 6 7 1 8 = 2.5.1 = 2 9 * changed: use updated WP coding standards -
s3-image-optimizer/trunk/phpcs.ruleset.xml
r2969430 r3129664 21 21 <rule ref="WordPress.PHP.NoSilencedErrors"> 22 22 <properties> 23 <property name="custom _whitelist" type="array" value="ini_get,exif_read_data,ob_end_flush"/>23 <property name="customist" type="array" value="ini_get,exif_read_data,ob_end_flush"/> 24 24 </properties> 25 25 </rule> 26 26 <rule ref="WordPress.NamingConventions.ValidVariableName"> 27 27 <properties> 28 <property name="customPropertiesWhitelist" type="array" value="formatOutput,imgQuality,parentNode,nextSibling,documentElement,imagePath,webimagePath,thumbPath,fileName,Version,Name" /> 29 </properties> 30 <properties> 31 <property name="customVariablesWhitelist" type="array" value="formatOutput,parentNode,nextSibling,documentElement" /> 28 <property name="allowed_custom_properties" type="array" value="formatOutput,imgQuality,parentNode,nextSibling,documentElement,imagePath,webimagePath,thumbPath,fileName,Version,Name" /> 32 29 </properties> 33 30 </rule> 34 31 35 <config name="testVersion"> 7.3-</config>32 <config name="testVersion">-</config> 36 33 <rule ref="PHPCompatibilityWP"> 37 34 <severity>10</severity> -
s3-image-optimizer/trunk/readme.txt
r2969432 r3129664 1 1 === S3 Image Optimizer === 2 2 Contributors: nosilver4u 3 Tags: amazon, s3, image, optimize, optimization, photo, picture, seo, compression, wp-cli4 Requires at least: 6. 15 Tested up to: 6. 36 Requires PHP: 7.37 Stable tag: 2. 5.13 Tags: , compression, wp-cli 4 Requires at least: 6. 5 Tested up to: 6. 6 Requires PHP: 7 Stable tag: 2. 8 8 License: GPLv3 9 9 … … 40 40 ` 41 41 42 43 44 45 46 47 48 49 50 51 52 42 53 [View the full list of region names.](https://docs.aws.amazon.com/general/latest/gr/rande.html#regional-endpoints) 43 54 … … 62 73 63 74 == Changelog == 75 76 77 78 79 80 81 64 82 65 83 = 2.5.1 = -
s3-image-optimizer/trunk/s3-image-optimizer.php
r2969432 r3129664 12 12 Description: Reduce file sizes for images in S3 buckets using lossless and lossy optimization methods via the EWWW Image Optimizer. 13 13 Author: Exactly WWW 14 Version: 2.5.1 15 Requires at least: 6.1 16 Requires PHP: 7.3 14 Version: 2.6.0 15 Requires at least: 6.3 16 Requires PHP: 8.1 17 Requires Plugins: ewww-image-optimizer 17 18 Author URI: https://ewww.io/ 18 19 License: GPLv3 … … 22 23 * Constants 23 24 */ 24 define( 'S3IO_VERSION', 2 51);25 define( 'S3IO_VERSION', 2 ); 25 26 // This is the full path of the plugin file itself. 26 27 define( 'S3IO_PLUGIN_FILE', __FILE__ ); … … 75 76 add_action( 'admin_notices', 's3io_missing_ewww_plugin' ); 76 77 } 77 if ( ! function_exists( 'ewwwio' ) || ! function_exists( 'ewww_image_optimizer_filesize') ) {78 if ( ) ) { 78 79 add_action( 'network_admin_notices', 's3io_ewww_plugin_outdated' ); 79 80 add_action( 'admin_notices', 's3io_ewww_plugin_outdated' ); … … 194 195 if ( defined( 'S3_IMAGE_OPTIMIZER_ENDPOINT' ) && S3_IMAGE_OPTIMIZER_ENDPOINT ) { 195 196 $args['endpoint'] = S3_IMAGE_OPTIMIZER_ENDPOINT; 197 198 199 196 200 } 197 201 return $args; … … 636 640 if ( empty( $bucket_list ) ) { 637 641 global $s3io_amazon_web_services; 642 638 643 try { 639 644 $client = $s3io_amazon_web_services->get_client(); 640 645 } catch ( Exception $e ) { 641 646 $s3io_errors[] = $e->getMessage(); 642 }643 $bucket_list = array();647 648 644 649 try { 645 650 $buckets = $client->listBuckets(); … … 699 704 $bucket_list = s3io_get_selected_buckets(); 700 705 if ( ! empty( $s3io_errors ) ) { 701 /* translators: %s: AWS error message */702 706 if ( $wpcli ) { 703 707 return 0; … … 721 725 } 722 726 try { 723 $location = $client->getBucketLocation( 724 array( 725 'Bucket' => $bucket, 726 ) 727 ); 727 $location = $client->getBucketLocation( array( 'Bucket' => $bucket ) ); 728 728 } catch ( Exception $e ) { 729 729 $location = new WP_Error( 'exception', $e->getMessage() ); … … 1134 1134 $alternate = true; 1135 1135 foreach ( $already_optimized as $optimized_image ) { 1136 $file_size = size_format( $optimized_image['image_size'], 2);1136 $file_size = size_format( $optimized_image['image_size'], ); 1137 1137 $file_size = str_replace( '.00 B ', ' B', $file_size ); 1138 1138 ?> … … 1143 1143 <?php 1144 1144 /* translators: %s: size of image, in bytes */ 1145 echo esc_html( $optimized_image['results'] ) . ' <br>' . sprintf( esc_html__( 'Image Size: %s', 's3-image-optimizer' ), (int) $file_size);1145 echo esc_html( $optimized_image['results'] ) . ' <br>' . sprintf( esc_html__( 'Image Size: %s', 's3-image-optimizer' ), ); 1146 1146 ?> 1147 1147 <br><a class="removeimage" onclick="s3ioRemoveImage( <?php echo (int) $optimized_image['id']; ?> )"><?php esc_html_e( 'Remove from table', 's3-image-optimizer' ); ?></a> … … 1472 1472 die(); 1473 1473 } 1474 1475 1476 1474 1477 $new_size = ewww_image_optimizer_filesize( $filename ); 1475 1478 if ( $new_size < $fetch_result['ContentLength'] ) { … … 1485 1488 'Key' => $image_record['path'], 1486 1489 'SourceFile' => $filename, 1487 'ACL' => 'public-read',1488 1490 'ContentType' => $fetch_result['ContentType'], 1489 1491 'CacheControl' => 'max-age=31536000', … … 1491 1493 ) 1492 1494 ); 1495 1496 1497 1498 1499 1500 1501 1502 1503 1493 1504 } catch ( Exception $e ) { 1494 1505 if ( defined( 'WP_CLI' ) && WP_CLI ) { … … 1519 1530 'Key' => $image_record['path'] . '.webp', 1520 1531 'SourceFile' => $filename . '.webp', 1521 'ACL' => 'public-read',1522 1532 'ContentType' => 'image/webp', 1523 1533 'CacheControl' => 'max-age=31536000', … … 1525 1535 ) 1526 1536 ); 1537 1538 1539 1540 1541 1542 1543 1544 1545 1527 1546 } catch ( Exception $e ) { 1528 1547 if ( defined( 'WP_CLI' ) && WP_CLI ) { … … 1605 1624 function s3io_url_loop() { 1606 1625 s3io_debug_message( '<b>' . __FUNCTION__ . '()</b>' ); 1607 $output = array();1608 1626 // Verify that an authorized user has started the optimizer. 1609 1627 $permissions = apply_filters( 'ewww_image_optimizer_bulk_permissions', '' ); … … 1611 1629 die( wp_json_encode( array( 'error' => esc_html__( 'Access token has expired, please reload the page.', 's3-image-optimizer' ) ) ) ); 1612 1630 } 1613 $started = microtime( true ); 1631 global $s3io_errors; 1632 $s3io_errors = array(); 1633 $output = array(); 1634 $started = microtime( true ); 1614 1635 if ( 1615 1636 function_exists( 'ewww_image_optimizer_stl_check' ) && … … 1622 1643 if ( empty( $_REQUEST['s3io_url'] ) ) { 1623 1644 $output['error'] = esc_html__( 'No URL supplied', 's3-image-optimizer' ); 1624 echo wp_json_encode( $output ); 1625 die(); 1645 wp_die( wp_json_encode( $output ) ); 1626 1646 } 1627 1647 $url = esc_url_raw( wp_unslash( $_REQUEST['s3io_url'] ) ); … … 1629 1649 $url_args = s3io_get_args_from_url( $url ); 1630 1650 } 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1631 1664 if ( empty( $url ) || empty( $url_args ) ) { 1632 1665 $output['error'] = esc_html__( 'Invalid URL supplied', 's3-image-optimizer' ); 1633 echo wp_json_encode( $output ); 1634 die(); 1666 wp_die( wp_json_encode( $output ) ); 1635 1667 } 1636 1668 $url_args['path'] = ltrim( $url_args['path'], '/' ); … … 1638 1670 $upload_dir = s3io_make_upload_dir(); 1639 1671 if ( ! $upload_dir ) { 1640 die(1672 die( 1641 1673 wp_json_encode( 1642 1674 array( … … 1649 1681 s3io_debug_message( "stashing files in $upload_dir" ); 1650 1682 if ( false !== strpos( $upload_dir, 's3://' ) ) { 1651 /* translators: %s: path to uploads directory */ 1652 die( wp_json_encode( array( 'error' => sprintf( esc_html__( 'Received an unusable working directory: %s', 's3-image-optimizer' ), $upload_dir ) ) ) ); 1683 wp_die( 1684 wp_json_encode( 1685 array( 1686 /* translators: %s: path to uploads directory */ 1687 'error' => sprintf( esc_html__( 'Received an unusable working directory: %s', 's3-image-optimizer' ), $upload_dir ), 1688 ) 1689 ) 1690 ); 1653 1691 } 1654 1692 global $s3io_amazon_web_services; … … 1656 1694 $client = $s3io_amazon_web_services->get_client(); 1657 1695 } catch ( Exception $e ) { 1658 die( wp_json_encode( array( 'error' => wp_kses_post( $e->getMessage() ) ) ) ); 1659 } 1660 try { 1661 $location = $client->getBucketLocation( 1662 array( 1663 'Bucket' => $url_args['bucket'], 1696 wp_die( 1697 wp_json_encode( 1698 array( 1699 'error' => sprintf( 1700 /* translators: %s: AWS/S3 error message */ 1701 esc_html__( 'Error connecting to S3: %s', 's3-image-optimizer' ), 1702 wp_kses_post( $e->getMessage() ) 1703 ), 1704 ) 1664 1705 ) 1665 1706 ); 1666 } catch ( Exception $e ) {1667 $location = new WP_Error( 'exception', $e->getMessage() );1668 1707 } 1669 1708 $filename = $upload_dir . $url_args['path']; … … 1691 1730 die( wp_json_encode( $output ) ); 1692 1731 } 1732 1733 1734 1693 1735 $new_size = filesize( $filename ); 1694 1736 if ( $new_size < $fetch_result['ContentLength'] ) { 1695 1737 // Re-upload to S3. 1696 $client->putObject( 1697 array( 1698 'Bucket' => $url_args['bucket'], 1699 'Key' => $url_args['path'], 1700 'SourceFile' => $filename, 1701 'ACL' => 'public-read', 1702 'ContentType' => $fetch_result['ContentType'], 1703 'CacheControl' => 'max-age=31536000', 1704 'Expires' => gmdate( 'D, d M Y H:i:s O', time() + 31536000 ), 1705 ) 1706 ); 1738 try { 1739 $client->putObject( 1740 array( 1741 'Bucket' => $url_args['bucket'], 1742 'Key' => $url_args['path'], 1743 'SourceFile' => $filename, 1744 'ContentType' => $fetch_result['ContentType'], 1745 'CacheControl' => 'max-age=31536000', 1746 'Expires' => gmdate( 'D, d M Y H:i:s O', time() + 31536000 ), 1747 ) 1748 ); 1749 if ( ! $ownership_control_enforced ) { 1750 $client->putObjectAcl( 1751 array( 1752 'Bucket' => $url_args['bucket'], 1753 'Key' => $url_args['path'], 1754 'ACL' => 'public-read', 1755 ) 1756 ); 1757 } 1758 } catch ( Exception $e ) { 1759 $output['error'] = wp_kses_post( "Put failed for bucket: {$url_args['bucket']}, path: {$url_args['path']}, message:" . $e->getMessage() ); 1760 wp_die( wp_json_encode( $output ) ); 1761 } 1707 1762 } 1708 1763 unlink( $filename ); 1709 1764 $webp_size = ewww_image_optimizer_filesize( $filename . '.webp' ); 1710 1765 if ( $webp_size ) { 1711 if ( $verbose && defined( 'WP_CLI' ) && WP_CLI ) {1712 WP_CLI::line( "About to upload $filename.webp" );1713 }1714 1766 // Upload to S3. 1715 1767 try { … … 1719 1771 'Key' => $url_args['path'] . '.webp', 1720 1772 'SourceFile' => $filename . '.webp', 1721 'ACL' => 'public-read',1722 1773 'ContentType' => 'image/webp', 1723 1774 'CacheControl' => 'max-age=31536000', … … 1725 1776 ) 1726 1777 ); 1778 1779 1780 1781 1782 1783 1784 1785 1786 1727 1787 } catch ( Exception $e ) { 1728 if ( defined( 'WP_CLI' ) && WP_CLI ) { 1729 WP_CLI::error( "Put failed for bucket: {$image_record['bucket']}, path: {$image_record['path']}.webp, message:" . $e->getMessage() ); 1730 } else { 1731 $output['error'] = wp_kses_post( "Put failed for bucket: {$image_record['bucket']}, path: {$image_record['path']}.webp, message:" . $e->getMessage() ); 1732 echo wp_json_encode( $output ); 1733 } 1734 die(); 1735 } 1736 if ( $verbose && defined( 'WP_CLI' ) && WP_CLI ) { 1737 WP_CLI::line( "Finished upload of $filename.webp" ); 1788 $output['error'] = wp_kses_post( "Put failed for bucket: {$url_args['bucket']}, path: {$url_args['path']}.webp, message:" . $e->getMessage() ); 1789 wp_die( wp_json_encode( $output ) ); 1738 1790 } 1739 1791 unlink( $filename . '.webp' ); … … 1753 1805 1754 1806 die( wp_json_encode( $output ) ); 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1755 1834 } 1756 1835 … … 1786 1865 } 1787 1866 } 1788 global $s3io_amazon_web_services; 1789 try { 1790 $client = $s3io_amazon_web_services->get_client(); 1791 } catch ( Exception $e ) { 1792 s3io_debug_message( 'unable to initialize AWS client lib' ); 1793 return false; 1794 } 1795 try { 1796 $buckets = $client->listBuckets(); 1797 } catch ( Exception $e ) { 1798 $buckets = new WP_Error( 'exception', $e->getMessage() ); 1799 } 1800 1801 // If retrieving buckets from AWS failed, then we use the bucketlist option. 1802 if ( is_wp_error( $buckets ) ) { 1803 $bucket_list = get_option( 's3io_bucketlist' ); 1804 } else { 1805 $bucket_list = array(); 1806 foreach ( $buckets['Buckets'] as $aws_bucket ) { 1807 $bucket_list[] = $aws_bucket['Name']; 1808 } 1809 } 1867 1868 $bucket_list = s3io_get_selected_buckets(); 1810 1869 1811 1870 // If we don't have a list of buckets, we can't do much more here. … … 1835 1894 // Otherwise, we must have a custom domain, so lets do a quick search for the attachment in all buckets. 1836 1895 // Doing it in a separate foreach, in case there are performance implications of switching the region in accounts with lots of buckets. 1896 1897 1898 1899 1900 1901 1902 1837 1903 $key = ltrim( $urlinfo['path'], '/' ); 1838 1904 foreach ( $bucket_list as $aws_bucket ) { 1839 try {1840 $location = $client->getBucketLocation(1841 array(1842 'Bucket' => $aws_bucket,1843 )1844 );1845 } catch ( Exception $e ) {1846 $location = new WP_Error( 'exception', $e->getMessage() );1847 }1848 1905 try { 1849 1906 $exists = $client->headObject(
Note: See TracChangeset
for help on using the changeset viewer.