Skip to content

Instantly share code, notes, and snippets.

@glueckpress
Created February 8, 2017 13:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glueckpress/054d16ace70abf4bfb81ab7b6e2be6bc to your computer and use it in GitHub Desktop.
Save glueckpress/054d16ace70abf4bfb81ab7b6e2be6bc to your computer and use it in GitHub Desktop.
[WordPress] Ask me anything on post #42. Auto-approves comments and replies automatically on post ID 42 until a given date. CUSTOM EDITS REQUIRED!
<?php
/**
* Plugin Name: AMA 42
* Description: Ask me anything on post 42.
* Version: 2017-02-08
* Author: Caspar Hübinger
* Author URI: https://profiles.wordpress.org/glueckpress
* License: GNU General Public License v3 or later
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
*/
defined( 'ABSPATH' ) or die( 'You know better.' );
/**
* Author slug to exclude from auto-replies.
*
* EDIT THIS TO YOUR OWN USER NAME!!!
* If you leave this as is, you’re in for a comment flood.
*/
if ( ! defined( 'AMA42_AUTHOR' ) )
define( 'AMA42_AUTHOR', 'caspar' );
/**
* Date/time to end all plugin functionality.
*
* EDIT THIS TO ANY SUITABLE DATE IN THE FUTURE.
* If you’re in a different timezone than Europe/Berlin, the function where
* this gets applied will need another parameter.
*/
if ( ! defined( 'AMA42_END_DATE' ) )
define( 'AMA42_END_DATE', '2017-02-09 00:00:00' );
/**
* Target this post ID only.
*/
if ( ! defined( 'AMA42_TARGET_POST_ID' ) )
define( 'AMA42_TARGET_POST_ID', 42 );
/* NO MORE EDITS REQUIRED BELOW THIS LINE. */
/**
* Do stuff, or not.
*
* @return bool False when bailing out, else true.
*/
function ama42() {
// Late to the party? Stop here.
if ( ama42__is_it_yet( AMA42_END_DATE ) )
return false;
// Auto-approve comments.
add_filter( 'pre_comment_approved', 'ama42__auto_approve_comments', 10, 2 );
// Post replies.
add_action( 'comment_post', 'ama42__post_reply', 10, 3 );
return true;
}
add_action( 'plugins_loaded', 'ama42' );
/**
* Auto-post a reply to any incoming comment.
*
* @param int $comment_id Incoming comment ID
* @param int $comment_approved Incoming comment approved status
* @param array $commentdata Incoming comment data
* @return int Comment ID
*/
function ama42__post_reply( $comment_id, $comment_approved, $commentdata ) {
if ( AMA42_TARGET_POST_ID !== absint( $commentdata['comment_post_ID'] ) )
return $comment_id;
if ( AMA42_AUTHOR === $commentdata['comment_author'] )
return $comment_id;
$reply_author_data = get_user_by( 'slug', AMA42_AUTHOR );
$reply_data = array(
'comment_post_ID' => AMA42_TARGET_POST_ID,
'comment_approved' => 1,
'comment_author' => AMA42_AUTHOR,
'comment_author_email' => $reply_author_data->user_email,
'comment_content' => ama42__reply_content( $commentdata ),
'comment_parent' => $comment_id
);
wp_insert_comment( $reply_data );
return $comment_id;
}
/**
* Auto-approve incoming comments for this specific post.
*
* @param int $approved Incoming comment approved status
* @param array $commentdata Incoming comment data
* @return int Comment approved status
*/
function ama42__auto_approve_comments( $approved, $commentdata ) {
if ( AMA42_TARGET_POST_ID === absint( $commentdata['comment_post_ID'] ) )
return 1;
return $approved;
}
/**
* Automatic replies.
* Essentially Hello Dolly, with some minor tweaks.
*
* @param array $commentdata Incoming comment data
* @return string Comment content to reply with
*/
function ama42__reply_content( $commentdata ) {
$comment_content = $commentdata['comment_content'];
preg_match('/(.*)(w?)\?/', $comment_content, $questions );
if ( empty( $questions ) )
return 'Is that’ a question?';
$replies = array();
$replies[] = '42.';
$replies[] = '42!';
$replies[] = '42!!';
$replies[] = 'Well, 42.';
$replies[] = '42 afaik.';
$replies[] = 'Forty-two.';
$replies[] = 'Clearly 42.';
$replies[] = 'For. Ty. Two.';
$replies[] = 'How about 42?';
$replies[] = 'This is so 42.';
$replies[] = 'Heck yeah, 42!!';
$replies[] = '42, essentially.';
$replies[] = 'Hmmm, I’’d say… 42?';
$replies[] = 'Good question! But 42.';
$replies[] = '42——you knew it, didn’t you?';
$replies[] = 'Have you actually… well, 42.';
$replies[] = 'Hey, you here! :) 42 for sure!';
$replies[] = '42. Who would have thought so?';
$replies[] = 'I wonder sometimes, but mostly 42.';
$replies[] = 'Would you be mad at me if I said 42?';
$replies[] = 'Is that even a question? 42, obviously!';
$replies[] = 'Glad you’’re asking. Once and for all: 42.';
$replies[] = 'This is even beyond 42. Wait, it can’’t be.';
$replies[] = 'Can’’t think of anything but 42 for this one.';
$replies[] = 'If you don’’t know, who would? 42, of course!';
$replies[] = 'I’ would have thought otherwise myself, but 42.';
$replies[] = 'Tough one.<br>&#160;<br>&#160;<br>&#160;<br>42?';
$replies[] = 'Not sure if I understand the question, but let’’s go with 42. :)';
shuffle( $replies );
$reply = wptexturize( $replies[ mt_rand( 0, count( $replies ) - 1 ) ] );
return $reply;
}
/**
* Check if we’re at a given date yet.
*
* Could probably be done in more WordPress-ish fashion.
* (Truth is, I had this in my cupboard and didn’t feel like putting in
* any extra work to fluff it up WordPress-style.)
*
* @param string $is_it_yet Timestamp to figure out
* @param string $time_zone Time zone to be applied
* @return bool TRUE if $is_it_yet is in the past, FALSE otherwise
*/
function ama42__is_it_yet( $is_it_yet = '2016-01-01 00:00:01', $time_zone = 'Europe/Berlin' ) {
date_default_timezone_set( $time_zone );
$earliest = new DateTime( $is_it_yet );
$current = new DateTime();
$diff = date_diff( $earliest, $current );
// DateInterval->invert is 1 if the interval represents a negative time period; 0 otherwise.
return ( 0 === $diff->invert ) ? true : false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment