Commit 0aabf639 authored by roigada's avatar roigada
Browse files

Merge branch 'feature/comments' into roigadev

parents 22c91974 aa81ab86
......@@ -166,6 +166,14 @@ $imcpress_post_types['post'] = array(
'taxonomy_slug' => array( 'wire', 'post_tag', 'place_tag' ),
);
$imcpress_disapproved_comments = array(
'wire' => array( 'global' ),
'post_type' => array(),
// 'post_type' => array( 'imcpress_tumble' ), // not needed as type doesn't support comments
'status' => array( 'refused', 'debate', 'pending'),
);
if ( file_exists( IMCPRESS_CONFIG_DIR . 'imcpress.php' ) ) {
include IMCPRESS_CONFIG_DIR . 'imcpress.php';
}
<?php
namespace IMCPress\Comment;
defined( 'ABSPATH' ) || die( 'Invalid request.' );
class Comment {
public static array $disapproved = [];
public static function set_disapproved_settings($args): void {
self::$disapproved = $args;
}
public static function plugins_loaded(): void {
// Comment form
add_filter( 'comment_form_default_fields', array( __CLASS__, 'default_comment_fields') );
add_filter( 'comment_form_fields', array( __CLASS__, 'comment_form_fields') );
add_filter( 'pre_comment_author_url', array( __CLASS__, 'remove_author_url' ) );
add_filter( 'comment_form_field_author', array( __CLASS__, 'logged_author_field') );
add_action( 'comment_form_logged_in_after', array( __CLASS__, 'logged_in_text'));
// Set status
add_filter( 'comments_open', array( __CLASS__, 'comments_opened'), 10, 2 );
add_action( 'comment_post', array( __CLASS__, 'update_comment_status'), 10, 3 );
add_action( 'save_post', array( __CLASS__, 'set_default_post_comment_status'), 15, 3 );
// Admin comments columns
add_filter( 'manage_edit-comments_columns', array( __CLASS__, 'set_comments_columns') );
add_action( 'manage_comments_custom_column', array( __CLASS__, 'title_column_content') , 10, 2);
add_filter( 'sanitize_email', array( __CLASS__, 'unsanitize_email'), 1, 2);
}
public static function default_comment_fields($fields) {
unset($fields['url']);
unset($fields['cookies']);
$fields['title'] = '<label for="title">Titre </label><input id="title" type="text" name="title" size="30" maxlength="245">';
return $fields;
}
public static function comment_form_fields( array $fields ): array {
// Set comment field last
$comment_field = $fields['comment'];
unset( $fields['comment'] );
$fields['comment'] = $comment_field;
// Override logged in form, cause wp only would display content field
if ( is_user_logged_in() )
{
$comment_field_keys = array_diff(array_keys($fields), array('comment'));
$first_field = reset($comment_field_keys);
$last_field = end($comment_field_keys);
foreach ($fields as $name => $field)
{
if ('comment' === $name)
{
echo apply_filters('comment_form_field_comment', $field);
}
else
{
if ($first_field === $name)
{
do_action('comment_form_before_fields');
}
echo apply_filters("comment_form_field_{$name}", $field) . "\n";
if ($last_field === $name)
{
do_action('comment_form_after_fields');
}
}
}
return array();
}
return $fields;
}
public static function set_default_post_comment_status( int $id, \WP_Post $post, bool $update): void {
if ( 'auto-draft' == $post->post_status)
{
return;
}
$status = self::get_custom_post_comment_status($post, true);
remove_action( 'save_post', array( __CLASS__, 'set_default_post_comment_status'), 15 );
wp_update_post( array(
'ID' => $id,
'comment_status' => $status ? 'open':'closed',
));
add_action( 'save_post', array( __CLASS__, 'set_default_post_comment_status'), 15, 3 );
}
public static function comments_opened( bool $open, int $post_id): bool {
// not if refused + !post_type_supports($type, 'comments')
$post = get_post( $post_id);
if ( null == $post)
{
return false;
}
if ( 'refused' == $post->post_status )
{
return false;
}
if ( !post_type_supports( $post->post_type, 'comments') )
{
return false;
}
return true;
}
public static function update_comment_status( int $comment_ID, $comment_approved, array $data): void {
$post = get_post( $data['comment_post_ID'] );
if (null === $post)
{
return;
}
$anon_author = __( 'Anonymous' );
// Make sure no comment cookie is set
if ( isset( $_POST['wp-comment-cookies-consent'] ) )
{
unset( $_POST['wp-comment-cookies-consent'] );
}
// Get default approval status
$status = self::get_custom_post_comment_status($post);
// Always approved for modadmins
if ( 0 != $data['user_ID'] )
{
$user = get_user_by( 'ID', $data['user_ID'] );
if ( user_can( $user, 'moderate_comments' ) )
{
$status = 1;
}
$data['comment_author'] = isset($_POST['author']) && !empty($_POST['author']) ? $_POST['author'] : $anon_author;
$data['comment_author_email'] = $_POST['email'] ?? '';
}
// Save title meta
if ( isset($_POST['title']) )
{
$title = (string) $_POST['title'];
$title = sanitize_text_field($title);
add_comment_meta( $comment_ID, 'imcpress_comment_title', $title);
}
wp_update_comment( array(
'comment_ID' => $comment_ID,
'comment_approved' => $status,
'comment_author' => $data['comment_author'],
// If logged in user emptied author field, treat it as anon and set user_id to 0
'user_id' => $data['comment_author'] == $anon_author ? 0 : $data['user_id'],
));
}
public static function get_custom_post_comment_status( \WP_Post $post, bool $publishing = false): int {
if ( 'closed' === $post->comment_status )
{
return 0;
}
if ( 'post' === $post->post_type )
{
$wire = get_the_terms( $post, 'wire' );
if ( !empty($wire) )
{
$wire = $wire[0]->slug;
}
else // If for whatever reason, a wire isn't set?
{
return 0;
}
if ( in_array( $wire, self::$disapproved['wire'], true ) )
{
return 0;
}
}
else
{
if ( in_array( $post->post_type, self::$disapproved['post_type'], true ) )
{
return 0;
}
}
if ( !$publishing && in_array( $post->post_status, self::$disapproved['status'], true ) )
{
return 0;
}
return 1;
}
public static function remove_author_url(): string {
return '';
}
public static function logged_author_field( $field ) {
if ( is_user_logged_in() )
{
$user = wp_get_current_user();
$field = str_replace('value=""', 'value="'.$user->display_name.'"', $field);
}
return $field;
}
public static function logged_in_text(): void {
echo '"'.__( 'Anonymous' ).'" will be set if name is empty, whatever else will still get your user ID associated wih comment';
}
public static function set_comments_columns($columns) {
$add['author'] = $columns['author'];
$add['comment'] = $columns['comment'];
$add['imc_com_title'] = 'Title';
unset($columns['author']);
unset($columns['comment']);
return array_merge( $add, $columns );
}
public static function title_column_content( string $column, int $id): void{
if ($column == 'imc_com_title')
{
echo get_comment_meta( $id, 'imcpress_comment_title', true);
}
}
public static function unsanitize_email( string $sanitized, string $email): string {
if ( 'wp-comments-post.php' == basename($_SERVER['REQUEST_URI']) )
{
remove_filter( 'sanitize_email', 'sanitize_email');
$email = sanitize_text_field($email);
}
return $email;
}
}
\ No newline at end of file
<?php
namespace IMCPress\Comment;
defined( 'ABSPATH' ) || die( 'Invalid request.' );
class Walker extends \Walker_Comment {
// Simply copied and adjusted from parent
protected function html5_comment( $comment, $depth, $args ) {
$tag = ( 'div' === $args['style'] ) ? 'div' : 'li';
$commenter = wp_get_current_commenter();
$show_pending_links = ! empty( $commenter['comment_author'] );
if ( $commenter['comment_author_email'] ) {
$moderation_note = __( 'Your comment is awaiting moderation.' );
} else {
$moderation_note = __( 'Your comment is awaiting moderation. This is a preview; your comment will be visible after it has been approved.' );
}
?>
<<?php echo $tag; ?> id="comment-<?php comment_ID(); ?>" <?php comment_class( $this->has_children ? 'parent' : '', $comment ); ?>>
<article id="div-comment-<?php comment_ID(); ?>" class="comment-body">
<footer class="comment-meta">
<div class="comment-author vcard">
<?php
$comment_author = get_comment_author( $comment );
if ( '0' == $comment->comment_approved && ! $show_pending_links ) {
$comment_author = get_comment_author( $comment );
}
$title = get_comment_meta($comment->comment_ID, 'imcpress_comment_title', true);
$title = empty($title) ? '' : ' <b>'.$title.'</b>';
printf(
// Fix: i18n namespace?
/* translators: %s: Comment author link. */
__( '%s <span class="says">says:</span>%s' ),
sprintf( '<b class="fn">%s</b>', $comment_author ),
$title
);
?>
</div><!-- .comment-author -->
<div class="comment-metadata">
<?php
printf(
'<a href="%s"><time datetime="%s">%s</time></a>',
esc_url( get_comment_link( $comment, $args ) ),
get_comment_time( 'c' ),
sprintf(
/* translators: 1: Comment date, 2: Comment time. */
__( '%1$s at %2$s' ),
get_comment_date( '', $comment ),
get_comment_time()
)
);
$email = get_comment_author_email();
$email = empty($email) ? '' : ' - '.$email.'';
echo $email;
?>
</div><!-- .comment-metadata -->
<?php if ( '0' == $comment->comment_approved ) : ?>
<em class="comment-awaiting-moderation"><?php echo $moderation_note; ?></em>
<?php endif; ?>
</footer><!-- .comment-meta -->
<div class="comment-content">
<?php comment_text(); ?>
</div><!-- .comment-content -->
<?php
if ( '1' == $comment->comment_approved || $show_pending_links ) {
comment_reply_link(
array_merge(
$args,
array(
'add_below' => 'div-comment',
'depth' => $depth,
'max_depth' => $args['max_depth'],
'before' => '<div class="reply">',
'after' => '</div>',
)
)
);
}
?>
</article><!-- .comment-body -->
<?php
}
}
\ No newline at end of file
......@@ -33,6 +33,7 @@ class IMCPress {
Rewrite::plugins_loaded();
User\Avatar::plugins_loaded();
User\Group::plugins_loaded();
Comment\Comment::plugins_loaded();
}
public static function nonce_user_logged_out(): string {
......@@ -54,6 +55,8 @@ class IMCPress {
require_once IMCPRESS_PATH . 'config/imcpress.php';
Comment\Comment::set_disapproved_settings($imcpress_disapproved_comments);
$imcpress_post_types = self::add_author_metabox( $imcpress_post_types, $contributor_login );
do_action( 'setup_statuses', $imcpress_statuses );
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment