0 ? get_post( $post_id ) : null; if ( ! $post instanceof \WP_Post || ! in_array( $post->post_type, self::TYPES, true ) ) { return null; } $lang = '' !== $lang ? Names::match( $lang ) : ''; if ( '' === $lang ) { return $post; } $translations = Providers::current()->post_translations( $post->ID ); if ( array() === $translations ) { return $post; } foreach ( $translations as $code => $id ) { if ( Language::normalize( (string) $code ) === $lang ) { $found = get_post( (int) $id ); return $found instanceof \WP_Post && $found->post_type === $post->post_type ? $found : null; } } return null; } public static function missing( int $post_id, string $lang ): bool { $post = $post_id > 0 ? get_post( $post_id ) : null; return $post instanceof \WP_Post && in_array( $post->post_type, self::TYPES, true ) && null === self::resolve( $post_id, $lang ); } public static function words( string $content ): int { $text = wp_strip_all_tags( strip_shortcodes( (string) preg_replace( '//s', ' ', $content ) ) ); $text = html_entity_decode( $text, ENT_QUOTES | ENT_HTML5, 'UTF-8' ); $n = preg_match_all( '/[\p{L}\p{N}]+(?:[\'’\-][\p{L}\p{N}]+)*/u', $text ); return false === $n ? 0 : $n; } public static function excerpt( \WP_Post $post ): string { $own = trim( wp_strip_all_tags( (string) $post->post_excerpt ) ); if ( '' !== $own ) { return $own; } $text = wp_strip_all_tags( strip_shortcodes( (string) preg_replace( '//s', ' ', (string) $post->post_content ) ) ); return wp_trim_words( html_entity_decode( $text, ENT_QUOTES | ENT_HTML5, 'UTF-8' ), self::EXCERPT_WORDS, '…' ); } public static function elementor_edits( string $post_type ): bool { if ( ! defined( 'ELEMENTOR_VERSION' ) ) { return false; } $types = get_option( 'elementor_cpt_support', array( 'page', 'post' ) ); return is_array( $types ) && in_array( $post_type, $types, true ); } public static function built_with_elementor( int $post_id ): bool { return defined( 'ELEMENTOR_VERSION' ) && 'builder' === (string) get_post_meta( $post_id, '_elementor_edit_mode', true ); } public static function summary( \WP_Post $post, string $lang, int $user_id ): array { $type = get_post_type_object( $post->post_type ); $rest_base = null !== $type && $type->show_in_rest ? ( is_string( $type->rest_base ) && '' !== $type->rest_base ? $type->rest_base : $post->post_type ) : ''; $elementor = self::elementor_edits( $post->post_type ) && user_can( $user_id, 'edit_post', $post->ID ); $own_lang = Providers::current()->post_language( $post->ID ); return array( 'post_id' => $post->ID, 'type' => $post->post_type, 'lang' => '' !== $own_lang ? Language::normalize( $own_lang ) : ( '' !== $lang ? $lang : Names::default_language() ), 'exists' => true, 'title' => trim( wp_strip_all_tags( $post->post_title ) ), 'status' => $post->post_status, 'excerpt' => self::excerpt( $post ), 'own_excerpt' => '' !== trim( (string) $post->post_excerpt ), 'words' => self::words( (string) $post->post_content ), 'empty' => '' === trim( (string) $post->post_content ), 'modified' => mysql_to_rfc3339( (string) $post->post_modified_gmt ), 'frame_url' => ContentFrame::url( $post->ID ), 'edit_url' => (string) get_edit_post_link( $post->ID, 'raw' ), 'elementor_url' => $elementor ? add_query_arg( array( 'post' => $post->ID, 'action' => 'elementor', ), admin_url( 'post.php' ) ) : '', 'elementor_built' => self::built_with_elementor( $post->ID ), 'rest_path' => '' !== $rest_base ? '/wp/v2/' . $rest_base . '/' . $post->ID : '', 'can_admin' => user_can( $user_id, 'edit_posts' ) || user_can( $user_id, Access::PAGE_CAP ), ); } public const PREVIEW_MAX = 200000; public static function preview_html( \WP_Post $post ): string { $content = (string) $post->post_content; if ( '' === trim( $content ) ) { return ''; } $previous = $GLOBALS['post'] ?? null; $GLOBALS['post'] = $post; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- put back below. try { $html = has_blocks( $content ) ? do_blocks( $content ) : wpautop( $content ); $html = do_shortcode( shortcode_unautop( $html ) ); } catch ( \Throwable $e ) { $html = wpautop( esc_html( wp_strip_all_tags( $content ) ) ); } finally { $GLOBALS['post'] = $previous; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited -- the caller's. } $html = wp_kses_post( (string) preg_replace( '//s', '', $html ) ); return strlen( $html ) > self::PREVIEW_MAX ? wp_kses_post( force_balance_tags( substr( $html, 0, self::PREVIEW_MAX ) ) ) : trim( $html ); } public static function missing_summary( \WP_Post $source, string $lang ): array { return array( 'post_id' => 0, 'source' => $source->ID, 'type' => $source->post_type, 'lang' => $lang, 'exists' => false, 'title' => trim( wp_strip_all_tags( $source->post_title ) ), 'edit_url' => (string) get_edit_post_link( $source->ID, 'raw' ), ); } }