Plugin Directory

Changeset 2793155


Ignore:
Timestamp:
10/02/2022 10:41:20 PM (3 years ago)
Author:
denisco
Message:

New version 2.4.0 https://github.com/yadenis/DCO-Comment-Attachment/releases/tag/v2.4.0

Location:
dco-comment-attachment
Files:
15 added
6 edited

Legend:

Unmodified
Added
Removed
  • dco-comment-attachment/trunk/assets/dco-comment-attachment-admin.js

    r2636964 r2793155  
    5454            }
    5555        );
     56
     57        // Only for DCO_CA_Admin::show_bulk_action_message()
     58        if ( $( '#the-comment-list' ).length ) {
     59            const $referer = $( '[name="_wp_http_referer"]' );
     60            const params = new URLSearchParams( $referer.val() );
     61            params.delete( 'deletedattachment' );
     62            $referer.val( decodeURIComponent( params.toString() ) );
     63        }
    5664
    5765        $( '#dco-comment-attachment' ).on(
  • dco-comment-attachment/trunk/dco-comment-attachment.php

    r2642680 r2793155  
    1313 * Plugin URI: https://denisco.pro/dco-comment-attachment/
    1414 * Description: Allows your visitors to attach files with their comments
    15  * Version: 2.3.1
     15 * Version: 2.4.0
    1616 * Author: Denis Yanchevskiy
    1717 * Author URI: https://denisco.pro
     
    2525define( 'DCO_CA_PATH', plugin_dir_path( __FILE__ ) );
    2626define( 'DCO_CA_BASENAME', plugin_basename( __FILE__ ) );
    27 define( 'DCO_CA_VERSION', '2.3.1' );
     27define( 'DCO_CA_VERSION', '2.4.0' );
    2828
    2929require_once DCO_CA_PATH . 'includes/functions.php';
  • dco-comment-attachment/trunk/includes/class-dco-ca-admin.php

    r2642679 r2793155  
    4040
    4141        add_filter( 'comment_row_actions', array( $this, 'add_comment_action_links' ), 10, 2 );
    42         add_action( 'admin_action_deleteattachment', array( $this, 'delete_attachment_action' ) );
     42        add_action( 'admin_action_deletecommentattachment', array( $this, 'delete_attachment_action' ) );
     43        add_filter( 'bulk_actions-edit-comments', array( $this, 'add_comments_bulk_actions' ) );
     44        add_action( 'admin_action_deleteattachment', array( $this, 'delete_attachment_bulk_action' ) );
     45        add_filter( 'ngettext', array( $this, 'show_bulk_action_message' ), 10, 5 );
     46        add_filter( 'removable_query_args', array( $this, 'add_removable_query_args' ) );
    4347        add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
    4448        add_action( 'wp_ajax_delete_attachment', array( $this, 'delete_attachment_ajax' ) );
     
    7175
    7276            $del_attach_nonce = esc_html( '_wpnonce=' . $nonce );
    73             $url              = esc_url( "comment.php?c=$comment_id&action=deleteattachment&$del_attach_nonce" );
    74 
    75             $title                       = esc_html__( 'Delete Attachment', 'dco-comment-attachment' );
    76             $actions['deleteattachment'] = "<a href='$url' class='dco-del-attachment' data-id='$comment_id' data-nonce='$nonce'>$title</a>";
     77            $url              = esc_url( "comment.php?c=$comment_id&action=deletecommentattachment&$del_attach_nonce" );
     78
     79            $title                              = esc_html__( 'Delete Attachment', 'dco-comment-attachment' );
     80            $actions['deletecommentattachment'] = "<a href='$url' class='dco-del-attachment' data-id='$comment_id' data-nonce='$nonce'>$title</a>";
    7781        }
    7882
     
    119123        wp_safe_redirect( $redir );
    120124        exit();
     125    }
     126
     127    /**
     128     * Adds additional bulk actions.
     129     *
     130     * @since 2.4.0
     131     *
     132     * @param array $actions An array of the available bulk actions.
     133     * @return array An array with standard bulk actions
     134     *               and attachment bulk actions.
     135     */
     136    public function add_comments_bulk_actions( $actions ) {
     137        $actions['deleteattachment'] = __( 'Delete Attachment', 'dco-comment-attachment' );
     138
     139        return $actions;
     140    }
     141
     142    /**
     143     * Handles a bulk action to delete comment attachments on the comments page.
     144     *
     145     * @since 2.4.0
     146     */
     147    public function delete_attachment_bulk_action() {
     148        check_admin_referer( 'bulk-comments' );
     149
     150        if ( isset( $_REQUEST['delete_comments'] ) && is_array( $_REQUEST['delete_comments'] ) ) {
     151            $comment_ids = array_map( 'absint', $_REQUEST['delete_comments'] );
     152        } else {
     153            return;
     154        }
     155
     156        $redirect_to = remove_query_arg( array( 'trashed', 'untrashed', 'deleted', 'spammed', 'unspammed', 'approved', 'unapproved', 'ids', 'deletedattachment' ), wp_get_referer() );
     157
     158        wp_defer_comment_counting( true );
     159
     160        $count = 0;
     161        foreach ( $comment_ids as $comment_id ) {
     162            if ( ! current_user_can( 'edit_comment', $comment_id ) ) {
     163                continue;
     164            }
     165
     166            $comment = get_comment( $comment_id );
     167            if ( ! $comment ) {
     168                continue;
     169            }
     170
     171            $delete = $this->get_option( 'delete_attachment_action' );
     172            if ( $this->delete_attachment( $comment_id, $delete ) ) {
     173                $count ++;
     174            }
     175        }
     176
     177        wp_defer_comment_counting( false );
     178
     179        $redirect_to = add_query_arg( 'deletedattachment', $count, $redirect_to );
     180
     181        // @see DCO_CA_Admin::show_bulk_action_message() for details.
     182        $redirect_to = add_query_arg( 'approved', $count, $redirect_to );
     183
     184        wp_safe_redirect( $redirect_to );
     185        exit;
     186    }
     187
     188    /**
     189     * Shows bulk action updated message.
     190     *
     191     * @since 2.4.0
     192     *
     193     * @param string $translation Translated text.
     194     * @param string $single      The text to be used if the number is singular.
     195     * @param string $plural      The text to be used if the number is plural.
     196     * @param string $number      The number to compare against to use either the singular or plural form.
     197     * @param string $domain      Text domain. Unique identifier for retrieving translated strings.
     198     *
     199     * @return string Filtered translated text.
     200     */
     201    public function show_bulk_action_message( $translation, $single, $plural, $number, $domain ) {
     202        /**
     203         * There is no hook in WordPress to add updated message for custom comments bulk action.
     204         * So we override the approval message if attachment deletion was triggered.
     205         */
     206        // phpcs:disable WordPress.Security.NonceVerification.Recommended, WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
     207        if ( ! isset( $_REQUEST['deletedattachment'] ) || ! $_REQUEST['deletedattachment'] ) {
     208            return $translation;
     209        }
     210        // phpcs:enable
     211
     212        if ( '%s comment approved.' === $single && '%s comments approved.' === $plural && 'default' === $domain ) {
     213            /* translators: %s: Number of comments. */
     214            $translation = sprintf( _n( 'Attachments deleted from %s comment.', 'Attachments deleted from %s comments.', $number, 'dco-comment-attachment' ), $number );
     215        }
     216
     217        return $translation;
     218    }
     219
     220    /**
     221     * Adds single-use URL parameters.
     222     *
     223     * @since 2.4.0
     224     *
     225     * @param array $removable_query_args An array of query variable names to remove from the URL.
     226     * @return array An array of query variable names to remove from the URL.
     227     */
     228    public function add_removable_query_args( $removable_query_args ) {
     229        $removable_query_args[] = 'deletedattachment';
     230
     231        return $removable_query_args;
    121232    }
    122233
     
    272383
    273384        $attachment_id = isset( $_POST['dco_attachment_id'] ) ? array_map( 'intval', $_POST['dco_attachment_id'] ) : array();
     385
     386        // We need to delete the last empty element, because it's used
     387        // as a placeholder in the attachments edit form.
     388        // @see DCO_CA_Admin::render_attachment_metabox.
     389        array_pop( $attachment_id );
     390
    274391        if ( $attachment_id ) {
    275             // We need to delete the last empty element, because it's used
    276             // as a placeholder in the attachments edit form.
    277             // @see DCO_CA_Admin::render_attachment_metabox.
    278             array_pop( $attachment_id );
    279 
    280392            $this->assign_attachment( $comment_id, $attachment_id );
    281393        } else {
  • dco-comment-attachment/trunk/includes/class-dco-ca-base.php

    r2642679 r2793155  
    6868     *
    6969     * @param int $comment_id Optional. The comment ID.
    70      * @return int|string The assigned attachment ID on success,
    71      *                    empty string on failure.
     70     * @return int|array|string The assigned attachment ID(s) on success,
     71     *                          empty string on failure.
    7272     */
    7373    public function get_attachment_id( $comment_id = 0 ) {
     
    114114     * @since 1.0.0
    115115     *
    116      * @param int $comment_id The comment ID.
    117      * @param int $attachment_id The attachment ID.
     116     * @param int       $comment_id The comment ID.
     117     * @param int|array $attachment_id The attachment ID(s).
    118118     * @return int|bool Meta ID on success, false on failure.
    119119     */
  • dco-comment-attachment/trunk/includes/class-dco-ca.php

    r2642679 r2793155  
    7272            add_filter( 'comment_text', array( $this, 'autoembed_links' ), 5 );
    7373        }
     74
     75        add_shortcode( 'dco_ca', array( $this, 'dco_ca_shortcode' ) );
    7476    }
    7577
     
    530532        }
    531533
    532         $this->assign_attachment( $comment_id, $ids );
     534        if ( $ids ) {
     535            $this->assign_attachment( $comment_id, $ids );
     536        }
    533537
    534538        $_FILES[ $field_name ] = $attachments;
     
    570574        }
    571575
    572         $attachment_id = (array) $this->get_attachment_id( $comment->comment_ID );
     576        $attachment_id      = $this->get_attachment_id( $comment->comment_ID );
     577        $attachment_content = $this->generate_attachment_markup( $attachment_id );
     578
     579        return $comment_text . $attachment_content;
     580    }
     581
     582    /**
     583     * Generates an attachment markup.
     584     *
     585     * @since 2.4.0
     586     *
     587     * @param int|array $attachment_id The attachment ID(s).
     588     * @return string The attachment HTML markup.
     589     */
     590    public function generate_attachment_markup( $attachment_id ) {
     591        if ( ! is_array( $attachment_id ) ) {
     592            $attachment_id = (array) $attachment_id;
     593        }
     594
    573595        if ( count( $attachment_id ) > 1 ) {
    574596            $this->enable_gallery_image_size();
    575597            $attachments_content = array();
    576598            foreach ( $attachment_id as $attach_id ) {
     599                // Check that the attachment exists.
     600                if ( ! wp_get_attachment_url( $attach_id ) ) {
     601                    continue;
     602                }
     603
    577604                $type = $this->get_embed_type( $attach_id );
    578605                $key  = "{$type}_{$attach_id}";
     
    582609
    583610            if ( $this->get_option( 'combine_images' ) || is_admin() ) {
    584                 // combine only images.
     611                // Combine only images.
    585612                $not_images = array();
    586613                foreach ( $attachments_content as $key => $content ) {
     
    603630        }
    604631
    605         return $comment_text . $attachment_content;
     632        return $attachment_content;
    606633    }
    607634
     
    634661
    635662        return $response;
     663    }
     664
     665    /**
     666     * The dco_ca shortcode handler.
     667     *
     668     * @since 2.4.0
     669     *
     670     * @param array $atts {
     671     *     An array of shortcode attributes.
     672     *
     673     *     @type int $post_id Optional. The post ID. Default current post ID.
     674     *     @type string $type Optional. Attachment types separated by comma.
     675     *                                  Accepts: all, image, video, audio, misc.
     676     *                                  Default: all.
     677     * }
     678     */
     679    public function dco_ca_shortcode( $atts ) {
     680        $atts = shortcode_atts(
     681            array(
     682                'post_id' => get_the_ID(),
     683                'type'    => 'all',
     684            ),
     685            $atts,
     686            'dco_ca'
     687        );
     688
     689        $comments = get_comments(
     690            array(
     691                'post_id'  => $atts['post_id'],
     692                'meta_key' => 'attachment_id',
     693                'status'   => 'approve',
     694            )
     695        );
     696
     697        $ids = array();
     698        foreach ( $comments as $comment ) {
     699            $attachment_id = (array) $this->get_attachment_id( $comment->comment_ID );
     700            if ( 'all' === $atts['type'] ) {
     701                $ids = array_merge( $ids, $attachment_id );
     702            } else {
     703                $types = array_map( 'trim', explode( ',', $atts['type'] ) );
     704                foreach ( $attachment_id as $attach_id ) {
     705                    $type = $this->get_embed_type( $attach_id );
     706                    if ( in_array( $type, $types, true ) ) {
     707                        $ids[] = $attach_id;
     708                    }
     709                }
     710            }
     711        }
     712
     713        return '<div class="dco-attachments-list">' . $this->generate_attachment_markup( $ids ) . '</div>';
    636714    }
    637715
  • dco-comment-attachment/trunk/readme.txt

    r2642679 r2793155  
    33Tags: comment, comment attachment, attachment, image, video
    44Requires at least: 4.6
    5 Tested up to: 5.8
     5Tested up to: 6.0
    66Requires PHP: 5.6
    7 Stable tag: 2.3.1
     7Stable tag: 2.4.0
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
     
    3333You can also:
    3434
    35 * Add, replace or delete an attachment from a comment on Edit Comment screen.
     35* Add, replace or delete an attachment from a comment on the Edit Comment screen.
    3636* Attach an unlimited number of attachments to the comment in the admin panel.
    37 * Delete an attachment on Comments screen.
     37* Delete an attachment from a specific comment or bulk delete attachments from comments on the Comments screen.
     38* Display attachments attached to comments to the current post (or a specific post) with the `[dco_ca]` shortcode. You can also filter by type. See [FAQ](#faq) for details.
    3839
    3940Attachments are uploaded to the WordPress Media Library and deleted along with the comment (if this is set in the settings).
     
    6061Feel free to create [a new topic](https://wordpress.org/support/plugin/dco-comment-attachment/) on support forum if you need integration with another plugin.
    6162
     63= How to use the [dco_ca] shortcode? =
     64
     65Without attributes specified, the `[dco_ca]` shortcode will display all attachments attached to current post.
     66
     67You can filter attachments using the `type` attribute. By default it is `all`. Also supported: `image`, `video`, `audio` and `misc`.
     68You can specify one value `[dco_ca type="image"]` or multiple values, separated by commas `[dco_ca type="video,audio"]`.
     69
     70You can also display attachments from the comments of another post using the `post_id` attribute.
     71For example, `[dco_ca post_id="45"]`, where `45` is the ID of the specific post.
     72
     73You can also combine these attributes. For example, `[dco_ca post_id="45" type="image"]` will display all images attached to comments to the post with ID 45.
     74
    6275== Changelog ==
     76
     77= 2.4.0 =
     78* Added bulk delete attachments action on the Comments screen.
     79* Added the `[dco_ca]` shortcode for display attachments attached to comments (see FAQ for details).
     80* Fixed bug: now if there is no attachment, the empty array is not saved to the database.
    6381
    6482= 2.3.1 =
Note: See TracChangeset for help on using the changeset viewer.