[wp-hackers] Problem with admin AJAX not executing plugin code
Dion Hulse (dd32)
wordpress at dd32.id.au
Mon Apr 4 15:02:17 UTC 2011
Ajax requests do not triger admin_menu, therefore I'm guessing your action
is never hooked. perhaps you should try admin_init instead?
On 5 April 2011 00:57, Jake McMurchie <jake.mcmurchie at googlemail.com> wrote:
> Hello all.
>
> I'm having a problem whereby the AJAX managing comments in the admin
> area is preventing my plugin code from running.
>
> To complement the 'Easy Comment Uploads' plugin (thanks to Tom Wright)
> I'm writing a plugin that will convert an uploaded file (notionally
> attached to a comment) to a post attachment when the comment is
> approved (and will remove it when the comment is unapproved, trashed,
> etc.)
>
> Briefly my plugin should work as follows:
> - the ECU plugin uploads files to a wp-content/uploads/comments folder
>
> - when 'approve' clicked:
> - the file is copied to the wp-content/uploads/ folder
> - converted to a post with post_type=attachment, thumbnails
> created (using wp_insert_attachment etc.)
> - comment meta created to link comment to new post attachment
>
> - when 'unapproved/spam/trash' clicked:
> - post attachment deleted, thumbnails etc deleted, copy of the
> file in /uploads/ is deleted (using wp_delete_attachment)
>
> My code works fine if I disable javascript on the admin
> edit-comments.php, but doesn't if javascript is on (the default).
>
> Any help or suggestions much appreciated. Code appended below. Thanks.
>
> Jake
>
> add_action( 'admin_menu', 'jm_comment_attachment_convertor' );
>
> function jm_comment_attachment_convertor() {
> add_action( 'wp_set_comment_status', 'jm_attach_file', 10, 2 );
> }
>
> function jm_attach_file( $comment_id, $comment_status ) {
>
> // get comment info (associated page and content of comment)
> $comment = get_comment( $comment_id );
> $postid = $comment->comment_post_ID;
> $content = $comment->comment_content;
>
> // find upload directory and comment uploads directory
> $upldir = wp_upload_dir();
> $upldir = $upldir[ 'path' ].'/';
> $commupldir = $upldir.'comments/';
>
> // find out if comment upload previously converted to attachment
> $attachment_array = get_comment_meta( $comment_id,
> "comment_attachment_id" );
> if ($attachment_array)
> $attachment_id = $attachment_array[0];
>
> // if upload not yet converted, check for uploaded image
> if ( !$attachment_id ) {
>
> $imgo = strpos( $content, '[img]' );
> $imgc = strpos( $content, '[/img]' );
> if ( $imgo!==false && $imgc && $imgo<$imgc ) {
>
> // if img url complete and right way round
> $imgurl = substr( $content, $imgo+5, $imgc-5-$imgo
> );
>
> // get image filename
> $imgpath = pathinfo( $imgurl );
> $imgpath = $imgpath[ 'basename' ];
> // get path of image
> $imgpath = $commupldir . $imgpath;
>
> // get path of uploads directory to get path of
> image
> if ( file_exists( $imgpath )) {
> $img = $imgpath;
> $imgname = basename( $img );
> }
> }
> }
>
> // APPROVE NEW COMMENT AND CONVERT UPLOAD TO ATTACHMENT
>
> if ( $img && !$attachment_id && "approve"==$comment_status ) {
>
> // copy file to the main uploads folder
> if ( copy( $img, $upldir . $imgname ))
> $approvedimg = $upldir . $imgname;
>
> // if we have an image and want to approve it
> $wp_filetype = wp_check_filetype( $imgname, null );
> $attachment = array(
> 'post_mime_type' => $wp_filetype[ 'type' ],
> 'post_title' => preg_replace('/\.[^.]+$/', '', $imgname),
> 'post_content' => '',
> 'post_status' => 'inherit'
> );
> $attach_id = wp_insert_attachment( $attachment,
> $approvedimg, $postid );
>
> // include the image.php file to create thumbnails etc.
> // for the function wp_generate_attachment_metadata() to
> work
> require_once(ABSPATH . "wp-admin" . '/includes/image.php');
> $attach_data = wp_generate_attachment_metadata( $attach_id,
> $approvedimg );
> wp_update_attachment_metadata( $attach_id, $attach_data );
>
> // make a note of the attachment id against the comment so
> we can
> delete the attachment if the comment is unapproved
> add_comment_meta( $comment_id, "comment_attachment_id",
> $attach_id );
> }
>
> // UNAPPROVE APPROVED COMMENT (DELETE ATTACHMENT BUT NOT ORIGINAL
> FILE)
>
> else if ( $attachment_id && ( "hold"==$comment_status ||
> "spam"==$comment_status || "trash"==$comment_status )) {
>
> // delete the attachment
> wp_delete_attachment( $attachment_id, $force_delete = false
> );
>
> // delete the comment meta linking to this attachment
> delete_comment_meta( $comment_id, "comment_attachment_id" );
> }
>
> // DELETE NEW/UNAPPROVED/SPAMMED/TRASHED COMMENT AND DELETE ORIGINAL
> FILE
>
> else if ( $img && !$attachment_id && "delete"==$comment_status ) {
> // ACTION 'wp_set_comment_status' NOT CALLED WHEN PERMANENT
> DELETE??? looks like it in wp-includes/comment.php
> // move the file to the deleted directory
> rename( $img, $deleteddir . $imgname );
> }
> }
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>
More information about the wp-hackers
mailing list