Completed
Push — master ( 9f3eac...f0c1b5 )
by David
02:49 queued 13s
created

Image_License_Page::render_image()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 58

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 2
dl 0
loc 58
rs 8.9163
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
namespace Wordlift\Images_Licenses\Admin;
4
5
use Wordlift\Images_Licenses\Caption_Builder;
6
use Wordlift\Images_Licenses\Tasks\Add_License_Caption_Or_Remove_Task;
7
use Wordlift\Images_Licenses\Tasks\Remove_All_Images_Task;
8
use Wordlift\Wordpress\Submenu_Page_Base;
9
10
class Image_License_Page extends Submenu_Page_Base {
11
12
	/**
13
	 * @var array
14
	 */
15
	private $data;
16
17
	/**
18
	 * @var string
19
	 */
20
	private $version;
21
22
	/**
23
	 * Image_License_Page constructor.
24
	 *
25
	 * @param array $data
26
	 * @param string $version
27
	 */
28
	public function __construct( $data, $version ) {
29
		$count = count( $data );
30
31
		// Display the page in the menu only if there's something to do.
32
		if ( 0 === $count ) {
33
			return;
34
		}
35
		$menu_title = __( 'License Compliance', 'wordlift' ) .
36
		              sprintf( '<span class="update-plugins count-%1$d"><span class="license-compliance-count">%1$d</span></span>', $count );
37
38
		parent::__construct( 'wl_image_license_page', __( 'License Compliance', 'wordlift' ), 'manage_options', 'wl_admin_menu', $menu_title );
39
40
		$this->data    = $data;
41
		$this->version = $version;
42
43
	}
44
45
	public function render() {
46
		?>
47
        <h1><?php esc_html_e( 'License Compliance', 'wordlift' ); ?></h1>
48
49
        <p><?php esc_html_e( 'By choosing "Remove All Images" you will ', 'wordlift' ); ?>
50
            <strong><?php esc_html_e( 'remove from your website all images that do not have a Public Domain or CC0 license', 'wordlift' ); ?></strong>.
51
			<?php esc_html_e( 'Alternatively, WordLift can write the terms of the detected license in the caption of each image. Make sure
52
            that attribution, when required, is visible to your readers. You can also selectively choose from the list
53
            of images below if removing or adding the license for each of the images.', 'wordlift' ); ?></p>
54
        <p><span class="dashicons dashicons-warning"></span> <?php esc_html_e( 'As site owner you are ultimately responsible for the images
55
            being published on your website.', 'wordlift' ); ?></p>
56
57
        <p class="top">
58
            <a class="button"
59
               href="<?php echo admin_url( 'admin.php?page=wl_images_licenses__reload_data' ); ?>"><?php esc_html_e( 'Reload data', 'wordlift' ); ?></a>
60
            <a class="button"
61
               href="<?php echo admin_url( 'admin.php?page=wl_images_licenses__remove_all_images' ); ?>"><?php esc_html_e( 'Remove all images', 'wordlift' ); ?></a>
62
            <a class="button"
63
               href="<?php echo admin_url( 'admin.php?page=wl_images_licenses__add_license_caption_or_remove' ); ?>"><?php esc_html_e( 'Add license caption to images and remove those with unknown license', 'wordlift' ); ?></a>
64
        </p>
65
66
        <h2 class="screen-reader-text"><?php esc_html_e( 'Images', 'wordlift' ); ?></h2>
67
68
        <table class="wp-list-table widefat fixed striped">
69
            <thead>
70
            <tr>
71
                <th><?php esc_html_e( 'Thumbnail', 'wordlift' ); ?></th>
72
                <th><?php esc_html_e( 'Filename', 'wordlift' ); ?></th>
73
                <th><?php esc_html_e( 'License', 'wordlift' ); ?></th>
74
                <th><?php esc_html_e( 'Author', 'wordlift' ); ?></th>
75
                <th><?php esc_html_e( 'Proposed Caption', 'wordlift' ); ?></th>
76
                <th><?php esc_html_e( 'More Info', 'wordlift' ); ?></th>
77
                <th><?php esc_html_e( 'Posts', 'wordlift' ); ?></th>
78
                <th><?php esc_html_e( 'Actions', 'wordlift' ); ?></th>
79
            </tr>
80
            </thead>
81
			<?php
82
			$images = $this->data;
83
84
			for ( $i = 0; $i < count( $images ); $i ++ ) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
85
				$this->render_image( $images[ $i ], $i );
86
			}
87
			?>
88
        </table>
89
		<?php
90
	}
91
92
	/**
93
	 * @param array $image
94
	 */
95
	private function render_image( $image, $idx ) {
96
97
		$attachment_id = $image['attachment_id'];
98
99
		// Skip if the post doesn't exist anymore or has been fixed.
100
		if ( ! $this->exists( $attachment_id ) ) {
101
			return;
102
		}
103
104
		$author = html_entity_decode( $image['author'] );
105
106
		$more_info_link_esc = esc_url( $image['more_info_link'] );
107
108
		$is_unknown_license = '#N/A' === $image['license'];
109
110
		$caption_builder  = new Caption_Builder( $image );
111
		$proposed_caption = $caption_builder->build();
112
113
		$script_id = "wl-image-$idx";
114
		$row_id    = "wl-row-$idx";
115
		?>
116
        <tr id="<?php echo $row_id; ?>">
117
            <td><?php echo wp_get_attachment_image( $attachment_id, array( 100, ) ); ?></td>
118
            <td><?php echo esc_html( $image['filename'] ); ?></td>
119
            <td><?php echo esc_html( $image['license'] ); ?></td>
120
            <td><?php echo $author; ?></td>
121
            <td><?php echo $proposed_caption; ?></td>
122
            <td>
123
                <a href="<?php echo $more_info_link_esc; ?>"
124
                   target="_blank"><?php esc_html_e( 'More information', 'wordlift' ); ?></a>
125
            </td>
126
            <td>
127
				<?php
128
				$this->partial_used_in_posts( $image['posts_ids_as_featured_image'], __( 'Used as featured image in %d post(s):', 'wordlift' ) );
129
				$this->partial_used_in_posts( $image['posts_ids_as_embed'], __( 'Embedded in %d post(s):', 'wordlift' ) );
130
				?>
131
            </td>
132
            <td>
133
                <script type="application/json"
134
                        id="<?php echo $script_id; ?>"><?php echo json_encode( $image ); ?></script>
135
                <button data-id="<?php echo $script_id; ?>"
136
                        data-row-id="<?php echo $row_id; ?>"
137
                        data-action="wl_remove_all_images_task__single"
138
                        class="button wl-action-btn"><?php esc_html_e( 'Remove image', 'wordlift' ); ?></button>
139
				<?php if ( ! $is_unknown_license ) { ?>
140
                    <button data-id="<?php echo $script_id; ?>"
141
                            data-row-id="<?php echo $row_id; ?>"
142
                            data-action="wl_add_license_caption_or_remove__single"
143
                            class="button wl-action-btn"><?php esc_html_e( 'Add license caption', 'wordlift' ); ?></button>
144
				<?php } ?>
145
                <a class="button"
146
                   href=" <?php echo get_edit_post_link( $attachment_id ); ?>"
147
                   target="_blank"><?php esc_html_e( 'Edit image', 'wordlift' ); ?> <span
148
                            class="dashicons dashicons-external"></span></a>
149
            </td>
150
        </tr>
151
		<?php
152
	}
153
154
	private function partial_used_in_posts( $data, $label ) {
155
156
		// Bail out if there's not data.
157
		$count = count( $data );
158
		if ( 0 === $count ) {
159
			return;
160
		}
161
162
		echo esc_html( sprintf( $label, $count ) );
163
		foreach ( $data as $post_id ) {
164
			$post = get_post( $post_id ); ?>
165
            <a href="<?php echo get_permalink( $post_id ); ?>"><?php echo esc_html( $post->post_title ); ?></a>
166
			<?php
167
		}
168
	}
169
170
	function enqueue_scripts() {
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...
171
172
		wp_enqueue_script( $this->get_menu_slug(), plugin_dir_url( __FILE__ ) . 'assets/image-license.js', array( 'wp-util' ), $this->version, true );
173
		wp_localize_script( $this->get_menu_slug(), '_wlImageLicensePageSettings', array(
174
			'_ajax_nonce' => array(
175
				Add_License_Caption_Or_Remove_Task::MENU_SLUG . '__single' => wp_create_nonce( Add_License_Caption_Or_Remove_Task::MENU_SLUG ),
176
				Remove_All_Images_Task::MENU_SLUG . '__single'             => wp_create_nonce( Remove_All_Images_Task::MENU_SLUG ),
177
			),
178
			'l10n'        => array(
179
				'Done'              => __( 'Done', 'wordlift' ),
180
				'An error occurred' => __( 'An error occurred', 'wordlift' ),
181
			)
182
		) );
183
	}
184
185
	private function exists( $attachment_id ) {
186
		global $wpdb;
187
188
		$sql =
189
			"
190
            SELECT COUNT( 1 )
191
            FROM {$wpdb->postmeta} pm1
192
            LEFT OUTER JOIN {$wpdb->postmeta} pm2
193
             ON pm2.post_id = pm1.post_id
194
              AND pm2.meta_key = %s
195
            WHERE pm1.post_id = %d
196
              AND pm1.meta_key = %s
197
              AND pm2.meta_value IS NULL
198
            ";
199
200
		return $wpdb->get_var( $wpdb->prepare(
201
			$sql,
202
			'_wl_image_license_fixed',
203
			$attachment_id,
204
			'_wp_attached_file'
205
		) );
206
	}
207
208
}
209