Plugin Directory

Changeset 3238162


Ignore:
Timestamp:
02/10/2025 07:57:52 PM (14 months ago)
Author:
artilab
Message:

Added support custom data-* attributes 1.3.0

Location:
picture-tag
Files:
7 edited
8 copied

Legend:

Unmodified
Added
Removed
  • picture-tag/tags/1.3.0/README.md

    r3212753 r3238162  
    11# Picture Tag
    22
    3 **Version:** 1.2.0 
     3**Version:** 1.3.0 
    44**Author:** Artilab
    55
     
    91918. fetchpriority (optional):
    9292Specifies the priority for resource fetching. Common values: high, low.
     939. data-* (optional):
     94Supports custom data attributes for the `<img>` tag, allowing developers to pass additional metadata.
    9395
    9496**Examples:**
  • picture-tag/tags/1.3.0/includes/shortcode.php

    r3209252 r3238162  
    1919 */
    2020function arti_picture_tag_shortcode_handler( $atts ) {
    21   // Define default attributes
    22   $atts = shortcode_atts( array(
    23       'id'       => 0,           // Attachment ID
    24       'size_1x'  => 'large',     // Default size for 1x
    25       'size_2x'  => 'full',      // Default size for 2x
    26       'alt'      => '',          // Alt attribute
    27       'title'    => '',          // Title attribute
    28       'class'    => '',          // Additional classes
    29       'loading'  => 'lazy',      // Loading attribute
    30       'fetchpriority' => 'low',     // Fetch priority (e.g., "high")
    31   ), $atts, 'arti_picture' );
     21    // Define default attributes
     22    $default_atts = array(
     23        'id'       => 0,           // Attachment ID
     24        'size_1x'  => 'large',     // Default size for 1x
     25        'size_2x'  => 'full',      // Default size for 2x
     26        'alt'      => '',          // Alt attribute
     27        'title'    => '',          // Title attribute
     28        'class'    => '',          // Additional classes
     29        'loading'  => 'lazy',      // Loading attribute
     30        'fetchpriority' => 'low',  // Fetch priority (e.g., "high")
     31    );
    3232
    33   // Validate and sanitize attributes
    34   $image_id = intval( $atts['id'] );
    35   $sizes    = array(
    36       '1x' => sanitize_text_field( $atts['size_1x'] ),
    37       '2x' => sanitize_text_field( $atts['size_2x'] ),
    38   );
     33    // get custom attributes (data-*)
     34    $custom_data_atts = array();
     35    foreach ( $atts as $key => $value ) {
     36        if ( strpos( $key, 'data-' ) === 0 ) {
     37            $custom_data_atts[ $key ] = esc_attr( $value );
     38        }
     39    }
    3940
    40   // Prepare additional attributes for the <img> tag
    41   $img_attr = array_filter( array(
    42       'alt'           => sanitize_text_field( $atts['alt'] ),
    43       'title'         => sanitize_text_field( $atts['title'] ),
    44       'class'         => sanitize_text_field( $atts['class'] ),
    45       'loading'       => sanitize_text_field( $atts['loading'] ),
    46       'fetchpriority' => sanitize_text_field( $atts['fetchpriority'] ),
    47   ) );
     41    // Merge user attributes with defaults
     42    $atts = shortcode_atts( $default_atts, $atts, 'arti_picture' );
    4843
    49   // Return early if no image ID is provided
    50   if ( ! $image_id ) {
    51       return '<!-- No image ID provided -->';
    52   }
     44    // Merge custom user attributes
     45    $atts = array_merge( $atts, $custom_data_atts );
    5346
    54   // Generate the <picture> tag using the provided sizes and attributes
    55   return arti_get_image_by_id( $image_id, $sizes, $img_attr );
     47   
     48    // Validate and sanitize attributes
     49    $image_id = intval( $atts['id'] );
     50    $sizes    = array(
     51        '1x' => sanitize_text_field( $atts['size_1x'] ),
     52        '2x' => sanitize_text_field( $atts['size_2x'] ),
     53    );
     54
     55    // Extract known attributes and prepare additional attributes
     56    $img_attr = array_filter( array(
     57        'alt'           => sanitize_text_field( $atts['alt'] ),
     58        'title'         => sanitize_text_field( $atts['title'] ),
     59        'class'         => sanitize_text_field( $atts['class'] ),
     60        'loading'       => sanitize_text_field( $atts['loading'] ),
     61        'fetchpriority' => sanitize_text_field( $atts['fetchpriority'] ),
     62    ) );
     63
     64    // Extract custom attributes (data-*)
     65    foreach ( $atts as $key => $value ) {
     66        if ( strpos( $key, 'data-' ) === 0 ) {
     67            $img_attr[ $key ] = esc_attr( $value );
     68        }
     69    }
     70
     71    // Return early if no image ID is provided
     72    if ( ! $image_id ) {
     73        return '<!-- No image ID provided -->';
     74    }
     75
     76    // Generate the <picture> tag using the provided sizes and attributes
     77    return arti_get_image_by_id( $image_id, $sizes, $img_attr );
    5678}
  • picture-tag/tags/1.3.0/includes/template-functions.php

    r3209252 r3238162  
    116116        'picture' => array(),
    117117    );
     118   
     119    // Allow custom data-* attributes
     120    foreach ( $attr as $key => $value ) {
     121        if ( strpos( $key, 'data-' ) === 0 ) {
     122            $allowed_tags['img'][$key] = true;
     123        }
     124    }
    118125
    119126    echo wp_kses( $html, $allowed_tags );
  • picture-tag/tags/1.3.0/picture-tag.php

    r3212753 r3238162  
    33 * Plugin Name: Picture Tag
    44 * Description: Picture Tag with Custom Path Settings
    5  * Version: 1.2.0
     5 * Version: 1.3.0
    66 * Author: Artilab
    77 * Author URI: https://artilab.pro/
     
    1818define( 'ARTI_PICTURE_TAG_PLUGIN', __FILE__ );
    1919define( 'ARTI_PICTURE_TAG_PLUGIN_DIR', untrailingslashit( dirname( ARTI_PICTURE_TAG_PLUGIN ) ) );
    20 define( 'ARTI_PICTURE_TAG_PLUGIN_VERSION', '1.2.0' );
     20define( 'ARTI_PICTURE_TAG_PLUGIN_VERSION', '1.3.0' );
    2121
    2222
  • picture-tag/tags/1.3.0/readme.txt

    r3212753 r3238162  
    55Tested up to:      6.7
    66Requires PHP:      7.4
    7 Stable tag:        1.2.0
     7Stable tag:        1.3.0
    88License:           GPL-2.0-or-later
    99License URI:       https://www.gnu.org/licenses/gpl-2.0.html
     
    4949== Changelog ==
    5050
     51= 1.3.0 =
     52* Added support for custom data-* attributes in ```[arti_picture]``` shortcode.
     53* Added support for custom data-* attributes in the ```arti_the_image``` function.
     54
    5155= 1.2.0 =
    5256* Added translation support for multilingual use.
  • picture-tag/trunk/README.md

    r3212753 r3238162  
    11# Picture Tag
    22
    3 **Version:** 1.2.0 
     3**Version:** 1.3.0 
    44**Author:** Artilab
    55
     
    91918. fetchpriority (optional):
    9292Specifies the priority for resource fetching. Common values: high, low.
     939. data-* (optional):
     94Supports custom data attributes for the `<img>` tag, allowing developers to pass additional metadata.
    9395
    9496**Examples:**
  • picture-tag/trunk/includes/shortcode.php

    r3209252 r3238162  
    1919 */
    2020function arti_picture_tag_shortcode_handler( $atts ) {
    21   // Define default attributes
    22   $atts = shortcode_atts( array(
    23       'id'       => 0,           // Attachment ID
    24       'size_1x'  => 'large',     // Default size for 1x
    25       'size_2x'  => 'full',      // Default size for 2x
    26       'alt'      => '',          // Alt attribute
    27       'title'    => '',          // Title attribute
    28       'class'    => '',          // Additional classes
    29       'loading'  => 'lazy',      // Loading attribute
    30       'fetchpriority' => 'low',     // Fetch priority (e.g., "high")
    31   ), $atts, 'arti_picture' );
     21    // Define default attributes
     22    $default_atts = array(
     23        'id'       => 0,           // Attachment ID
     24        'size_1x'  => 'large',     // Default size for 1x
     25        'size_2x'  => 'full',      // Default size for 2x
     26        'alt'      => '',          // Alt attribute
     27        'title'    => '',          // Title attribute
     28        'class'    => '',          // Additional classes
     29        'loading'  => 'lazy',      // Loading attribute
     30        'fetchpriority' => 'low',  // Fetch priority (e.g., "high")
     31    );
    3232
    33   // Validate and sanitize attributes
    34   $image_id = intval( $atts['id'] );
    35   $sizes    = array(
    36       '1x' => sanitize_text_field( $atts['size_1x'] ),
    37       '2x' => sanitize_text_field( $atts['size_2x'] ),
    38   );
     33    // get custom attributes (data-*)
     34    $custom_data_atts = array();
     35    foreach ( $atts as $key => $value ) {
     36        if ( strpos( $key, 'data-' ) === 0 ) {
     37            $custom_data_atts[ $key ] = esc_attr( $value );
     38        }
     39    }
    3940
    40   // Prepare additional attributes for the <img> tag
    41   $img_attr = array_filter( array(
    42       'alt'           => sanitize_text_field( $atts['alt'] ),
    43       'title'         => sanitize_text_field( $atts['title'] ),
    44       'class'         => sanitize_text_field( $atts['class'] ),
    45       'loading'       => sanitize_text_field( $atts['loading'] ),
    46       'fetchpriority' => sanitize_text_field( $atts['fetchpriority'] ),
    47   ) );
     41    // Merge user attributes with defaults
     42    $atts = shortcode_atts( $default_atts, $atts, 'arti_picture' );
    4843
    49   // Return early if no image ID is provided
    50   if ( ! $image_id ) {
    51       return '<!-- No image ID provided -->';
    52   }
     44    // Merge custom user attributes
     45    $atts = array_merge( $atts, $custom_data_atts );
    5346
    54   // Generate the <picture> tag using the provided sizes and attributes
    55   return arti_get_image_by_id( $image_id, $sizes, $img_attr );
     47   
     48    // Validate and sanitize attributes
     49    $image_id = intval( $atts['id'] );
     50    $sizes    = array(
     51        '1x' => sanitize_text_field( $atts['size_1x'] ),
     52        '2x' => sanitize_text_field( $atts['size_2x'] ),
     53    );
     54
     55    // Extract known attributes and prepare additional attributes
     56    $img_attr = array_filter( array(
     57        'alt'           => sanitize_text_field( $atts['alt'] ),
     58        'title'         => sanitize_text_field( $atts['title'] ),
     59        'class'         => sanitize_text_field( $atts['class'] ),
     60        'loading'       => sanitize_text_field( $atts['loading'] ),
     61        'fetchpriority' => sanitize_text_field( $atts['fetchpriority'] ),
     62    ) );
     63
     64    // Extract custom attributes (data-*)
     65    foreach ( $atts as $key => $value ) {
     66        if ( strpos( $key, 'data-' ) === 0 ) {
     67            $img_attr[ $key ] = esc_attr( $value );
     68        }
     69    }
     70
     71    // Return early if no image ID is provided
     72    if ( ! $image_id ) {
     73        return '<!-- No image ID provided -->';
     74    }
     75
     76    // Generate the <picture> tag using the provided sizes and attributes
     77    return arti_get_image_by_id( $image_id, $sizes, $img_attr );
    5678}
  • picture-tag/trunk/includes/template-functions.php

    r3209252 r3238162  
    116116        'picture' => array(),
    117117    );
     118   
     119    // Allow custom data-* attributes
     120    foreach ( $attr as $key => $value ) {
     121        if ( strpos( $key, 'data-' ) === 0 ) {
     122            $allowed_tags['img'][$key] = true;
     123        }
     124    }
    118125
    119126    echo wp_kses( $html, $allowed_tags );
  • picture-tag/trunk/picture-tag.php

    r3212753 r3238162  
    33 * Plugin Name: Picture Tag
    44 * Description: Picture Tag with Custom Path Settings
    5  * Version: 1.2.0
     5 * Version: 1.3.0
    66 * Author: Artilab
    77 * Author URI: https://artilab.pro/
     
    1818define( 'ARTI_PICTURE_TAG_PLUGIN', __FILE__ );
    1919define( 'ARTI_PICTURE_TAG_PLUGIN_DIR', untrailingslashit( dirname( ARTI_PICTURE_TAG_PLUGIN ) ) );
    20 define( 'ARTI_PICTURE_TAG_PLUGIN_VERSION', '1.2.0' );
     20define( 'ARTI_PICTURE_TAG_PLUGIN_VERSION', '1.3.0' );
    2121
    2222
  • picture-tag/trunk/readme.txt

    r3212753 r3238162  
    55Tested up to:      6.7
    66Requires PHP:      7.4
    7 Stable tag:        1.2.0
     7Stable tag:        1.3.0
    88License:           GPL-2.0-or-later
    99License URI:       https://www.gnu.org/licenses/gpl-2.0.html
     
    4949== Changelog ==
    5050
     51= 1.3.0 =
     52* Added support for custom data-* attributes in ```[arti_picture]``` shortcode.
     53* Added support for custom data-* attributes in the ```arti_the_image``` function.
     54
    5155= 1.2.0 =
    5256* Added translation support for multilingual use.
Note: See TracChangeset for help on using the changeset viewer.