Repository navigation
Expand file tree
/
Copy pathembed-post.php
More file actions
139 lines (116 loc) · 3.78 KB
/
Copy pathembed-post.php
File metadata and controls
139 lines (116 loc) · 3.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/**
* Plugin Name: Embed Post
* Description: Embed a post, showing its post title, category, featured images, and meta description.
* Requires at least: 6.2
* Requires PHP: 7.0
* Version: 1.0.0
* Author: Cole Welsome
* License: GPL-2.0-or-later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: embed-post
*
* @package create-block
*/
/**
* Registers the block using the metadata loaded from the `block.json` file.
* Behind the scenes, it registers also all assets so they can be enqueued
* through the block editor in the corresponding context.
*
* @see https://developer.wordpress.org/reference/functions/register_block_type/
*/
class embed_post {
public function __construct()
{
// Create our Dynamic Block
add_action('init', array($this, 'create_block_embed_post_block_init'));
}
public function create_block_embed_post_block_init() {
register_block_type(__DIR__ . '/build');
}
// Find our post's values
private function fetch_post($attributes)
{
$post = get_post($attributes);
// Get the list of categories our post belongs to
$cat_list = wp_get_object_terms($post->ID, 'category', array('fields' => 'names'));
// Implode the array of categories into a nice string
if (! empty($cat_list)) {
$categories = implode(' | ' , $cat_list);
} else {
$categories = '';
}
// Grab our post data
// TODO Maybe switch over to WP_Query() as it might be faster to do this in one go
$details = array(
'title' => $post->post_title,
'categories' => $categories,
'featured_image' => get_the_post_thumbnail_url($attributes),
'excerpt' => get_the_excerpt($post->ID),
'permalink' => get_post_permalink($post->ID)
);
return $details;
}
// Build an array of post values to display on render.php
public function get_embeded_post($attributes)
{
if (! empty($attributes) && 0 !== $attributes['selectedPost']) {
$post = $this->fetch_post($attributes['selectedPost']);
} else{
$post = array(
'title' => 'Lorem Ipsum',
'categories' => 'Llabore | Dolore | Aliqua',
'featured_image' => plugins_url( '/public/images/', __FILE__ ) . 'stock-image.png',
'excerpt' => 'Excepteur sint occaecat cupidatat non proident, sunt in culpa qui.',
'permalink' => ''
);
}
return $post;
}
}
$embed_post = new embed_post();
/*
function create_block_embed_post_block_init() {
register_block_type( __DIR__ . '/build' );
}
add_action( 'init', 'create_block_embed_post_block_init' );
// Find our post's values
function fetch_post($attributes)
{
$post = get_post($attributes);
// Get the list of categories our post belongs to
$cat_list = wp_get_object_terms($post->ID, 'category', array('fields' => 'names'));
// Implode the array of categories into a nice string
if (! empty($cat_list)) {
$categories = implode(' | ' , $cat_list);
} else {
$categories = '';
}
// Grab our post data
// TODO Maybe switch over to WP_Query() as it might be faster to do this in one go
$details = array(
'title' => $post->post_title,
'categories' => $categories,
'featured_image' => get_the_post_thumbnail_url($attributes),
'excerpt' => get_the_excerpt($post->ID),
'permalink' => get_post_permalink($post->ID)
);
return $details;
}
// Build an array of post values to display on render.php
function get_embeded_post($attributes)
{
if (! empty($attributes) && 0 !== $attributes['selectedPost']) {
$post = fetch_post($attributes['selectedPost']);
} else{
$post = array(
'title' => 'Lorem Ipsum',
'categories' => 'Llabore | Dolore | Aliqua',
'featured_image' => plugins_url( '/images/', __FILE__ ) . 'stock-image.png',
'excerpt' => 'Excepteur sint occaecat cupidatat non proident, sunt in culpa qui.',
'permalink' => ''
);
}
return $post;
}
*/