errors = AnyCommentSocialAuth::getErrors(); } /** * Custom logout URL to redirect user back to post on logout. * * @param string $logout_url Generated logout URL by WordPress. * * @since 0.0.52 * * @return string */ function logout_redirect( $logout_url ) { $permaLink = get_permalink(); if ( $permaLink !== false && is_singular() ) { $query = parse_url( $logout_url, PHP_URL_QUERY ); $hashedPermalink = sprintf( '%s#%s', $permaLink, 'comments' ); if ( $permaLink ) { $logout_url .= sprintf( '%sredirect_to=%s', ( $query ? '&' : '?' ), $hashedPermalink ); } } return $logout_url; } /** * Make custom template for comments. * @return string */ public function override_comment( $atts ) { $params = shortcode_atts( array( 'include' => false, ), $atts ); $isInclude = $params['include']; if ( ! post_password_required() && comments_open() ) { wp_enqueue_script( 'anycomment-react', AnyComment()->plugin_url() . '/static/js/main.min.js', [], md5( AnyComment()->version ) ); if ( AnyCommentGenericSettings::isDesignCustom() ) { $url = AnyCommentGenericSettings::getCustomDesignStylesheetUrl(); wp_enqueue_style( 'anycomment-custom-styles', $url, [], md5( AnyComment()->version ) ); } else { wp_enqueue_style( 'anycomment-styles', AnyComment()->plugin_url() . '/static/css/main.min.css', [], md5( AnyComment()->version ) ); } if ( strpos( AnyCommentGenericSettings::getDesignFontFamily(), 'Noto-Sans' ) !== false ) { wp_enqueue_style( 'anycomment-google-font', 'https://fonts.googleapis.com/css?family=Noto+Sans:400,700&subset=cyrillic' ); } $postId = get_the_ID(); $postPermalink = get_permalink( $postId ); wp_localize_script( 'anycomment-react', 'anyCommentApiSettings', [ 'postId' => $postId, 'nonce' => wp_create_nonce( 'wp_rest' ), 'locale' => get_locale(), 'restUrl' => esc_url_raw( rest_url( 'anycomment/v1/' ) ), 'commentCount' => ( $res = get_comment_count( $postId ) ) !== null ? (int) $res['all'] : 0, 'errors' => $this->errors, 'urls' => [ 'logout' => wp_logout_url(), 'postUrl' => $postPermalink, ], 'rating' => [ 'value' => AnyCommentRating::get_average_by_post( $postId ), 'count' => AnyCommentRating::get_count_by_post( $postId ), 'hasRated' => AnyCommentRating::current_user_rated( $postId, get_current_user_id() ) ], // Options from plugin 'options' => [ 'limit' => AnyCommentGenericSettings::getPerPage(), 'isCopyright' => AnyCommentGenericSettings::isCopyrightOn(), 'socials' => anycomment_login_with( false, get_permalink( $postId ) ), 'sort_order' => AnyCommentGenericSettings::getSortOrder(), 'guestInputs' => AnyCommentGenericSettings::getGuestFields( true ), 'isShowProfileUrl' => AnyCommentGenericSettings::isShowProfileUrl(), 'isShowImageAttachments' => AnyCommentGenericSettings::isShowImageAttachments(), 'isShowVideoAttachments' => AnyCommentGenericSettings::isShowVideoAttachments(), 'isShowTwitterEmbeds' => AnyCommentGenericSettings::isShowTwitterEmbeds(), 'userAgreementLink' => AnyCommentGenericSettings::getUserAgreementLink(), 'notifyOnNewComment' => AnyCommentGenericSettings::isNotifyOnNewComment(), 'intervalCommentsCheck' => AnyCommentGenericSettings::getIntervalCommentsCheck(), 'isLoadOnScroll' => AnyCommentGenericSettings::isLoadOnScroll(), 'isFormTypeAll' => AnyCommentGenericSettings::isFormTypeAll(), 'isFormTypeGuests' => AnyCommentGenericSettings::isFormTypeGuests(), 'isFormTypeSocials' => AnyCommentGenericSettings::isFormTypeSocials(), 'isFileUploadAllowed' => AnyCommentGenericSettings::isFileUploadAllowed(), 'isGuestCanUpload' => AnyCommentGenericSettings::isGuestCanUpload(), 'fileMimeTypes' => AnyCommentGenericSettings::getFileMimeTypes(), 'fileLimit' => AnyCommentGenericSettings::getFileLimit(), 'fileMaxSize' => AnyCommentGenericSettings::getFileMaxSize(), 'fileUploadLimit' => AnyCommentGenericSettings::getFileUploadLimit(), 'isRatingOn' => AnyCommentGenericSettings::isRatingOn(), 'isReadMoreOn' => AnyCommentGenericSettings::isReadMoreOn(), 'isEditorOn' => AnyCommentGenericSettings::isEditorToolbarOn(), 'editorToolbarOptions' => AnyCommentGenericSettings::getEditorToolbarOptions(), 'reCaptchaOn' => AnyCommentIntegrationSettings::isRecaptchaOn(), 'reCaptchaUserAll' => AnyCommentIntegrationSettings::isRecaptchaUserAll(), 'reCaptchaUserGuest' => AnyCommentIntegrationSettings::isRecaptchaUserGuest(), 'reCaptchaUserAuth' => AnyCommentIntegrationSettings::isRecaptchaUserAuth(), 'reCaptchaSiteKey' => AnyCommentIntegrationSettings::getRecaptchaSiteKey(), 'reCaptchaTheme' => AnyCommentIntegrationSettings::getRecaptchaTheme(), 'reCaptchaPosition' => AnyCommentIntegrationSettings::getRecaptchaBadge(), ], 'user' => AnyCommentUser::getSafeUser(), 'i18' => [ 'error_generic' => __( "Oops, something went wrong...", "anycomment" ), 'loading' => __( 'Loading...', 'anycomment' ), 'load_more' => __( "Load more", "anycomment" ), 'button_send' => __( 'Send', 'anycomment' ), 'button_save' => __( 'Save', 'anycomment' ), 'button_reply' => __( 'Reply', 'anycomment' ), 'sorting' => __( 'Sorting', 'anycomment' ), 'sort_by' => __( 'Sort by', 'anycomment' ), 'sort_oldest' => __( 'oldest', 'anycomment' ), 'sort_newest' => __( 'newest', 'anycomment' ), 'reply_to' => __( 'reply to', 'anycomment' ), 'editing' => __( 'editing', 'anycomment' ), 'add_comment' => __( 'Your comment...', 'anycomment' ), 'no_comments' => __( 'No comments to display', "anycomment" ), 'footer_copyright' => __( 'Add Anycomment to your site', 'anycomment' ), 'reply' => __( 'Reply', 'anycomment' ), 'edit' => __( 'Edit', 'anycomment' ), 'delete' => __( 'Delete', 'anycomment' ), 'cancel' => __( 'Cancel', 'anycomment' ), 'quick_login' => __( 'Quick Login', 'anycomment' ), 'guest' => __( 'Guest', 'anycomment' ), 'login' => __( 'Login', 'anycomment' ), 'logout' => __( 'Logout', 'anycomment' ), 'new_comment_was_added' => __( 'New comment was added', 'anycomment' ), 'author' => __( 'Author', 'anycomment' ), 'name' => __( 'Name', 'anycomment' ), 'email' => __( 'Email', 'anycomment' ), 'website' => __( 'Website', 'anycomment' ), 'already_rated' => __( 'You have already rated', 'anycomment' ), 'accept_user_agreement' => sprintf( __( 'I accept the User Agreement', 'anycomment' ), AnyCommentGenericSettings::getUserAgreementLink(), ' target="_blank" ' ), 'upload_file' => __( 'Upload file', 'anycomment' ), 'file_upload_in_progress' => __( "Uploading...", 'anycomment' ), 'file_uploaded' => __( "Uploaded!", 'anycomment' ), 'file_too_big' => __( "File %s is too big", 'anycomment' ), 'file_limit' => sprintf( __( "You may upload %s file(s) at maximum", 'anycomment' ), AnyCommentGenericSettings::getFileLimit() ), 'file_not_selected_or_extension' => __( "No file selected or select proper extension", 'anycomment' ), 'read_more' => __( 'Read more', 'anycomment' ), 'show_less' => __( 'Show less', 'anycomment' ), 'login_with' => __( 'Login with', 'anycomment' ), 'or_as_guest' => sprintf( __( 'or as %sguest%s', 'anycomment' ), '', '' ), /** * Lightbox */ 'lighbox_close' => __( 'Close (Esc)', 'anycomment' ), 'lighbox_left_arrow' => __( 'Previous (Left arrow key)', 'anycomment' ), 'lighbox_right_arrow' => __( 'Next (Right arrow key)', 'anycomment' ), 'lighbox_image_count_separator' => __( ' of ', 'anycomment' ), ] ] ); } $path = ANYCOMMENT_ABSPATH . 'templates/comments.php'; if ( $isInclude ) { return anycomment_get_template( 'comments' ); } return $path; } /** * Get comments. * * @param null|int $postId Post ID to check comments for. Avoid then get_the_ID() will be used to get id. * @param int $limit Limit number of comments to load. * @param string $sort Sorting type. New or old. Default is new. * * @return array|null NULL when there are no comments for post. */ public function get_comments( $postId = null, $limit = null, $sort = null ) { if ( $limit === null || empty( $limit ) ) { $limit = AnyCommentGenericSettings::getPerPage(); } if ( $sort === null || ( $sort !== self::SORT_ASC && $sort !== self::SORT_DESC ) ) { $sort = self::SORT_DESC; } $options = [ 'post_id' => $postId === null ? get_the_ID() : $postId, 'parent' => 0, 'comment_status' => 1, 'number' => $limit, 'orderby' => 'comment_ID', 'order' => $sort ]; $comments = get_comments( $options ); return count( $comments ) > 0 ? $comments : null; } /** * Get parent child comments. * * @param int $commentId Parent comment id. * @param null|int $postId Post ID to check comments for. Avoid then get_the_ID() will be used to get id. * * @return array|null NULL when there are no comments for post. */ public function get_child_comments( $commentId, $postId = null ) { if ( $commentId === null ) { return null; } $comments = get_comments( [ 'parent' => $commentId, 'post_id' => $postId === null ? get_the_ID() : $postId ] ); return count( $comments ) > 0 ? $comments : null; } /** * Get comment count. * * @param int $post_id Post ID. * * @return string */ public function get_comment_count( $post_id ) { $count = get_comments_number( $post_id ); return sprintf( _nx( '%s comment', '%s comments', $count, 'REST API Comments Count', 'anycomment' ), number_format_i18n( $count ) ); } /** * Check whether it is too old to edit (update/delete) comment. * * @param WP_Comment $comment Comment to be checked. * @param int $minutes Number of minutes comment allow to be edited. * * Note: if `$minutes` is below 5, it will be set to 5 as it is the default value. * * @return bool */ public function is_old_to_edit( $comment, $minutes = 5 ) { $commentTime = strtotime( $comment->comment_date_gmt ); if ( (int) $minutes < 5 ) { $minutes = 5; } $secondsToEdit = (int) $minutes * 60; $currentUnixTimeMysql = strtotime( current_time( 'mysql', true ) ); return $currentUnixTimeMysql > ( $commentTime + $secondsToEdit ); } /** * Check whether current user has ability to edit comment. * * @param WP_Comment $comment * * @return bool */ public function can_edit_comment( $comment ) { if ( current_user_can( 'moderate_comments' ) || current_user_can( 'edit_comment', $comment->comment_ID ) ) { return true; } if ( $this->is_old_to_edit( $comment ) ) { return false; } $user = wp_get_current_user(); if ( ! $user instanceof WP_User ) { return false; } return (int) $user->ID === (int) $comment->user_id; } } endif;