Plugin Directory

Changeset 3830437


Ignore:
Timestamp:
11/27/2026 08:34:35 PM (2 hours ago)
Author:
parorrey
Message:

Release PI Giveaways 1.0.13: winner photo picker, video reels, silent historical winners; tested with WordPress 7.1.5

Location:
pi-giveaways
Files:
4 added
10 edited
1 copied

Legend:

Unmodified
Added
Removed
  • pi-giveaways/tags/1.0.13/includes/class-pigi-admin.php

    r3761409 r3830437  
    285285                wp_enqueue_style( 'pigiveaways-admin-preview', PIGIVEAWAYS_URL . 'assets/admin-preview.css', array( 'pigiveaways-admin' ), PIGIVEAWAYS_VERSION );
    286286                wp_enqueue_script( 'pigiveaways-admin', PIGIVEAWAYS_URL . 'assets/admin.js', array(), PIGIVEAWAYS_VERSION, true );
     287                if ( $screen && 'pigiveaways_giveaway' === $screen->post_type && 'post' === $screen->base && current_user_can( 'upload_files' ) ) {
     288                        wp_enqueue_media();
     289                        wp_enqueue_script( 'pigiveaways-winner-photos', PIGIVEAWAYS_URL . 'assets/winner-photos.js', array( 'media-editor' ), PIGIVEAWAYS_VERSION, true );
     290                }
    287291                if ( false !== strpos( $hook, 'pigiveaways' ) ) {
    288292                        $settings = wp_parse_args( get_option( $this->option, array() ), $this->defaults() );
    … …  
    351355                add_meta_box( 'pig-details', __( 'Giveaway Details', 'pi-giveaways' ), array( $this, 'details_box' ), 'pigiveaways_giveaway', 'normal', 'high' );
    352356                add_meta_box( 'pig-form-builder', __( 'Giveaway Entry Form', 'pi-giveaways' ), array( $this, 'form_builder_box' ), 'pigiveaways_giveaway', 'normal', 'default' );
     357                add_meta_box( 'pig-winner-publication', __( 'Public Winners and Reviews', 'pi-giveaways' ), array( $this, 'winner_publication_box' ), 'pigiveaways_giveaway', 'normal', 'default' );
     358        }
     359
     360        public function winner_publication_box( $post ) {
     361                $publication = (array) get_post_meta( $post->ID, '_pigiveaways_winner_publication', true );
     362                echo '<input type="hidden" name="pig_winner_publication_present" value="1">';
     363                echo '<p><label><input type="checkbox" name="pig_historical_giveaway" value="1" ' . checked( ! empty( $publication['historical'] ), true, false ) . '> ' . esc_html__( 'Historical giveaway: record past winners silently', 'pi-giveaways' ) . '</label></p>';
     364                echo '<p>' . esc_html__( 'Set the giveaway status to Ended or Awarded before recording historical winners.', 'pi-giveaways' ) . '</p>';
     365                echo '<p><label><input type="checkbox" name="pig_publish_winners" value="1" ' . checked( ! empty( $publication['enabled'] ), true, false ) . '> ' . esc_html__( 'Automatically display recorded winners on the closed giveaway page', 'pi-giveaways' ) . '</label></p>';
     366                echo '<p>' . esc_html__( 'This setting publishes winner names, reviews, and photos on Ended and Awarded giveaways. Enable it only with the winners’ permission. Paste image URLs, one per line, or paste HTML image tags.', 'pi-giveaways' ) . '</p>';
     367                foreach ( PIGI_Repository::publication_winner_slots( $post->ID ) as $award ) {
     368                        $row = $publication['winners'][ $award->id ] ?? array();
     369                        $key = 'pig_public_winner[' . $award->id . ']';
     370                        $label = trim( $award->first_name . ' ' . $award->last_name );
     371                        echo '<fieldset><legend><strong>' . esc_html( $label ?: __( 'Historical winner', 'pi-giveaways' ) ) . '</strong></legend>';
     372                        echo '<p><label>' . esc_html__( 'Public display name (optional)', 'pi-giveaways' ) . '<br><input class="regular-text" name="' . esc_attr( $key . '[name]' ) . '" value="' . esc_attr( $row['name'] ?? '' ) . '"></label></p>';
     373                        echo '<p><label>' . esc_html__( 'Winner review', 'pi-giveaways' ) . '<br><textarea class="large-text" rows="4" name="' . esc_attr( $key . '[review]' ) . '">' . esc_textarea( $row['review'] ?? '' ) . '</textarea></label></p>';
     374                        echo '<div data-pig-winner-photos>';
     375                        if ( current_user_can( 'upload_files' ) ) {
     376                                echo '<p><button type="button" class="button" data-pig-choose-photos data-title="' . esc_attr__( 'Select or upload winner photos', 'pi-giveaways' ) . '" data-confirm="' . esc_attr__( 'Add photos', 'pi-giveaways' ) . '">' . esc_html__( 'Select or upload photos', 'pi-giveaways' ) . '</button></p>';
     377                        }
     378                        echo '<p><label>' . esc_html__( 'Photo URLs (remove a line to remove a photo)', 'pi-giveaways' ) . '<br><textarea class="large-text" rows="3" name="' . esc_attr( $key . '[photos]' ) . '">' . esc_textarea( implode( "\n", $row['photos'] ?? array() ) ) . '</textarea></label></p></div>';
     379                        echo '<p><label>' . esc_html__( 'Video or reel URLs (YouTube, Instagram or Facebook, one per line)', 'pi-giveaways' ) . '<br><textarea class="large-text" rows="3" name="' . esc_attr( $key . '[videos]' ) . '">' . esc_textarea( implode( "\n", $row['videos'] ?? array() ) ) . '</textarea></label></p></fieldset>';
     380                }
     381        }
     382
     383        private function save_winner_publication( $post_id ) {
     384                if ( ! isset( $_POST['pig_winner_publication_present'] ) ) {
     385                        return;
     386                }
     387                $submitted = (array) wp_unslash( $_POST['pig_public_winner'] ?? array() );
     388                $previous  = (array) get_post_meta( $post_id, '_pigiveaways_winner_publication', true );
     389                $winners   = (array) ( $previous['winners'] ?? array() );
     390                foreach ( PIGI_Repository::publication_winner_slots( $post_id ) as $award ) {
     391                        $row = $submitted[ $award->id ] ?? null;
     392                        if ( ! is_array( $row ) ) {
     393                                continue;
     394                        }
     395                        $winners[ $award->id ] = array(
     396                                'name'   => sanitize_text_field( $row['name'] ?? '' ),
     397                                'review' => sanitize_textarea_field( $row['review'] ?? '' ),
     398                                'photos' => $this->parse_winner_photos( $row['photos'] ?? '' ),
     399                                'videos' => PIGI_Repository::winner_video_urls( $row['videos'] ?? '' ),
     400                        );
     401                }
     402                update_post_meta( $post_id, '_pigiveaways_winner_publication', wp_slash( array(
     403                        'enabled' => isset( $_POST['pig_publish_winners'] ),
     404                        'historical' => isset( $_POST['pig_historical_giveaway'] ),
     405                        'winners' => $winners,
     406                ) ) );
     407        }
     408
     409        private function parse_winner_photos( $input ) {
     410                if ( ! is_string( $input ) ) {
     411                        return array();
     412                }
     413
     414                $candidates = array();
     415                $tags = new WP_HTML_Tag_Processor( $input );
     416                while ( $tags->next_tag( 'IMG' ) ) {
     417                        $src = $tags->get_attribute( 'src' );
     418                        if ( is_string( $src ) ) {
     419                                $candidates[] = $src;
     420                        }
     421                }
     422                foreach ( preg_split( '/\R/', $input ) as $line ) {
     423                        if ( preg_match( '~^(https?://|//|/)~i', trim( $line ) ) && false === strpos( $line, '<' ) ) {
     424                                $candidates[] = $line;
     425                        }
     426                }
     427
     428                $photos = array();
     429                foreach ( $candidates as $candidate ) {
     430                        $url = trim( $candidate );
     431                        if ( 0 === strpos( $url, '//' ) ) {
     432                                $url = 'https:' . $url;
     433                        } elseif ( 0 === strpos( $url, '/' ) ) {
     434                                $url = home_url( $url );
     435                        }
     436                        if ( ! preg_match( '~^https?://~i', $url ) ) {
     437                                continue;
     438                        }
     439                        $url = esc_url_raw( $url, array( 'http', 'https' ) );
     440                        if ( wp_parse_url( $url, PHP_URL_HOST ) ) {
     441                                $photos[] = $url;
     442                        }
     443                }
     444                return array_values( array_unique( $photos ) );
    353445        }
    354446
    … …  
    522614                update_post_meta( $post_id, '_pigiveaways_form_fields', $stored_fields );
    523615                update_post_meta( $post_id, '_pigiveaways_form_mode', $mode );
     616                $this->save_winner_publication( $post_id );
    524617        }
    525618
  • pi-giveaways/tags/1.0.13/includes/class-pigi-frontend.php

    r3751015 r3830437  
    241241                        echo '<p class="pig-related-event"><a href="' . esc_url( $d['event_url'] ) . '">' . esc_html__( 'View related event', 'pi-giveaways' ) . ' →</a></p>';
    242242                }
     243                $winners = '';
     244                if ( 'publish' === $post->post_status && ! post_password_required( $post ) && in_array( $state, array( 'ended', 'awarded' ), true ) ) {
     245                        $winners = $this->render_public_winners( $id );
     246                }
    243247                if ( 'active' === $state ) {
    244248                        $this->form( $id, $d, $settings );
    … …  
    246250                        echo '<p>' . esc_html__( 'This giveaway is coming soon.', 'pi-giveaways' ) . '</p>';
    247251                } elseif ( 'ended' === $state ) {
    248                         echo '<p>' . esc_html__( 'Entries are closed. Any award will be assigned manually by the promoter.', 'pi-giveaways' ) . '</p>';
     252                        echo '<p>' . esc_html( $winners ? __( 'Entries are closed.', 'pi-giveaways' ) : __( 'Entries are closed. Any award will be assigned manually by the promoter.', 'pi-giveaways' ) ) . '</p>';
    249253                } elseif ( 'awarded' === $state ) {
    250254                                $announcement = $d['announcement'] ? $d['announcement'] : __( 'The award has been announced.', 'pi-giveaways' );
    … …  
    255259                        echo '<p>' . esc_html__( 'This giveaway is not accepting entries.', 'pi-giveaways' ) . '</p>';
    256260                }
     261                // The winner section escapes its data when building the markup.
     262                echo $winners; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
    257263                echo '</div></section>';
    258264                return ob_get_clean();
     265        }
     266
     267        private function render_public_winners( $id ) {
     268                $publication = (array) get_post_meta( $id, '_pigiveaways_winner_publication', true );
     269                if ( empty( $publication['enabled'] ) ) {
     270                        return '';
     271                }
     272
     273                ob_start();
     274                foreach ( PIGI_Repository::publication_winner_slots( $id ) as $award ) {
     275                        $row     = $publication['winners'][ $award->id ] ?? array();
     276                        $initial = preg_match( '/^\p{L}/u', trim( (string) $award->last_name ), $match ) ? $match[0] . '.' : '';
     277                        $name    = $row['name'] ?? '';
     278                        if ( '' === $name && empty( $row['review'] ) && empty( $row['photos'] ) && empty( $row['videos'] ) && 0 === strpos( (string) $award->id, 'historical-' ) ) {
     279                                continue;
     280                        }
     281                        if ( '' === $name ) {
     282                                $name = trim( $award->first_name . ' ' . $initial );
     283                        }
     284                        echo '<article class="pig-winner"><h3>' . esc_html( $name ?: __( 'Giveaway winner', 'pi-giveaways' ) ) . '</h3>';
     285                        if ( ! empty( $row['review'] ) ) {
     286                                echo '<div class="pig-winner-review">' . wp_kses_post( wpautop( esc_html( $row['review'] ) ) ) . '</div>';
     287                        }
     288                        foreach ( $row['photos'] ?? array() as $photo ) {
     289                                echo '<img class="pig-winner-photo" src="' . esc_url( $photo, array( 'http', 'https' ) ) . '" loading="lazy" alt="' . esc_attr__( 'Photo shared by a giveaway winner', 'pi-giveaways' ) . '">';
     290                        }
     291                        foreach ( $row['videos'] ?? array() as $video ) {
     292                                $this->render_winner_video( $id, $video );
     293                        }
     294                        echo '</article>';
     295                }
     296                $cards = ob_get_clean();
     297                return $cards ? '<section class="pig-winners"><h2>' . esc_html__( 'Giveaway Winners', 'pi-giveaways' ) . '</h2>' . $cards . '</section>' : '';
     298        }
     299
     300        private function render_winner_video( $post_id, $url ) {
     301                if ( ! PIGI_Repository::winner_video_urls( $url ) ) {
     302                        return;
     303                }
     304                $host = strtolower( wp_parse_url( $url, PHP_URL_HOST ) );
     305                $path = wp_parse_url( $url, PHP_URL_PATH );
     306                $player = '';
     307                if ( preg_match( '~^(?:www\.|m\.)?instagram\.com$~', $host ) && preg_match( '~^/(reel|p|tv)/([A-Za-z0-9_-]+)/?$~', $path, $match ) ) {
     308                        $player = 'https://www.instagram.com/' . $match[1] . '/' . $match[2] . '/embed/';
     309                } elseif ( preg_match( '~^(?:(?:www\.|m\.)?facebook\.com|fb\.watch)$~', $host ) ) {
     310                        $player = add_query_arg( 'href', rawurlencode( $url ), 'https://www.facebook.com/plugins/video.php' );
     311                }
     312                if ( $player ) {
     313                        echo '<div class="pig-winner-video"><iframe src="' . esc_url( $player ) . '" title="' . esc_attr__( 'Winner video', 'pi-giveaways' ) . '" width="500" height="480" style="max-width:100%;border:0" loading="lazy" allowfullscreen></iframe>';
     314                        echo '<p><a href="' . esc_url( $url ) . '">' . esc_html__( 'Watch on the original platform', 'pi-giveaways' ) . '</a></p></div>';
     315                        return;
     316                }
     317                $embed_url = preg_replace( '~^https://(?:www\.|m\.)?youtube\.com/shorts/([a-zA-Z0-9_-]{11})(?:[/?#].*)?$~i', 'https://www.youtube.com/watch?v=$1', $url );
     318                global $wp_embed;
     319                $previous_post_id = $wp_embed->post_ID;
     320                $previous_link    = $wp_embed->linkifunknown;
     321                $wp_embed->post_ID = $post_id;
     322                $wp_embed->linkifunknown = false;
     323                try {
     324                        $embed = $wp_embed->shortcode( array( 'discover' => false ), $embed_url );
     325                } finally {
     326                        $wp_embed->post_ID = $previous_post_id;
     327                        $wp_embed->linkifunknown = $previous_link;
     328                }
     329                echo '<div class="pig-winner-video">';
     330                if ( $embed && $embed_url !== $embed && esc_html( $embed_url ) !== $embed ) {
     331                        echo $embed; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- WordPress-generated oEmbed HTML.
     332                } else {
     333                        echo '<a href="' . esc_url( $url ) . '">' . esc_html__( 'Watch the winner’s video', 'pi-giveaways' ) . '</a>';
     334                }
     335                echo '</div>';
    259336        }
    260337
  • pi-giveaways/tags/1.0.13/includes/class-pigi-repository.php

    r3761409 r3830437  
    177177        }
    178178
     179        public static function winner_video_urls( $input ) {
     180                $urls = array();
     181                foreach ( preg_split( '/\R/', is_string( $input ) ? $input : '' ) as $line ) {
     182                        $url = esc_url_raw( trim( $line ), array( 'https' ) );
     183                        if ( ! preg_match( '~^https://(?:www\.|m\.)?(?:youtube\.com|youtu\.be|instagram\.com|facebook\.com|fb\.watch)/[^\s]+$~i', $url ) ) {
     184                                continue;
     185                        }
     186                        $urls[] = $url;
     187                }
     188                return array_values( array_unique( $urls ) );
     189        }
     190
     191        public static function is_historical( $giveaway_id ) {
     192                $publication = (array) get_post_meta( $giveaway_id, '_pigiveaways_winner_publication', true );
     193                $details     = (array) get_post_meta( $giveaway_id, '_pigiveaways_details', true );
     194                return ! empty( $publication['historical'] ) && in_array( $details['status'] ?? '', array( 'ended', 'awarded' ), true );
     195        }
     196
     197        public static function publication_winner_slots( $giveaway_id ) {
     198                $awards = array_values( array_filter( self::active_awards( $giveaway_id ), static function ( $award ) {
     199                        return 'winner' === $award->award_type;
     200                } ) );
     201                if ( self::is_historical( $giveaway_id ) ) {
     202                        $remaining = self::winner_limit( $giveaway_id ) - count( $awards );
     203                        for ( $slot = 1; $slot <= $remaining; ++$slot ) {
     204                                $awards[] = (object) array(
     205                                        'id' => 'historical-' . $slot,
     206                                        'first_name' => '',
     207                                        'last_name' => '',
     208                                );
     209                        }
     210                }
     211                return $awards;
     212        }
     213
    179214        public static function award( $entry_id, $note = '' ) {
    180215                global $wpdb;
    … …  
    183218                        return new WP_Error( 'invalid_entry', __( 'Only a valid entry can receive an award.', 'pi-giveaways' ) );
    184219                }
     220                $historical = self::is_historical( $entry->giveaway_id );
    185221                $permission = apply_filters( 'pigiveaways_can_award_entry', true, $entry );
    186222                if ( is_wp_error( $permission ) ) {
    … …  
    216252                }
    217253                $award_id = (int) $wpdb->insert_id;
    218                 do_action( 'pigiveaways_winner_awarded', $award_id, $entry );
     254                if ( ! $historical ) {
     255                        do_action( 'pigiveaways_winner_awarded', $award_id, $entry );
     256                }
    219257                return $award_id;
    220258        }
  • pi-giveaways/tags/1.0.13/pi-giveaways.php

    r3761409 r3830437  
    44 * Plugin URI: https://wordpress.org/plugins/pi-giveaways/
    55 * Description: Create giveaways, collect entries, and manually award winners through a focused, privacy-aware workflow.
    6  * Version: 1.0.7
     6 * Version: 1.0.13
    77 * Author: Ali Qureshi
    88 * Author URI: https://www.parorrey.com/
    … …  
    1818defined( 'ABSPATH' ) || exit;
    1919
    20 define( 'PIGIVEAWAYS_VERSION', '1.0.7' );
     20define( 'PIGIVEAWAYS_VERSION', '1.0.13' );
    2121define( 'PIGIVEAWAYS_FILE', __FILE__ );
    2222define( 'PIGIVEAWAYS_DIR', plugin_dir_path( __FILE__ ) );
  • pi-giveaways/tags/1.0.13/readme.txt

    r3811301 r3830437  
    66Tested up to: 7.1.5
    77Requires PHP: 7.4
    8 Stable tag: 1.0.7
     8Stable tag: 1.0.13
    99License: GPLv2 or later
    1010License URI: https://www.gnu.org/licenses/gpl-2.0.html
    … …  
    2626* Giveaway listings with current and past sections.
    2727* Entry moderation, CSV export, and unlimited manual winner records.
     28* Winner reviews, Media Library photo selection, and YouTube, Instagram, or Facebook video/reel URLs.
     29* Historical giveaways with silent entry-linked or manually named winner records and separate opt-in public display.
    2830* Entry confirmation and administrator notification emails through `wp_mail()`.
    2931* Honeypot protection and optional Google reCAPTCHA v3.
    … …  
    100102== Changelog ==
    101103
     104= 1.0.13 =
     105* Add a Select or upload photos button for each winner using the WordPress Media Library.
     106* Add a Video or reel URLs field alongside each winner’s photos and review, supporting YouTube, Instagram, and Facebook.
     107* Add optional Historical giveaway mode to record past winners from existing entries or by public display name when no entry is on file.
     108* Support reviews and photos for both entry-linked and manually recorded historical winners.
     109* Record historical winners silently without triggering winner-award notifications.
     110* Keep publication of winner details on the giveaway page controlled by a separate checkbox.
     111
     112= 1.0.12 =
     113* Select or upload multiple winner photos using the WordPress Media Library.
     114
     115= 1.0.11 =
     116* Accept pasted HTML image tags in winner photo fields and preserve URL encoding.
     117
     118= 1.0.10 =
     119* Display historical winner reviews and photos when the optional public name is blank.
     120
     121= 1.0.9 =
     122* Add optional historical giveaway mode for silently recording and publishing past winners.
     123
     124= 1.0.8 =
     125* Add optional public winner names, reviews, and photos on closed giveaway pages.
     126
    102127= 1.0.7 =
    103128* Adds extension hooks so eligibility-aware add-ons can replace a premature Award winner link with their confirmation workflow.
  • pi-giveaways/trunk/includes/class-pigi-admin.php

    r3761409 r3830437  
    285285                wp_enqueue_style( 'pigiveaways-admin-preview', PIGIVEAWAYS_URL . 'assets/admin-preview.css', array( 'pigiveaways-admin' ), PIGIVEAWAYS_VERSION );
    286286                wp_enqueue_script( 'pigiveaways-admin', PIGIVEAWAYS_URL . 'assets/admin.js', array(), PIGIVEAWAYS_VERSION, true );
     287                if ( $screen && 'pigiveaways_giveaway' === $screen->post_type && 'post' === $screen->base && current_user_can( 'upload_files' ) ) {
     288                        wp_enqueue_media();
     289                        wp_enqueue_script( 'pigiveaways-winner-photos', PIGIVEAWAYS_URL . 'assets/winner-photos.js', array( 'media-editor' ), PIGIVEAWAYS_VERSION, true );
     290                }
    287291                if ( false !== strpos( $hook, 'pigiveaways' ) ) {
    288292                        $settings = wp_parse_args( get_option( $this->option, array() ), $this->defaults() );
    … …  
    351355                add_meta_box( 'pig-details', __( 'Giveaway Details', 'pi-giveaways' ), array( $this, 'details_box' ), 'pigiveaways_giveaway', 'normal', 'high' );
    352356                add_meta_box( 'pig-form-builder', __( 'Giveaway Entry Form', 'pi-giveaways' ), array( $this, 'form_builder_box' ), 'pigiveaways_giveaway', 'normal', 'default' );
     357                add_meta_box( 'pig-winner-publication', __( 'Public Winners and Reviews', 'pi-giveaways' ), array( $this, 'winner_publication_box' ), 'pigiveaways_giveaway', 'normal', 'default' );
     358        }
     359
     360        public function winner_publication_box( $post ) {
     361                $publication = (array) get_post_meta( $post->ID, '_pigiveaways_winner_publication', true );
     362                echo '<input type="hidden" name="pig_winner_publication_present" value="1">';
     363                echo '<p><label><input type="checkbox" name="pig_historical_giveaway" value="1" ' . checked( ! empty( $publication['historical'] ), true, false ) . '> ' . esc_html__( 'Historical giveaway: record past winners silently', 'pi-giveaways' ) . '</label></p>';
     364                echo '<p>' . esc_html__( 'Set the giveaway status to Ended or Awarded before recording historical winners.', 'pi-giveaways' ) . '</p>';
     365                echo '<p><label><input type="checkbox" name="pig_publish_winners" value="1" ' . checked( ! empty( $publication['enabled'] ), true, false ) . '> ' . esc_html__( 'Automatically display recorded winners on the closed giveaway page', 'pi-giveaways' ) . '</label></p>';
     366                echo '<p>' . esc_html__( 'This setting publishes winner names, reviews, and photos on Ended and Awarded giveaways. Enable it only with the winners’ permission. Paste image URLs, one per line, or paste HTML image tags.', 'pi-giveaways' ) . '</p>';
     367                foreach ( PIGI_Repository::publication_winner_slots( $post->ID ) as $award ) {
     368                        $row = $publication['winners'][ $award->id ] ?? array();
     369                        $key = 'pig_public_winner[' . $award->id . ']';
     370                        $label = trim( $award->first_name . ' ' . $award->last_name );
     371                        echo '<fieldset><legend><strong>' . esc_html( $label ?: __( 'Historical winner', 'pi-giveaways' ) ) . '</strong></legend>';
     372                        echo '<p><label>' . esc_html__( 'Public display name (optional)', 'pi-giveaways' ) . '<br><input class="regular-text" name="' . esc_attr( $key . '[name]' ) . '" value="' . esc_attr( $row['name'] ?? '' ) . '"></label></p>';
     373                        echo '<p><label>' . esc_html__( 'Winner review', 'pi-giveaways' ) . '<br><textarea class="large-text" rows="4" name="' . esc_attr( $key . '[review]' ) . '">' . esc_textarea( $row['review'] ?? '' ) . '</textarea></label></p>';
     374                        echo '<div data-pig-winner-photos>';
     375                        if ( current_user_can( 'upload_files' ) ) {
     376                                echo '<p><button type="button" class="button" data-pig-choose-photos data-title="' . esc_attr__( 'Select or upload winner photos', 'pi-giveaways' ) . '" data-confirm="' . esc_attr__( 'Add photos', 'pi-giveaways' ) . '">' . esc_html__( 'Select or upload photos', 'pi-giveaways' ) . '</button></p>';
     377                        }
     378                        echo '<p><label>' . esc_html__( 'Photo URLs (remove a line to remove a photo)', 'pi-giveaways' ) . '<br><textarea class="large-text" rows="3" name="' . esc_attr( $key . '[photos]' ) . '">' . esc_textarea( implode( "\n", $row['photos'] ?? array() ) ) . '</textarea></label></p></div>';
     379                        echo '<p><label>' . esc_html__( 'Video or reel URLs (YouTube, Instagram or Facebook, one per line)', 'pi-giveaways' ) . '<br><textarea class="large-text" rows="3" name="' . esc_attr( $key . '[videos]' ) . '">' . esc_textarea( implode( "\n", $row['videos'] ?? array() ) ) . '</textarea></label></p></fieldset>';
     380                }
     381        }
     382
     383        private function save_winner_publication( $post_id ) {
     384                if ( ! isset( $_POST['pig_winner_publication_present'] ) ) {
     385                        return;
     386                }
     387                $submitted = (array) wp_unslash( $_POST['pig_public_winner'] ?? array() );
     388                $previous  = (array) get_post_meta( $post_id, '_pigiveaways_winner_publication', true );
     389                $winners   = (array) ( $previous['winners'] ?? array() );
     390                foreach ( PIGI_Repository::publication_winner_slots( $post_id ) as $award ) {
     391                        $row = $submitted[ $award->id ] ?? null;
     392                        if ( ! is_array( $row ) ) {
     393                                continue;
     394                        }
     395                        $winners[ $award->id ] = array(
     396                                'name'   => sanitize_text_field( $row['name'] ?? '' ),
     397                                'review' => sanitize_textarea_field( $row['review'] ?? '' ),
     398                                'photos' => $this->parse_winner_photos( $row['photos'] ?? '' ),
     399                                'videos' => PIGI_Repository::winner_video_urls( $row['videos'] ?? '' ),
     400                        );
     401                }
     402                update_post_meta( $post_id, '_pigiveaways_winner_publication', wp_slash( array(
     403                        'enabled' => isset( $_POST['pig_publish_winners'] ),
     404                        'historical' => isset( $_POST['pig_historical_giveaway'] ),
     405                        'winners' => $winners,
     406                ) ) );
     407        }
     408
     409        private function parse_winner_photos( $input ) {
     410                if ( ! is_string( $input ) ) {
     411                        return array();
     412                }
     413
     414                $candidates = array();
     415                $tags = new WP_HTML_Tag_Processor( $input );
     416                while ( $tags->next_tag( 'IMG' ) ) {
     417                        $src = $tags->get_attribute( 'src' );
     418                        if ( is_string( $src ) ) {
     419                                $candidates[] = $src;
     420                        }
     421                }
     422                foreach ( preg_split( '/\R/', $input ) as $line ) {
     423                        if ( preg_match( '~^(https?://|//|/)~i', trim( $line ) ) && false === strpos( $line, '<' ) ) {
     424                                $candidates[] = $line;
     425                        }
     426                }
     427
     428                $photos = array();
     429                foreach ( $candidates as $candidate ) {
     430                        $url = trim( $candidate );
     431                        if ( 0 === strpos( $url, '//' ) ) {
     432                                $url = 'https:' . $url;
     433                        } elseif ( 0 === strpos( $url, '/' ) ) {
     434                                $url = home_url( $url );
     435                        }
     436                        if ( ! preg_match( '~^https?://~i', $url ) ) {
     437                                continue;
     438                        }
     439                        $url = esc_url_raw( $url, array( 'http', 'https' ) );
     440                        if ( wp_parse_url( $url, PHP_URL_HOST ) ) {
     441                                $photos[] = $url;
     442                        }
     443                }
     444                return array_values( array_unique( $photos ) );
    353445        }
    354446
    … …  
    522614                update_post_meta( $post_id, '_pigiveaways_form_fields', $stored_fields );
    523615                update_post_meta( $post_id, '_pigiveaways_form_mode', $mode );
     616                $this->save_winner_publication( $post_id );
    524617        }
    525618
  • pi-giveaways/trunk/includes/class-pigi-frontend.php

    r3751015 r3830437  
    241241                        echo '<p class="pig-related-event"><a href="' . esc_url( $d['event_url'] ) . '">' . esc_html__( 'View related event', 'pi-giveaways' ) . ' →</a></p>';
    242242                }
     243                $winners = '';
     244                if ( 'publish' === $post->post_status && ! post_password_required( $post ) && in_array( $state, array( 'ended', 'awarded' ), true ) ) {
     245                        $winners = $this->render_public_winners( $id );
     246                }
    243247                if ( 'active' === $state ) {
    244248                        $this->form( $id, $d, $settings );
    … …  
    246250                        echo '<p>' . esc_html__( 'This giveaway is coming soon.', 'pi-giveaways' ) . '</p>';
    247251                } elseif ( 'ended' === $state ) {
    248                         echo '<p>' . esc_html__( 'Entries are closed. Any award will be assigned manually by the promoter.', 'pi-giveaways' ) . '</p>';
     252                        echo '<p>' . esc_html( $winners ? __( 'Entries are closed.', 'pi-giveaways' ) : __( 'Entries are closed. Any award will be assigned manually by the promoter.', 'pi-giveaways' ) ) . '</p>';
    249253                } elseif ( 'awarded' === $state ) {
    250254                                $announcement = $d['announcement'] ? $d['announcement'] : __( 'The award has been announced.', 'pi-giveaways' );
    … …  
    255259                        echo '<p>' . esc_html__( 'This giveaway is not accepting entries.', 'pi-giveaways' ) . '</p>';
    256260                }
     261                // The winner section escapes its data when building the markup.
     262                echo $winners; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
    257263                echo '</div></section>';
    258264                return ob_get_clean();
     265        }
     266
     267        private function render_public_winners( $id ) {
     268                $publication = (array) get_post_meta( $id, '_pigiveaways_winner_publication', true );
     269                if ( empty( $publication['enabled'] ) ) {
     270                        return '';
     271                }
     272
     273                ob_start();
     274                foreach ( PIGI_Repository::publication_winner_slots( $id ) as $award ) {
     275                        $row     = $publication['winners'][ $award->id ] ?? array();
     276                        $initial = preg_match( '/^\p{L}/u', trim( (string) $award->last_name ), $match ) ? $match[0] . '.' : '';
     277                        $name    = $row['name'] ?? '';
     278                        if ( '' === $name && empty( $row['review'] ) && empty( $row['photos'] ) && empty( $row['videos'] ) && 0 === strpos( (string) $award->id, 'historical-' ) ) {
     279                                continue;
     280                        }
     281                        if ( '' === $name ) {
     282                                $name = trim( $award->first_name . ' ' . $initial );
     283                        }
     284                        echo '<article class="pig-winner"><h3>' . esc_html( $name ?: __( 'Giveaway winner', 'pi-giveaways' ) ) . '</h3>';
     285                        if ( ! empty( $row['review'] ) ) {
     286                                echo '<div class="pig-winner-review">' . wp_kses_post( wpautop( esc_html( $row['review'] ) ) ) . '</div>';
     287                        }
     288                        foreach ( $row['photos'] ?? array() as $photo ) {
     289                                echo '<img class="pig-winner-photo" src="' . esc_url( $photo, array( 'http', 'https' ) ) . '" loading="lazy" alt="' . esc_attr__( 'Photo shared by a giveaway winner', 'pi-giveaways' ) . '">';
     290                        }
     291                        foreach ( $row['videos'] ?? array() as $video ) {
     292                                $this->render_winner_video( $id, $video );
     293                        }
     294                        echo '</article>';
     295                }
     296                $cards = ob_get_clean();
     297                return $cards ? '<section class="pig-winners"><h2>' . esc_html__( 'Giveaway Winners', 'pi-giveaways' ) . '</h2>' . $cards . '</section>' : '';
     298        }
     299
     300        private function render_winner_video( $post_id, $url ) {
     301                if ( ! PIGI_Repository::winner_video_urls( $url ) ) {
     302                        return;
     303                }
     304                $host = strtolower( wp_parse_url( $url, PHP_URL_HOST ) );
     305                $path = wp_parse_url( $url, PHP_URL_PATH );
     306                $player = '';
     307                if ( preg_match( '~^(?:www\.|m\.)?instagram\.com$~', $host ) && preg_match( '~^/(reel|p|tv)/([A-Za-z0-9_-]+)/?$~', $path, $match ) ) {
     308                        $player = 'https://www.instagram.com/' . $match[1] . '/' . $match[2] . '/embed/';
     309                } elseif ( preg_match( '~^(?:(?:www\.|m\.)?facebook\.com|fb\.watch)$~', $host ) ) {
     310                        $player = add_query_arg( 'href', rawurlencode( $url ), 'https://www.facebook.com/plugins/video.php' );
     311                }
     312                if ( $player ) {
     313                        echo '<div class="pig-winner-video"><iframe src="' . esc_url( $player ) . '" title="' . esc_attr__( 'Winner video', 'pi-giveaways' ) . '" width="500" height="480" style="max-width:100%;border:0" loading="lazy" allowfullscreen></iframe>';
     314                        echo '<p><a href="' . esc_url( $url ) . '">' . esc_html__( 'Watch on the original platform', 'pi-giveaways' ) . '</a></p></div>';
     315                        return;
     316                }
     317                $embed_url = preg_replace( '~^https://(?:www\.|m\.)?youtube\.com/shorts/([a-zA-Z0-9_-]{11})(?:[/?#].*)?$~i', 'https://www.youtube.com/watch?v=$1', $url );
     318                global $wp_embed;
     319                $previous_post_id = $wp_embed->post_ID;
     320                $previous_link    = $wp_embed->linkifunknown;
     321                $wp_embed->post_ID = $post_id;
     322                $wp_embed->linkifunknown = false;
     323                try {
     324                        $embed = $wp_embed->shortcode( array( 'discover' => false ), $embed_url );
     325                } finally {
     326                        $wp_embed->post_ID = $previous_post_id;
     327                        $wp_embed->linkifunknown = $previous_link;
     328                }
     329                echo '<div class="pig-winner-video">';
     330                if ( $embed && $embed_url !== $embed && esc_html( $embed_url ) !== $embed ) {
     331                        echo $embed; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- WordPress-generated oEmbed HTML.
     332                } else {
     333                        echo '<a href="' . esc_url( $url ) . '">' . esc_html__( 'Watch the winner’s video', 'pi-giveaways' ) . '</a>';
     334                }
     335                echo '</div>';
    259336        }
    260337
  • pi-giveaways/trunk/includes/class-pigi-repository.php

    r3761409 r3830437  
    177177        }
    178178
     179        public static function winner_video_urls( $input ) {
     180                $urls = array();
     181                foreach ( preg_split( '/\R/', is_string( $input ) ? $input : '' ) as $line ) {
     182                        $url = esc_url_raw( trim( $line ), array( 'https' ) );
     183                        if ( ! preg_match( '~^https://(?:www\.|m\.)?(?:youtube\.com|youtu\.be|instagram\.com|facebook\.com|fb\.watch)/[^\s]+$~i', $url ) ) {
     184                                continue;
     185                        }
     186                        $urls[] = $url;
     187                }
     188                return array_values( array_unique( $urls ) );
     189        }
     190
     191        public static function is_historical( $giveaway_id ) {
     192                $publication = (array) get_post_meta( $giveaway_id, '_pigiveaways_winner_publication', true );
     193                $details     = (array) get_post_meta( $giveaway_id, '_pigiveaways_details', true );
     194                return ! empty( $publication['historical'] ) && in_array( $details['status'] ?? '', array( 'ended', 'awarded' ), true );
     195        }
     196
     197        public static function publication_winner_slots( $giveaway_id ) {
     198                $awards = array_values( array_filter( self::active_awards( $giveaway_id ), static function ( $award ) {
     199                        return 'winner' === $award->award_type;
     200                } ) );
     201                if ( self::is_historical( $giveaway_id ) ) {
     202                        $remaining = self::winner_limit( $giveaway_id ) - count( $awards );
     203                        for ( $slot = 1; $slot <= $remaining; ++$slot ) {
     204                                $awards[] = (object) array(
     205                                        'id' => 'historical-' . $slot,
     206                                        'first_name' => '',
     207                                        'last_name' => '',
     208                                );
     209                        }
     210                }
     211                return $awards;
     212        }
     213
    179214        public static function award( $entry_id, $note = '' ) {
    180215                global $wpdb;
    … …  
    183218                        return new WP_Error( 'invalid_entry', __( 'Only a valid entry can receive an award.', 'pi-giveaways' ) );
    184219                }
     220                $historical = self::is_historical( $entry->giveaway_id );
    185221                $permission = apply_filters( 'pigiveaways_can_award_entry', true, $entry );
    186222                if ( is_wp_error( $permission ) ) {
    … …  
    216252                }
    217253                $award_id = (int) $wpdb->insert_id;
    218                 do_action( 'pigiveaways_winner_awarded', $award_id, $entry );
     254                if ( ! $historical ) {
     255                        do_action( 'pigiveaways_winner_awarded', $award_id, $entry );
     256                }
    219257                return $award_id;
    220258        }
  • pi-giveaways/trunk/pi-giveaways.php

    r3761409 r3830437  
    44 * Plugin URI: https://wordpress.org/plugins/pi-giveaways/
    55 * Description: Create giveaways, collect entries, and manually award winners through a focused, privacy-aware workflow.
    6  * Version: 1.0.7
     6 * Version: 1.0.13
    77 * Author: Ali Qureshi
    88 * Author URI: https://www.parorrey.com/
    … …  
    1818defined( 'ABSPATH' ) || exit;
    1919
    20 define( 'PIGIVEAWAYS_VERSION', '1.0.7' );
     20define( 'PIGIVEAWAYS_VERSION', '1.0.13' );
    2121define( 'PIGIVEAWAYS_FILE', __FILE__ );
    2222define( 'PIGIVEAWAYS_DIR', plugin_dir_path( __FILE__ ) );
  • pi-giveaways/trunk/readme.txt

    r3811301 r3830437  
    66Tested up to: 7.1.5
    77Requires PHP: 7.4
    8 Stable tag: 1.0.7
     8Stable tag: 1.0.13
    99License: GPLv2 or later
    1010License URI: https://www.gnu.org/licenses/gpl-2.0.html
    … …  
    2626* Giveaway listings with current and past sections.
    2727* Entry moderation, CSV export, and unlimited manual winner records.
     28* Winner reviews, Media Library photo selection, and YouTube, Instagram, or Facebook video/reel URLs.
     29* Historical giveaways with silent entry-linked or manually named winner records and separate opt-in public display.
    2830* Entry confirmation and administrator notification emails through `wp_mail()`.
    2931* Honeypot protection and optional Google reCAPTCHA v3.
    … …  
    100102== Changelog ==
    101103
     104= 1.0.13 =
     105* Add a Select or upload photos button for each winner using the WordPress Media Library.
     106* Add a Video or reel URLs field alongside each winner’s photos and review, supporting YouTube, Instagram, and Facebook.
     107* Add optional Historical giveaway mode to record past winners from existing entries or by public display name when no entry is on file.
     108* Support reviews and photos for both entry-linked and manually recorded historical winners.
     109* Record historical winners silently without triggering winner-award notifications.
     110* Keep publication of winner details on the giveaway page controlled by a separate checkbox.
     111
     112= 1.0.12 =
     113* Select or upload multiple winner photos using the WordPress Media Library.
     114
     115= 1.0.11 =
     116* Accept pasted HTML image tags in winner photo fields and preserve URL encoding.
     117
     118= 1.0.10 =
     119* Display historical winner reviews and photos when the optional public name is blank.
     120
     121= 1.0.9 =
     122* Add optional historical giveaway mode for silently recording and publishing past winners.
     123
     124= 1.0.8 =
     125* Add optional public winner names, reviews, and photos on closed giveaway pages.
     126
    102127= 1.0.7 =
    103128* Adds extension hooks so eligibility-aware add-ons can replace a premature Award winner link with their confirmation workflow.
Note: See TracChangeset for help on using the changeset viewer.