GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( b3ee5f...a83cd4 )
by Brad
06:08 queued 03:01
created

FooGallery_Demo_Content_Generator   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 174
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
dl 0
loc 174
rs 10
c 0
b 0
f 0
wmc 16
lcom 1
cbo 2

8 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A add_menu() 0 6 1
A render_view() 0 3 1
A search() 0 48 4
A clean_up_tags() 0 18 2
A build_caption_title() 0 18 2
A generate() 0 32 4
A create_attachment() 0 35 1
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 {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
9
		function __construct() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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() {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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 ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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 ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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 ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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 ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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 );
0 ignored issues
show
Coding Style Comprehensibility introduced by
$attachments was never initialized. Although not strictly required by PHP, it is generally a good practice to add $attachments = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
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 ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
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
}