|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by Brad Vincent. |
|
4
|
|
|
* Date: 04/03/2018 |
|
5
|
|
|
*/ |
|
6
|
|
|
if ( ! class_exists( 'FooGallery_Demo_Content_Generator' ) ) { |
|
7
|
|
|
|
|
8
|
|
|
class FooGallery_Demo_Content_Generator { |
|
|
|
|
|
|
9
|
|
|
function __construct() { |
|
|
|
|
|
|
10
|
|
|
//always show the menu |
|
11
|
|
|
add_action( 'foogallery_admin_menu_after', array( $this, 'add_menu' ) ); |
|
12
|
|
|
add_action( 'foogallery_extension_activated-demo-content', array( $this, 'add_menu' ) ); |
|
13
|
|
|
} |
|
14
|
|
|
|
|
15
|
|
|
function add_menu() { |
|
|
|
|
|
|
16
|
|
|
foogallery_add_submenu_page( __( 'Demo Content', 'foogallery' ), 'manage_options', 'foogallery-demo-content', array( |
|
17
|
|
|
$this, |
|
18
|
|
|
'render_view', |
|
19
|
|
|
) ); |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
function render_view() { |
|
23
|
|
|
require_once 'view-demo-content.php'; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
static function search( $query, $count = 20 ) { |
|
|
|
|
|
|
27
|
|
|
require_once 'includes/class-pixabay.php'; |
|
28
|
|
|
require_once 'includes/class-lorem-ipsum.php'; |
|
29
|
|
|
|
|
30
|
|
|
$client = new FooGallery_PixabayClient(); |
|
31
|
|
|
$key = apply_filters( 'foogallery_pixabay_key', '1843003-12be68cf2726df47797f19cd7' ); |
|
32
|
|
|
|
|
33
|
|
|
$results = $client->search( $key, $query, $count ); |
|
34
|
|
|
|
|
35
|
|
|
$hits = $results->hits; |
|
36
|
|
|
|
|
37
|
|
|
$lorem = new FooGallery_LoremIpsum(); |
|
38
|
|
|
|
|
39
|
|
|
$results = array(); |
|
40
|
|
|
|
|
41
|
|
|
//get random images |
|
42
|
|
|
$random_image_indexes = array_rand($hits, $count); |
|
43
|
|
|
foreach ( $random_image_indexes as $image_index ) { |
|
44
|
|
|
$image = $hits[$image_index]; |
|
45
|
|
|
if ( array_key_exists( 'largeImageURL', $image ) ) { |
|
46
|
|
|
$url = $image->largeImageURL; |
|
47
|
|
|
} else { |
|
48
|
|
|
$url = $image->webformatURL; |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
$thumb = $image->previewURL; |
|
52
|
|
|
|
|
53
|
|
|
$title = 'FooGallery Demo Image ' . $image->id; |
|
54
|
|
|
$caption_title = self::build_caption_title( $image->tags ); |
|
55
|
|
|
if ( false === $caption_title ) { |
|
56
|
|
|
$caption_title = $lorem->words( rand(3,5) ); |
|
57
|
|
|
} |
|
58
|
|
|
$caption_desc = $lorem->words( rand(8, 15) ); |
|
59
|
|
|
|
|
60
|
|
|
$results[] = array( |
|
61
|
|
|
'title' => $title, |
|
62
|
|
|
'caption' => $caption_title, |
|
63
|
|
|
'description' => $caption_desc, |
|
64
|
|
|
'src' => $thumb, |
|
65
|
|
|
'href' => $url, |
|
66
|
|
|
'width' => 150, |
|
67
|
|
|
'height' => 150, |
|
68
|
|
|
'tags' => self::clean_up_tags($image->tags) |
|
69
|
|
|
); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
return $results; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
static function clean_up_tags( $tag_source ) { |
|
|
|
|
|
|
76
|
|
|
if ( empty( $tag_source ) ) { |
|
77
|
|
|
return ''; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
$tag_source = str_replace( ', ', ',', $tag_source ); |
|
81
|
|
|
$tag_source = str_replace( ' ', ',', $tag_source ); |
|
82
|
|
|
|
|
83
|
|
|
$tags = explode( ',', $tag_source ); |
|
84
|
|
|
|
|
85
|
|
|
$tags = array_unique( $tags ); |
|
86
|
|
|
|
|
87
|
|
|
$tags = array_splice( $tags, 0, 5, true ); |
|
88
|
|
|
|
|
89
|
|
|
$tag_output = implode(',', $tags ); |
|
90
|
|
|
|
|
91
|
|
|
return $tag_output; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
static function build_caption_title( $tag_source ) { |
|
|
|
|
|
|
95
|
|
|
if ( empty( $tag_source ) ) { |
|
96
|
|
|
return false; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
$tag_source = str_replace( ', ', ',', $tag_source ); |
|
100
|
|
|
$tag_source = str_replace( ' ', ',', $tag_source ); |
|
101
|
|
|
|
|
102
|
|
|
$tags = explode( ',', $tag_source ); |
|
103
|
|
|
|
|
104
|
|
|
$tags = array_unique( $tags ); |
|
105
|
|
|
|
|
106
|
|
|
$tags = array_splice( $tags, 0, 4, true ); |
|
107
|
|
|
|
|
108
|
|
|
$tag_output = implode(' ', $tags ); |
|
109
|
|
|
|
|
110
|
|
|
return ucwords( $tag_output ); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
static function generate( $query, $count = 20 ) { |
|
|
|
|
|
|
114
|
|
|
$images = self::search( $query, $count ); |
|
115
|
|
|
|
|
116
|
|
|
foreach ( $images as $image ) { |
|
117
|
|
|
|
|
118
|
|
|
$title = foo_convert_to_key($image['caption']); |
|
119
|
|
|
$caption_title = $image['caption']; |
|
120
|
|
|
$caption_desc = $image['description']; |
|
121
|
|
|
$src = $image['href']; |
|
122
|
|
|
$tags = $image['tags']; |
|
123
|
|
|
|
|
124
|
|
|
// check if attachment already exists |
|
125
|
|
|
$attachment_args = array( |
|
126
|
|
|
'posts_per_page' => 1, |
|
127
|
|
|
'post_type' => 'attachment', |
|
128
|
|
|
'name' => $title |
|
129
|
|
|
); |
|
130
|
|
|
|
|
131
|
|
|
$attachment_check = new WP_Query( $attachment_args ); |
|
132
|
|
|
|
|
133
|
|
|
if ( !$attachment_check->have_posts() ) { |
|
134
|
|
|
$attachments[] = self::create_attachment( $src, $title, $caption_title, $caption_desc, $tags ); |
|
|
|
|
|
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
if ( !empty( $attachments ) ) { |
|
140
|
|
|
return foogallery_create_gallery( 'masonry', implode( ',', $attachments ) ); |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
return false; |
|
144
|
|
|
} |
|
145
|
|
|
|
|
146
|
|
|
static function create_attachment( $image_url, $title, $caption_title, $caption_description, $tags ) { |
|
|
|
|
|
|
147
|
|
|
// Include image.php so we can call wp_generate_attachment_metadata() |
|
148
|
|
|
require_once( ABSPATH . 'wp-admin/includes/image.php' ); |
|
149
|
|
|
|
|
150
|
|
|
// Get the contents of the picture |
|
151
|
|
|
$response = wp_remote_get( $image_url ); |
|
152
|
|
|
$contents = wp_remote_retrieve_body( $response ); |
|
153
|
|
|
|
|
154
|
|
|
// Upload and get file data |
|
155
|
|
|
$upload = wp_upload_bits( basename( $image_url ), null, $contents ); |
|
156
|
|
|
$guid = $upload['url']; |
|
157
|
|
|
$file = $upload['file']; |
|
158
|
|
|
$file_type = wp_check_filetype( basename( $file ), null ); |
|
159
|
|
|
|
|
160
|
|
|
// Create attachment |
|
161
|
|
|
$attachment = array( |
|
162
|
|
|
'ID' => 0, |
|
163
|
|
|
'guid' => $guid, |
|
164
|
|
|
'post_title' => $title, |
|
165
|
|
|
'post_excerpt' => $caption_title, |
|
166
|
|
|
'post_content' => $caption_description, |
|
167
|
|
|
'post_date' => '', |
|
168
|
|
|
'post_mime_type' => $file_type['type'], |
|
169
|
|
|
); |
|
170
|
|
|
|
|
171
|
|
|
// Insert the attachment |
|
172
|
|
|
$attachment_id = wp_insert_attachment( $attachment, $file, 0 ); |
|
173
|
|
|
$attachment_data = wp_generate_attachment_metadata( $attachment_id, $file ); |
|
174
|
|
|
wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
|
175
|
|
|
|
|
176
|
|
|
// Save tags |
|
177
|
|
|
wp_set_object_terms($attachment_id, array_map('trim', preg_split('/,+/', $tags)), FOOGALLERY_ATTACHMENT_TAXONOMY_TAG, false); |
|
178
|
|
|
|
|
179
|
|
|
return $attachment_id; |
|
180
|
|
|
} |
|
181
|
|
|
} |
|
182
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.