get_id(); } else { return $object->id; } } public static function get_property( $object, $property ) { if ( method_exists( $object, 'get_' . $property ) ) { return call_user_func( array( $object, 'get_' . $property ) ); } else { return $object->{$property}; } } /** * @param WP_Post|string $item * @return array|false */ public static function get_image_dimensions( $item ) { if ( is_string( $item ) ) { $id = attachment_url_to_postid( $item ); $item = get_post( $id ); } if ( ! is_a( $item, 'WP_Post' ) ) { return false; } $meta = wp_get_attachment_metadata( $item->ID ); if ( empty( $meta ) ) { $attachment_path = get_attached_file( $item->ID ); $attach_data = wp_generate_attachment_metadata( $item->ID, $attachment_path ); wp_update_attachment_metadata( $item->ID, $attach_data ); // Wrap the data in a response object $meta = wp_get_attachment_metadata( $item->ID ); } if ( isset( $meta['width'], $meta['height'] ) ) { return array( 'width' => $meta['width'], 'height' => $meta['height'], ); } else { return false; } } /** * @param WP_Post $post * @param string $template * * @return array */ public static function get_post_widget( $post, $template = 'post-list-1' ) { $GLOBALS['post'] = $post; setup_postdata( $post ); $featured_media_url = get_the_post_thumbnail_url( $post ); $response = array( 'view' => 'simple', 'template' => 'template-2', 'content' => self::prepare_excerpt_response( $post->post_excerpt ), 'author' => get_the_author_meta( 'display_name', $post->post_author ), 'action' => array( 'type' => 'OPEN_IN_APP_PAGE', 'params' => array( 'id' => 'wp/posts/' . $post->ID, ), ), ); if ( $featured_media_url ) { $response['image'] = $featured_media_url; } return $response; } public static function decode_html( $string ) { return wp_strip_all_tags( html_entity_decode( $string ) ); } public static function prepare_excerpt_response( $excerpt ) { if ( post_password_required() ) { return __( 'There is no excerpt because this is a protected post.' ); } /** This filter is documented in wp-includes/post-template.php */ $excerpt = apply_filters( 'the_excerpt', apply_filters( 'get_the_excerpt', $excerpt ) ); if ( empty( $excerpt ) ) { return ''; } return self::decode_html( $excerpt ); } }