ID ); return (int) $existing->ID; } $id = wp_insert_post( array( 'post_type' => 'page', 'post_status' => 'publish', 'post_title' => __( 'Guest book', 'stubli' ), 'post_name' => 'guest-book', 'post_content' => '
' . esc_html__( 'Thanks for stopping by. Say hello, share a tip, or tell me where you\'re reading from.', 'stubli' ) . '
', 'comment_status' => 'open', 'ping_status' => 'open', ) ); if ( $id && ! is_wp_error( $id ) ) { update_option( self::OPTION, (int) $id ); return (int) $id; } return 0; } /** * @return int */ public static function page_id() { return (int) get_option( self::OPTION, 0 ); } /** * @return string */ public static function url() { $id = self::page_id(); return $id ? (string) get_permalink( $id ) : ''; } /** * Approved guest book entries, pinned first, then newest. * * @return array[] */ public static function entries() { $id = self::page_id(); if ( ! $id ) { return array(); } $comments = get_comments( array( 'post_id' => $id, 'status' => 'approve', 'orderby' => 'comment_date_gmt', 'order' => 'DESC', ) ); $out = array_map( array( Moderation::class, 'public_comment' ), $comments ); usort( $out, static function ( $a, $b ) { return (int) $b['pinned'] - (int) $a['pinned']; } ); return $out; } /** * Pin or unpin an entry. * * @param int $comment_id Comment ID. * @param bool $pinned Pin state. * @return bool */ public static function pin( $comment_id, $pinned ) { $comment = get_comment( $comment_id ); if ( ! $comment || (int) $comment->comment_post_ID !== self::page_id() ) { return false; } if ( $pinned ) { update_comment_meta( $comment->comment_ID, 'stubli_pinned', 1 ); } else { delete_comment_meta( $comment->comment_ID, 'stubli_pinned' ); } return true; } /** * The welcome line at the top of the guest book, as plain text. * * @return string */ public static function intro() { $id = self::page_id(); return $id ? trim( html_entity_decode( wp_strip_all_tags( (string) get_post_field( 'post_content', $id ) ), ENT_QUOTES ) ) : ''; } /** * Change the welcome line. * * @param string $text Text, up to 300 characters. * @return string|\WP_Error */ public static function set_intro( $text ) { $id = self::page_id(); if ( ! $id ) { return new \WP_Error( 'stubli_no_guestbook', __( 'Your guest book page isn\'t there any more.', 'stubli' ), array( 'status' => 404 ) ); } $text = mb_substr( trim( sanitize_textarea_field( (string) $text ) ), 0, 300 ); wp_update_post( array( 'ID' => $id, 'post_content' => '' === $text ? '' : '' . esc_html( $text ) . '
', ) ); return self::intro(); } /** * Take an entry off the guest book (to the bin, so it can come back). * * @param int $comment_id Comment ID. * @param bool $restore Bring it back instead. * @return bool */ public static function remove( $comment_id, $restore = false ) { $comment = get_comment( $comment_id ); if ( ! $comment || (int) $comment->comment_post_ID !== self::page_id() ) { return false; } return (bool) ( $restore ? wp_untrash_comment( $comment->comment_ID ) : wp_trash_comment( $comment->comment_ID ) ); } }