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 — feature/demo-content ( 34d331...6867d1 )
by Brad
20:01 queued 08:51
created

create_attachment()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 19
nc 1
nop 4
dl 0
loc 29
rs 8.8571
c 0
b 0
f 0
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 generate( $query ) {
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
			// 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 );
0 ignored issues
show
Unused Code introduced by
$attachment_id is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

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