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 generate( $query ) { |
|
|
|
|
27
|
|
|
require_once 'includes/class-pixabay.php'; |
28
|
|
|
require_once 'includes/class-lorem-ipsum.php'; |
29
|
|
|
// Include image.php so we can call wp_generate_attachment_metadata() |
30
|
|
|
require_once( ABSPATH . 'wp-admin/includes/image.php' ); |
31
|
|
|
|
32
|
|
|
$client = new FooGallery_PixabayClient(); |
33
|
|
|
$key = apply_filters( 'foogallery_pixabay_key', '1843003-12be68cf2726df47797f19cd7' ); |
34
|
|
|
|
35
|
|
|
$results = $client->search( $key, $query ); |
36
|
|
|
|
37
|
|
|
$hits = $results->hits; |
38
|
|
|
|
39
|
|
|
$lorem = new FooGallery_LoremIpsum(); |
40
|
|
|
|
41
|
|
|
//get 10 random images |
42
|
|
|
$random_image_indexes = array_rand($hits, 10); |
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
|
|
|
$title = 'Demo Image ' . $image->id; |
52
|
|
|
$caption_title = $lorem->words( rand(3,5) ); |
53
|
|
|
$caption_desc = $lorem->words( rand(8, 15) ); |
54
|
|
|
|
55
|
|
|
// check if attachment already exists |
56
|
|
|
$attachment_args = array( |
57
|
|
|
'posts_per_page' => 1, |
58
|
|
|
'post_type' => 'attachment', |
59
|
|
|
'name' => $title |
60
|
|
|
); |
61
|
|
|
$attachment_check = new WP_Query( $attachment_args ); |
62
|
|
|
|
63
|
|
|
if ( !$attachment_check->have_posts() ) { |
64
|
|
|
$attachment_id = self::create_attachment( $url, $title, $caption_title, $caption_desc ); |
|
|
|
|
65
|
|
|
} |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
//largeImageURL |
69
|
|
|
|
70
|
|
|
return 'found ' . count($hits); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
static function create_attachment( $image_url, $title, $caption_title, $caption_description ) { |
|
|
|
|
74
|
|
|
// Get the contents of the picture |
75
|
|
|
$response = wp_remote_get( $image_url ); |
76
|
|
|
$contents = wp_remote_retrieve_body( $response ); |
77
|
|
|
|
78
|
|
|
// Upload and get file data |
79
|
|
|
$upload = wp_upload_bits( basename( $image_url ), null, $contents ); |
80
|
|
|
$guid = $upload['url']; |
81
|
|
|
$file = $upload['file']; |
82
|
|
|
$file_type = wp_check_filetype( basename( $file ), null ); |
83
|
|
|
|
84
|
|
|
// Create attachment |
85
|
|
|
$attachment = array( |
86
|
|
|
'ID' => 0, |
87
|
|
|
'guid' => $guid, |
88
|
|
|
'post_title' => $title, |
89
|
|
|
'post_excerpt' => $caption_title, |
90
|
|
|
'post_content' => $caption_description, |
91
|
|
|
'post_date' => '', |
92
|
|
|
'post_mime_type' => $file_type['type'], |
93
|
|
|
); |
94
|
|
|
|
95
|
|
|
// Insert the attachment |
96
|
|
|
$attachment_id = wp_insert_attachment( $attachment, $file, 0 ); |
97
|
|
|
$attachment_data = wp_generate_attachment_metadata( $attachment_id, $file ); |
98
|
|
|
wp_update_attachment_metadata( $attachment_id, $attachment_data ); |
99
|
|
|
|
100
|
|
|
return $attachment_id; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
} |
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.