Completed
Push — develop ( 1d4633...83eb6a )
by David
02:35 queued 10s
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>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Delectus laudantium nam nihil provident sequi?
50
            Alias aliquam, animi debitis distinctio dolores laboriosam modi optio possimus qui, quis suscipit, unde
51
            veritatis voluptates?</p>
52
53
        <div class="tablenav top">
54
            <a class="button"
55
               href="<?php echo admin_url( 'admin.php?page=wl_images_licenses__reload_data' ); ?>"><?php esc_html_e( 'Reload data', 'wordlift' ); ?></a>
56
            <a class="button"
57
               href="<?php echo admin_url( 'admin.php?page=wl_images_licenses__remove_all_images' ); ?>"><?php esc_html_e( 'Remove all images', 'wordlift' ); ?></a>
58
            <a class="button"
59
               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>
60
        </div>
61
62
        <h2 class="screen-reader-text"><?php esc_html_e( 'Images', 'wordlift' ); ?></h2>
63
64
        <table class="wp-list-table widefat fixed striped">
65
            <thead>
66
            <tr>
67
                <th><?php esc_html_e( 'Thumbnail', 'wordlift' ); ?></th>
68
                <th><?php esc_html_e( 'Filename', 'wordlift' ); ?></th>
69
                <th><?php esc_html_e( 'License', 'wordlift' ); ?></th>
70
                <th><?php esc_html_e( 'Author', 'wordlift' ); ?></th>
71
                <th><?php esc_html_e( 'Proposed Caption', 'wordlift' ); ?></th>
72
                <th><?php esc_html_e( 'More Info', 'wordlift' ); ?></th>
73
                <th><?php esc_html_e( 'Posts', 'wordlift' ); ?></th>
74
                <th><?php esc_html_e( 'Actions', 'wordlift' ); ?></th>
75
            </tr>
76
            </thead>
77
			<?php
78
			$images = $this->data;
79
80
			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...
81
				$this->render_image( $images[ $i ], $i );
82
			}
83
			?>
84
        </table>
85
		<?php
86
	}
87
88
	/**
89
	 * @param array $image
90
	 */
91
	private function render_image( $image, $idx ) {
92
93
		$attachment_id = $image['attachment_id'];
94
95
		// Skip if the post doesn't exist anymore or has been fixed.
96
		if ( ! $this->exists( $attachment_id ) ) {
97
			return;
98
		}
99
100
		$author = html_entity_decode( $image['author'] );
101
102
		$more_info_link_esc = esc_url( $image['more_info_link'] );
103
104
		$is_unknown_license = '#N/A' === $image['license'];
105
106
		$caption_builder  = new Caption_Builder( $image );
107
		$proposed_caption = $caption_builder->build();
108
109
		$script_id = "wl-image-$idx";
110
		$row_id    = "wl-row-$idx";
111
		?>
112
        <tr id="<?php echo $row_id; ?>">
113
            <td><?php echo wp_get_attachment_image( $attachment_id, array( 100, ) ); ?></td>
114
            <td><?php echo esc_html( $image['filename'] ); ?></td>
115
            <td><?php echo esc_html( $image['license'] ); ?></td>
116
            <td><?php echo $author; ?></td>
117
            <td><?php echo $proposed_caption; ?></td>
118
            <td>
119
                <a href="<?php echo $more_info_link_esc; ?>"
120
                   target="_blank"><?php esc_html_e( 'More information', 'wordlift' ); ?></a>
121
            </td>
122
            <td>
123
				<?php
124
				$this->partial_used_in_posts( $image['posts_ids_as_featured_image'], __( 'Used as featured image in %d post(s):', 'wordlift' ) );
125
				$this->partial_used_in_posts( $image['posts_ids_as_embed'], __( 'Embedded in %d post(s):', 'wordlift' ) );
126
				?>
127
            </td>
128
            <td>
129
                <script type="application/json"
130
                        id="<?php echo $script_id; ?>"><?php echo json_encode( $image ); ?></script>
131
                <button data-id="<?php echo $script_id; ?>"
132
                        data-row-id="<?php echo $row_id; ?>"
133
                        data-action="wl_remove_all_images_task"
134
                        class="button wl-action-btn"><?php esc_html_e( 'Remove image', 'wordlift' ); ?></button>
135
				<?php if ( ! $is_unknown_license ) { ?>
136
                    <button data-id="<?php echo $script_id; ?>"
137
                            data-row-id="<?php echo $row_id; ?>"
138
                            data-action="wl_add_license_caption_or_remove"
139
                            class="button wl-action-btn"><?php esc_html_e( 'Add license caption', 'wordlift' ); ?></button>
140
				<?php } ?>
141
                <a class="button"
142
                   href=" <?php echo get_edit_post_link( $attachment_id ); ?>"
143
                   target="_blank"><?php esc_html_e( 'Edit image', 'wordlift' ); ?> <span
144
                            class="dashicons dashicons-external"></span></a>
145
            </td>
146
        </tr>
147
		<?php
148
	}
149
150
	private function partial_used_in_posts( $data, $label ) {
151
152
		// Bail out if there's not data.
153
		$count = count( $data );
154
		if ( 0 === $count ) {
155
			return;
156
		}
157
158
		echo esc_html( sprintf( $label, $count ) );
159
		foreach ( $data as $post_id ) {
160
			$post = get_post( $post_id ); ?>
161
            <a href="<?php echo get_permalink( $post_id ); ?>"><?php echo esc_html( $post->post_title ); ?></a>
162
			<?php
163
		}
164
	}
165
166
	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...
167
168
		wp_enqueue_script( $this->get_menu_slug(), plugin_dir_url( __FILE__ ) . 'assets/image-license.js', array( 'wp-util' ), $this->version, true );
169
		wp_localize_script( $this->get_menu_slug(), '_wlImageLicensePageSettings', array(
170
			'_ajax_nonce' => array(
171
				Add_License_Caption_Or_Remove_Task::MENU_SLUG => wp_create_nonce( Add_License_Caption_Or_Remove_Task::MENU_SLUG ),
172
				Remove_All_Images_Task::MENU_SLUG             => wp_create_nonce( Remove_All_Images_Task::MENU_SLUG ),
173
			),
174
			'l10n'        => array(
175
				'Done'              => __( 'Done', 'wordlift' ),
176
				'An error occurred' => __( 'An error occurred', 'wordlift' ),
177
			)
178
		) );
179
	}
180
181
	private function exists( $attachment_id ) {
182
		global $wpdb;
183
184
		$sql =
185
			"
186
            SELECT COUNT( 1 )
187
            FROM {$wpdb->postmeta} pm1
188
            LEFT OUTER JOIN {$wpdb->postmeta} pm2
189
             ON pm2.post_id = pm1.post_id
190
              AND pm2.meta_key = %s
191
            WHERE pm1.post_id = %d
192
              AND pm1.meta_key = %s
193
              AND pm2.meta_value IS NULL
194
            ";
195
196
		return $wpdb->get_var( $wpdb->prepare(
197
			$sql,
198
			'_wl_image_license_fixed',
199
			$attachment_id,
200
			'_wp_attached_file'
201
		) );
202
	}
203
204
}
205