From 637eae97870518818cd0dfbf696b3bbb2941d41f Mon Sep 17 00:00:00 2001
From: Otto Rautamaa
Date: Sun, 19 Feb 2017 19:23:28 +0200
Subject: [PATCH 1/2] add image helper to the core
---
helpers/image.php | 304 ++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 304 insertions(+)
create mode 100644 helpers/image.php
diff --git a/helpers/image.php b/helpers/image.php
new file mode 100644
index 00000000..99e10269
--- /dev/null
+++ b/helpers/image.php
@@ -0,0 +1,304 @@
+get_image_data( $this->params );
+
+ // ID given
+ if ( null !== $image_data['id'] ) {
+
+ // SRC also given.
+ if ( null !== $image_data['src'] ) {
+
+ return 'Dustpress image helper error:
+ Image id and custom src both given.
+ Only one of these parameters can be used.
';
+
+ } else { // Only the ID given as the original image source.
+
+ // No custom responsive parameters given
+ if ( null === $image_data['srcset'] && null === $image_data['sizes'] ) {
+
+ // Return the WordPress default img-tag
+ // from the full-sized image with a source set.
+ $the_image_markup = wp_get_attachment_image(
+ $image_data['id'],
+ 'full',
+ false,
+ $image_data['attrs']
+ );
+
+ if ( $the_image_markup ) {
+
+ return $the_image_markup;
+
+ } else {
+
+ return 'Dustpress image helper error:
+ No image found from the database with the given id.
';
+
+ }
+ } else { // Custom responsive parameters are given.
+
+ // SRCSET exists but no SIZES attribute is given.
+ if ( null === $image_data['sizes'] ) {
+
+ return 'Dustpress image helper error:
+ Srcset exists but no sizes attribute is given.
';
+
+ } else { // Both custom responsive parameters and the id is given.
+
+ return $this->get_image_markup( $image_data );
+
+ }
+ }
+ }
+ } else { // No image ID given
+
+ // No SRC given either.
+ if ( null === $image_data['src'] ) {
+
+ return 'Dustpress image helper error:
+ Image id or custom src not given.
+ The helper needs at least one of these parameters.
';
+
+ } else { // Only the SRC given as the original image source.
+
+ // When using the custom SRC, both SRCSET and SIZES need to be given.
+ if ( null === $image_data['srcset'] ) {
+
+ return 'Dustpress image helper error:
+ Srcset not given. Both the srcset and the sizes are
+ needed when using a custom src.
';
+ }
+
+ // When using the custom SRC, both SRCSET and SIZES need to be given.
+ if ( null === $image_data['sizes'] ) {
+
+ return 'Dustpress image helper error:
+ Sizes not given. Both the srcset and the sizes are
+ needed when using a custom src.
';
+
+ }
+
+ return $this->get_image_markup( $image_data );
+ }
+ }
+ }
+
+ /**
+ * Gets and formats the data from the parameters given
+ * to the image helper tag.
+ *
+ * @param array $params The array object.
+ *
+ * @return array $image_data The formatted array.
+ */
+ private function get_image_data( $params ) {
+
+ // Init the settings array and the img attrs array.
+ $image_data = [
+ 'id' => null,
+ 'src' => null,
+ 'srcset' => null,
+ 'sizes' => null,
+ 'attrs' => [],
+ ];
+
+ // Store the images ID if it is given.
+ if ( isset( $params->id ) ) {
+ $image_data['id'] = (int) $params->id;
+ }
+
+ // Add the src attribute to the data array if it is given.
+ if ( isset( $params->src ) ) {
+ $image_data['src'] = $params->src;
+ }
+
+ // Add the srcset attribute to the data array if it is given.
+ if ( isset( $params->srcset ) ) {
+ $image_data['srcset'] = $params->srcset;
+ }
+
+ // Add the sizes attribute to the data array if it is given.
+ if ( isset( $params->sizes ) ) {
+ $image_data['sizes'] = $params->sizes;
+ }
+
+ // If a class string is given, store it to the meta params.
+ if ( isset( $params->class ) ) {
+ $image_data['attrs']['class'] = $params->class;
+ }
+
+ // If an alt string is given, store it to the meta params.
+ if ( isset( $params->alt ) ) {
+ $image_data['attrs']['alt'] = $params->alt;
+ }
+
+ return $image_data;
+ }
+
+ /**
+ * Get the custom HTML srcset markup with the given settings
+ *
+ * @param array $image_data The given srcset and sizes.
+ *
+ * @return string The image markup.
+ */
+ private function get_image_markup( $image_data ) {
+
+ // If the src attribute is given, use it as the original src.
+ if ( null !== $image_data['src'] ) {
+
+ $image_src_string = 'Dustpress image helper error:
+ No image found from the database with the given id.
';
+
+ }
+ }
+
+ // Set the class string.
+ $image_class_string = ( isset( $image_data['attrs']['class'] )
+ ? 'class="'. $image_data['attrs']['class'] .'"'
+ : ''
+ );
+
+ // Set the alt string.
+ $image_alt_string = ( isset( $image_data['attrs']['alt'] )
+ ? 'alt="'. $image_data['attrs']['alt'] .'"'
+ : ''
+ );
+
+ // Set the sizes attribute string.
+ $sizes = $image_data['sizes'];
+
+ // Check that the srcset is given as an array.
+ if ( ! is_array( $sizes ) ) {
+
+ return 'Dustpress image helper error:
+ Given sizes attribute is not an array.
';
+
+ }
+
+ // Concatenate the given sizes to a comma separated list
+ // and construct the sizes string.
+ $image_sizes_string = 'sizes="' . implode( ', ', $sizes ) .'"';
+
+ // Either use the srcset array that is given
+ // or fetch the urls and widths using the WP sizes.
+ $srcset_array = ( isset( $image_data['srcset'] )
+ ? $image_data['srcset']
+ : $this->get_wp_image_sizes_array( $image_data['id'] )
+ );
+
+ // Check that the srcset is given as an array.
+ if ( ! is_array( $srcset_array ) ) {
+
+ return 'Dustpress image helper error:
+ Given srcset attribute is not an array.
';
+
+ }
+
+ // Construct the srcset string.
+ $image_srcset_string = 'srcset="' . implode( ', ', $srcset_array ) .'"';
+
+ // Close the img tag.
+ $image_close_string = '>';
+
+ // Concatenate all of the images strings together.
+ $html = $image_src_string .
+ $image_alt_string .
+ $image_class_string .
+ $image_srcset_string .
+ $image_sizes_string .
+ $image_close_string;
+
+ return $html;
+ }
+
+ /**
+ * Get all the registered image sizes along with their dimensions
+ *
+ * @global array $_wp_additional_image_sizes
+ * @param int $id The image ID.
+ *
+ * @return array $image_sizes The image sizes
+ */
+ private function get_wp_image_sizes_array( $id ) {
+
+ // The registered image sizes.
+ global $_wp_additional_image_sizes;
+
+ // The default wordpress image sizes. Exclude the thumbnail size.
+ $default_image_sizes = array( 'medium', 'medium_large', 'large' );
+
+ // Loop through the sizes and get the corresponding options from the db.
+ foreach ( $default_image_sizes as $size ) {
+
+ $image_sizes[ $size ]['width'] = intval( get_option( "{$size}_size_w" ) );
+ $image_sizes[ $size ]['height'] = intval( get_option( "{$size}_size_h" ) );
+ $image_sizes[ $size ]['crop'] = ( get_option( "{$size}_crop" )
+ ? get_option( "{$size}_crop" )
+ : false
+ );
+ }
+
+ // Add custom sizes to the array.
+ if ( isset( $_wp_additional_image_sizes ) && count( $_wp_additional_image_sizes ) ) {
+
+ $image_sizes = array_merge( $image_sizes, $_wp_additional_image_sizes );
+
+ }
+
+ // The final array in which we have the properly formatted urls and widths.
+ $srcset_array = [];
+
+ // Loop through the sizes in the array and get the urls and widths from WP.
+ foreach ( $image_sizes as $size => $size_options ) {
+
+ $url = wp_get_attachment_image_src( $id, $size )[0];
+ $width = $size_options['width'];
+ $entry = $url . ' ' . $width . 'w';
+ $srcset_array[] = $entry;
+
+ }
+
+ return $srcset_array;
+ }
+}
+// Add the helper.
+$this->add_helper( 'image', new Image() );
From 1886dd753914b9e5db224106cd05f7b1e9e23daa Mon Sep 17 00:00:00 2001
From: Otto Rautamaa
Date: Sun, 19 Feb 2017 19:28:56 +0200
Subject: [PATCH 2/2] Change the file comment
---
helpers/image.php | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/helpers/image.php b/helpers/image.php
index 99e10269..89df4317 100644
--- a/helpers/image.php
+++ b/helpers/image.php
@@ -1,6 +1,6 @@