rousnay /
lighthouse
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
|
0 ignored issues
–
show
|
|||
| 2 | |||
| 3 | /* |
||
| 4 | * ACF Gallery Field Class |
||
| 5 | * |
||
| 6 | * All the logic for this field type |
||
| 7 | * |
||
| 8 | * @class acf_field_gallery |
||
| 9 | * @extends acf_field |
||
| 10 | * @package ACF |
||
| 11 | * @subpackage Fields |
||
| 12 | */ |
||
| 13 | |||
| 14 | if( ! class_exists('acf_field_gallery') ) : |
||
| 15 | |||
| 16 | class acf_field_gallery extends acf_field { |
||
|
0 ignored issues
–
show
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...
|
|||
| 17 | |||
| 18 | |||
| 19 | /* |
||
| 20 | * __construct |
||
| 21 | * |
||
| 22 | * This function will setup the field type data |
||
| 23 | * |
||
| 24 | * @type function |
||
| 25 | * @date 5/03/2014 |
||
| 26 | * @since 5.0.0 |
||
| 27 | * |
||
| 28 | * @param n/a |
||
| 29 | * @return n/a |
||
| 30 | */ |
||
|
0 ignored issues
–
show
The doc-type
n/a could not be parsed: Unknown type name "n/a" at position 0. (view supported doc-types)
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types. Loading history...
|
|||
| 31 | |||
| 32 | function __construct() { |
||
|
0 ignored issues
–
show
|
|||
| 33 | |||
| 34 | // vars |
||
| 35 | $this->name = 'gallery'; |
||
|
0 ignored issues
–
show
The property
name cannot be accessed from this context as it is declared private in class acf_field.
This check looks for access to properties that are not accessible from the current context. If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class. Loading history...
|
|||
| 36 | $this->label = __("Gallery",'acf'); |
||
|
0 ignored issues
–
show
The property
label does not exist. Did you maybe forget to declare it?
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code: class MyClass { }
$x = new MyClass();
$x->foo = true;
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: class MyClass {
public $foo;
}
$x = new MyClass();
$x->foo = true;
Loading history...
|
|||
| 37 | $this->category = 'content'; |
||
|
0 ignored issues
–
show
The property
category cannot be accessed from this context as it is declared private in class acf_field.
This check looks for access to properties that are not accessible from the current context. If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class. Loading history...
|
|||
| 38 | $this->defaults = array( |
||
|
0 ignored issues
–
show
The property
defaults cannot be accessed from this context as it is declared private in class acf_field.
This check looks for access to properties that are not accessible from the current context. If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class. Loading history...
|
|||
| 39 | 'preview_size' => 'thumbnail', |
||
| 40 | 'library' => 'all', |
||
| 41 | 'min' => 0, |
||
| 42 | 'max' => 0, |
||
| 43 | 'min_width' => 0, |
||
| 44 | 'min_height' => 0, |
||
| 45 | 'min_size' => 0, |
||
| 46 | 'max_width' => 0, |
||
| 47 | 'max_height' => 0, |
||
| 48 | 'max_size' => 0, |
||
| 49 | 'mime_types' => '' |
||
| 50 | ); |
||
| 51 | $this->l10n = array( |
||
|
0 ignored issues
–
show
The property
l10n cannot be accessed from this context as it is declared private in class acf_field.
This check looks for access to properties that are not accessible from the current context. If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class. Loading history...
|
|||
| 52 | 'select' => __("Add Image to Gallery",'acf'), |
||
| 53 | 'edit' => __("Edit Image",'acf'), |
||
| 54 | 'update' => __("Update Image",'acf'), |
||
| 55 | 'uploadedTo' => __("uploaded to this post",'acf'), |
||
| 56 | 'max' => __("Maximum selection reached",'acf') |
||
| 57 | ); |
||
| 58 | |||
| 59 | |||
| 60 | // actions |
||
| 61 | add_action('wp_ajax_acf/fields/gallery/get_attachment', array($this, 'ajax_get_attachment')); |
||
| 62 | add_action('wp_ajax_nopriv_acf/fields/gallery/get_attachment', array($this, 'ajax_get_attachment')); |
||
| 63 | |||
| 64 | add_action('wp_ajax_acf/fields/gallery/update_attachment', array($this, 'ajax_update_attachment')); |
||
| 65 | add_action('wp_ajax_nopriv_acf/fields/gallery/update_attachment', array($this, 'ajax_update_attachment')); |
||
| 66 | |||
| 67 | add_action('wp_ajax_acf/fields/gallery/get_sort_order', array($this, 'ajax_get_sort_order')); |
||
| 68 | add_action('wp_ajax_nopriv_acf/fields/gallery/get_sort_order', array($this, 'ajax_get_sort_order')); |
||
| 69 | |||
| 70 | |||
| 71 | |||
| 72 | // do not delete! |
||
| 73 | parent::__construct(); |
||
| 74 | } |
||
| 75 | |||
| 76 | |||
| 77 | /* |
||
| 78 | * ajax_get_attachment |
||
| 79 | * |
||
| 80 | * description |
||
| 81 | * |
||
| 82 | * @type function |
||
| 83 | * @date 13/12/2013 |
||
| 84 | * @since 5.0.0 |
||
| 85 | * |
||
| 86 | * @param $post_id (int) |
||
| 87 | * @return $post_id (int) |
||
| 88 | */ |
||
|
0 ignored issues
–
show
The doc-type
$post_id could not be parsed: Unknown type name "$post_id" at position 0. (view supported doc-types)
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types. Loading history...
|
|||
| 89 | |||
| 90 | function ajax_get_attachment() { |
||
|
0 ignored issues
–
show
ajax_get_attachment uses the super-global variable $_POST which is generally not recommended.
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: // Bad
class Router
{
public function generate($path)
{
return $_SERVER['HOST'].$path;
}
}
// Better
class Router
{
private $host;
public function __construct($host)
{
$this->host = $host;
}
public function generate($path)
{
return $this->host.$path;
}
}
class Controller
{
public function myAction(Request $request)
{
// Instead of
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
// Better (assuming you use the Symfony2 request)
$page = $request->query->get('page', 1);
}
}
Loading history...
|
|||
| 91 | |||
| 92 | // options |
||
| 93 | $options = acf_parse_args( $_POST, array( |
||
| 94 | 'post_id' => 0, |
||
| 95 | 'id' => 0, |
||
| 96 | 'field_key' => '', |
||
| 97 | 'nonce' => '', |
||
| 98 | )); |
||
| 99 | |||
| 100 | |||
| 101 | // validate |
||
| 102 | if( ! wp_verify_nonce($options['nonce'], 'acf_nonce') ) { |
||
| 103 | |||
| 104 | die(); |
||
|
0 ignored issues
–
show
The method
ajax_get_attachment() contains an exit expression.
An exit expression should only be used in rare cases. For example, if you write a short command line script. In most cases however, using an Loading history...
|
|||
| 105 | |||
| 106 | } |
||
| 107 | |||
| 108 | if( empty($options['id']) ) { |
||
| 109 | |||
| 110 | die(); |
||
|
0 ignored issues
–
show
The method
ajax_get_attachment() contains an exit expression.
An exit expression should only be used in rare cases. For example, if you write a short command line script. In most cases however, using an Loading history...
|
|||
| 111 | |||
| 112 | } |
||
| 113 | |||
| 114 | |||
| 115 | // load field |
||
| 116 | $field = acf_get_field( $options['field_key'] ); |
||
| 117 | |||
| 118 | if( !$field ) { |
||
| 119 | |||
| 120 | die(); |
||
|
0 ignored issues
–
show
The method
ajax_get_attachment() contains an exit expression.
An exit expression should only be used in rare cases. For example, if you write a short command line script. In most cases however, using an Loading history...
|
|||
| 121 | |||
| 122 | } |
||
| 123 | |||
| 124 | |||
| 125 | // render |
||
| 126 | $this->render_attachment( $options['id'], $field ); |
||
| 127 | die; |
||
|
0 ignored issues
–
show
The method
ajax_get_attachment() contains an exit expression.
An exit expression should only be used in rare cases. For example, if you write a short command line script. In most cases however, using an Loading history...
|
|||
| 128 | |||
| 129 | } |
||
| 130 | |||
| 131 | |||
| 132 | /* |
||
| 133 | * ajax_update_attachment |
||
| 134 | * |
||
| 135 | * description |
||
| 136 | * |
||
| 137 | * @type function |
||
| 138 | * @date 13/12/2013 |
||
| 139 | * @since 5.0.0 |
||
| 140 | * |
||
| 141 | * @param $post_id (int) |
||
| 142 | * @return $post_id (int) |
||
| 143 | */ |
||
|
0 ignored issues
–
show
The doc-type
$post_id could not be parsed: Unknown type name "$post_id" at position 0. (view supported doc-types)
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types. Loading history...
|
|||
| 144 | |||
| 145 | function ajax_update_attachment() { |
||
|
0 ignored issues
–
show
ajax_update_attachment uses the super-global variable $_POST which is generally not recommended.
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: // Bad
class Router
{
public function generate($path)
{
return $_SERVER['HOST'].$path;
}
}
// Better
class Router
{
private $host;
public function __construct($host)
{
$this->host = $host;
}
public function generate($path)
{
return $this->host.$path;
}
}
class Controller
{
public function myAction(Request $request)
{
// Instead of
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
// Better (assuming you use the Symfony2 request)
$page = $request->query->get('page', 1);
}
}
Loading history...
|
|||
| 146 | |||
| 147 | // validate nonce |
||
| 148 | if( !wp_verify_nonce($_POST['nonce'], 'acf_nonce') ) { |
||
| 149 | |||
| 150 | wp_send_json_error(); |
||
| 151 | |||
| 152 | } |
||
| 153 | |||
| 154 | |||
| 155 | // bail early if no attachments |
||
| 156 | if( empty($_POST['attachments']) ) { |
||
| 157 | |||
| 158 | wp_send_json_error(); |
||
| 159 | |||
| 160 | } |
||
| 161 | |||
| 162 | |||
| 163 | // loop over attachments |
||
| 164 | foreach( $_POST['attachments'] as $id => $changes ) { |
||
| 165 | |||
| 166 | if ( !current_user_can( 'edit_post', $id ) ) |
||
| 167 | wp_send_json_error(); |
||
| 168 | |||
| 169 | $post = get_post( $id, ARRAY_A ); |
||
| 170 | |||
| 171 | if ( 'attachment' != $post['post_type'] ) |
||
| 172 | wp_send_json_error(); |
||
| 173 | |||
| 174 | if ( isset( $changes['title'] ) ) |
||
| 175 | $post['post_title'] = $changes['title']; |
||
| 176 | |||
| 177 | if ( isset( $changes['caption'] ) ) |
||
| 178 | $post['post_excerpt'] = $changes['caption']; |
||
| 179 | |||
| 180 | if ( isset( $changes['description'] ) ) |
||
| 181 | $post['post_content'] = $changes['description']; |
||
| 182 | |||
| 183 | if ( isset( $changes['alt'] ) ) { |
||
| 184 | $alt = wp_unslash( $changes['alt'] ); |
||
| 185 | if ( $alt != get_post_meta( $id, '_wp_attachment_image_alt', true ) ) { |
||
| 186 | $alt = wp_strip_all_tags( $alt, true ); |
||
| 187 | update_post_meta( $id, '_wp_attachment_image_alt', wp_slash( $alt ) ); |
||
| 188 | } |
||
| 189 | } |
||
| 190 | |||
| 191 | |||
| 192 | // save post |
||
| 193 | wp_update_post( $post ); |
||
| 194 | |||
| 195 | |||
| 196 | /** This filter is documented in wp-admin/includes/media.php */ |
||
| 197 | // - seems off to run this filter AFTER the update_post function, but there is a reason |
||
| 198 | // - when placed BEFORE, an empty post_title will be populated by WP |
||
| 199 | // - this filter will still allow 3rd party to save extra image data! |
||
| 200 | $post = apply_filters( 'attachment_fields_to_save', $post, $changes ); |
||
|
0 ignored issues
–
show
$post 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 Loading history...
|
|||
| 201 | |||
| 202 | |||
| 203 | // save meta |
||
| 204 | acf_save_post( $id ); |
||
| 205 | |||
| 206 | } |
||
| 207 | |||
| 208 | |||
| 209 | // return |
||
| 210 | wp_send_json_success(); |
||
| 211 | |||
| 212 | } |
||
| 213 | |||
| 214 | |||
| 215 | /* |
||
| 216 | * ajax_get_sort_order |
||
| 217 | * |
||
| 218 | * description |
||
| 219 | * |
||
| 220 | * @type function |
||
| 221 | * @date 13/12/2013 |
||
| 222 | * @since 5.0.0 |
||
| 223 | * |
||
| 224 | * @param $post_id (int) |
||
| 225 | * @return $post_id (int) |
||
| 226 | */ |
||
|
0 ignored issues
–
show
The doc-type
$post_id could not be parsed: Unknown type name "$post_id" at position 0. (view supported doc-types)
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types. Loading history...
|
|||
| 227 | |||
| 228 | function ajax_get_sort_order() { |
||
|
0 ignored issues
–
show
ajax_get_sort_order uses the super-global variable $_POST which is generally not recommended.
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: // Bad
class Router
{
public function generate($path)
{
return $_SERVER['HOST'].$path;
}
}
// Better
class Router
{
private $host;
public function __construct($host)
{
$this->host = $host;
}
public function generate($path)
{
return $this->host.$path;
}
}
class Controller
{
public function myAction(Request $request)
{
// Instead of
$page = isset($_GET['page']) ? intval($_GET['page']) : 1;
// Better (assuming you use the Symfony2 request)
$page = $request->query->get('page', 1);
}
}
Loading history...
|
|||
| 229 | |||
| 230 | // vars |
||
| 231 | $r = array(); |
||
|
0 ignored issues
–
show
$r 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 Loading history...
|
|||
| 232 | $order = 'DESC'; |
||
| 233 | $args = acf_parse_args( $_POST, array( |
||
| 234 | 'ids' => 0, |
||
| 235 | 'sort' => 'date', |
||
| 236 | 'field_key' => '', |
||
| 237 | 'nonce' => '', |
||
| 238 | )); |
||
| 239 | |||
| 240 | |||
| 241 | // validate |
||
| 242 | if( ! wp_verify_nonce($args['nonce'], 'acf_nonce') ) { |
||
| 243 | |||
| 244 | wp_send_json_error(); |
||
| 245 | |||
| 246 | } |
||
| 247 | |||
| 248 | |||
| 249 | // reverse |
||
| 250 | if( $args['sort'] == 'reverse' ) { |
||
| 251 | |||
| 252 | $ids = array_reverse($args['ids']); |
||
| 253 | |||
| 254 | wp_send_json_success($ids); |
||
| 255 | |||
| 256 | } |
||
| 257 | |||
| 258 | |||
| 259 | if( $args['sort'] == 'title' ) { |
||
| 260 | |||
| 261 | $order = 'ASC'; |
||
| 262 | |||
| 263 | } |
||
| 264 | |||
| 265 | |||
| 266 | // find attachments (DISTINCT POSTS) |
||
| 267 | $ids = get_posts(array( |
||
| 268 | 'post_type' => 'attachment', |
||
| 269 | 'numberposts' => -1, |
||
| 270 | 'post_status' => 'any', |
||
| 271 | 'post__in' => $args['ids'], |
||
| 272 | 'order' => $order, |
||
| 273 | 'orderby' => $args['sort'], |
||
| 274 | 'fields' => 'ids' |
||
| 275 | )); |
||
| 276 | |||
| 277 | |||
| 278 | // success |
||
| 279 | if( !empty($ids) ) { |
||
| 280 | |||
| 281 | wp_send_json_success($ids); |
||
| 282 | |||
| 283 | } |
||
| 284 | |||
| 285 | |||
| 286 | // failure |
||
| 287 | wp_send_json_error(); |
||
| 288 | |||
| 289 | } |
||
| 290 | |||
| 291 | |||
| 292 | /* |
||
| 293 | * render_attachment |
||
| 294 | * |
||
| 295 | * description |
||
| 296 | * |
||
| 297 | * @type function |
||
| 298 | * @date 13/12/2013 |
||
| 299 | * @since 5.0.0 |
||
| 300 | * |
||
| 301 | * @param $post_id (int) |
||
| 302 | * @return $post_id (int) |
||
| 303 | */ |
||
|
0 ignored issues
–
show
The doc-type
$post_id could not be parsed: Unknown type name "$post_id" at position 0. (view supported doc-types)
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types. Loading history...
|
|||
| 304 | |||
| 305 | function render_attachment( $id = 0, $field ) { |
||
|
0 ignored issues
–
show
|
|||
| 306 | |||
| 307 | // vars |
||
| 308 | $attachment = wp_prepare_attachment_for_js( $id ); |
||
| 309 | $thumb = ''; |
||
|
0 ignored issues
–
show
$thumb 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 Loading history...
|
|||
| 310 | $prefix = "attachments[{$id}]"; |
||
| 311 | $compat = get_compat_media_markup( $id ); |
||
| 312 | $dimentions = ''; |
||
| 313 | |||
| 314 | |||
| 315 | // thumb |
||
| 316 | if( isset($attachment['thumb']['src']) ) { |
||
| 317 | |||
| 318 | // video |
||
| 319 | $thumb = $attachment['thumb']['src']; |
||
| 320 | |||
| 321 | } elseif( isset($attachment['sizes']['thumbnail']['url']) ) { |
||
| 322 | |||
| 323 | // image |
||
| 324 | $thumb = $attachment['sizes']['thumbnail']['url']; |
||
| 325 | |||
| 326 | } elseif( $attachment['type'] === 'image' ) { |
||
| 327 | |||
| 328 | // svg |
||
| 329 | $thumb = $attachment['url']; |
||
| 330 | |||
| 331 | } else { |
||
| 332 | |||
| 333 | // fallback (perhaps attachment does not exist) |
||
| 334 | $thumb = $attachment['icon']; |
||
| 335 | |||
| 336 | } |
||
| 337 | |||
| 338 | |||
| 339 | |||
| 340 | // dimentions |
||
| 341 | if( $attachment['type'] === 'audio' ) { |
||
| 342 | |||
| 343 | $dimentions = __('Length', 'acf') . ': ' . $attachment['fileLength']; |
||
| 344 | |||
| 345 | } elseif( !empty($attachment['width']) ) { |
||
| 346 | |||
| 347 | $dimentions = $attachment['width'] . ' x ' . $attachment['height']; |
||
| 348 | |||
| 349 | } |
||
| 350 | |||
| 351 | if( $attachment['filesizeHumanReadable'] ) { |
||
| 352 | |||
| 353 | $dimentions .= ' (' . $attachment['filesizeHumanReadable'] . ')'; |
||
| 354 | |||
| 355 | } |
||
| 356 | |||
| 357 | ?> |
||
| 358 | <div class="acf-gallery-side-info acf-cf"> |
||
| 359 | <img src="<?php echo $thumb; ?>" alt="<?php echo $attachment['alt']; ?>" /> |
||
| 360 | <p class="filename"><strong><?php echo $attachment['filename']; ?></strong></p> |
||
| 361 | <p class="uploaded"><?php echo $attachment['dateFormatted']; ?></p> |
||
| 362 | <p class="dimensions"><?php echo $dimentions; ?></p> |
||
| 363 | <p class="actions"><a href="#" class="edit-attachment" data-id="<?php echo $id; ?>"><?php _e('Edit', 'acf'); ?></a> <a href="#" class="remove-attachment" data-id="<?php echo $id; ?>"><?php _e('Remove', 'acf'); ?></a></p> |
||
| 364 | </div> |
||
| 365 | <table class="form-table"> |
||
| 366 | <tbody> |
||
| 367 | <?php |
||
| 368 | |||
| 369 | acf_render_field_wrap(array( |
||
| 370 | //'key' => "{$field['key']}-title", |
||
|
0 ignored issues
–
show
Unused Code
Comprehensibility
introduced
by
67% of this comment could be valid code. Did you maybe forget this after debugging?
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it. The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production. This check looks for comments that seem to be mostly valid code and reports them. Loading history...
|
|||
| 371 | 'name' => 'title', |
||
| 372 | 'prefix' => $prefix, |
||
| 373 | 'type' => 'text', |
||
| 374 | 'label' => 'Title', |
||
| 375 | 'value' => $attachment['title'] |
||
| 376 | ), 'tr'); |
||
| 377 | |||
| 378 | acf_render_field_wrap(array( |
||
| 379 | //'key' => "{$field['key']}-caption", |
||
|
0 ignored issues
–
show
Unused Code
Comprehensibility
introduced
by
67% of this comment could be valid code. Did you maybe forget this after debugging?
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it. The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production. This check looks for comments that seem to be mostly valid code and reports them. Loading history...
|
|||
| 380 | 'name' => 'caption', |
||
| 381 | 'prefix' => $prefix, |
||
| 382 | 'type' => 'textarea', |
||
| 383 | 'label' => 'Caption', |
||
| 384 | 'value' => $attachment['caption'] |
||
| 385 | ), 'tr'); |
||
| 386 | |||
| 387 | acf_render_field_wrap(array( |
||
| 388 | //'key' => "{$field['key']}-alt", |
||
|
0 ignored issues
–
show
Unused Code
Comprehensibility
introduced
by
67% of this comment could be valid code. Did you maybe forget this after debugging?
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it. The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production. This check looks for comments that seem to be mostly valid code and reports them. Loading history...
|
|||
| 389 | 'name' => 'alt', |
||
| 390 | 'prefix' => $prefix, |
||
| 391 | 'type' => 'text', |
||
| 392 | 'label' => 'Alt Text', |
||
| 393 | 'value' => $attachment['alt'] |
||
| 394 | ), 'tr'); |
||
| 395 | |||
| 396 | acf_render_field_wrap(array( |
||
| 397 | //'key' => "{$field['key']}-description", |
||
|
0 ignored issues
–
show
Unused Code
Comprehensibility
introduced
by
67% of this comment could be valid code. Did you maybe forget this after debugging?
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it. The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production. This check looks for comments that seem to be mostly valid code and reports them. Loading history...
|
|||
| 398 | 'name' => 'description', |
||
| 399 | 'prefix' => $prefix, |
||
| 400 | 'type' => 'textarea', |
||
| 401 | 'label' => 'Description', |
||
| 402 | 'value' => $attachment['description'] |
||
| 403 | ), 'tr'); |
||
| 404 | |||
| 405 | ?> |
||
| 406 | </tbody> |
||
| 407 | </table> |
||
| 408 | <?php echo $compat['item']; ?> |
||
| 409 | |||
| 410 | <?php |
||
| 411 | |||
| 412 | } |
||
| 413 | |||
| 414 | |||
| 415 | /* |
||
| 416 | * render_field() |
||
| 417 | * |
||
| 418 | * Create the HTML interface for your field |
||
| 419 | * |
||
| 420 | * @param $field - an array holding all the field's data |
||
| 421 | * |
||
| 422 | * @type action |
||
| 423 | * @since 3.6 |
||
| 424 | * @date 23/01/13 |
||
| 425 | */ |
||
| 426 | |||
| 427 | function render_field( $field ) { |
||
|
0 ignored issues
–
show
|
|||
| 428 | |||
| 429 | // enqueue |
||
| 430 | acf_enqueue_uploader(); |
||
| 431 | |||
| 432 | |||
| 433 | // vars |
||
| 434 | $posts = array(); |
||
| 435 | $atts = array( |
||
| 436 | 'id' => $field['id'], |
||
| 437 | 'class' => "acf-gallery {$field['class']}", |
||
| 438 | 'data-preview_size' => $field['preview_size'], |
||
| 439 | 'data-library' => $field['library'], |
||
| 440 | 'data-min' => $field['min'], |
||
| 441 | 'data-max' => $field['max'], |
||
| 442 | 'data-mime_types' => $field['mime_types'], |
||
| 443 | ); |
||
| 444 | |||
| 445 | |||
| 446 | // set gallery height |
||
| 447 | $height = acf_get_user_setting('gallery_height', 400); |
||
|
0 ignored issues
–
show
400 is of type integer, but the function expects a boolean.
It seems like the type of the argument is not accepted by the function/method which you are calling. In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug. We suggest to add an explicit type cast like in the following example: function acceptsInteger($int) { }
$x = '123'; // string "123"
// Instead of
acceptsInteger($x);
// we recommend to use
acceptsInteger((integer) $x);
Loading history...
|
|||
| 448 | $height = max( $height, 200 ); // minimum height is 200 |
||
| 449 | $atts['style'] = "height:{$height}px"; |
||
| 450 | |||
| 451 | |||
| 452 | // load posts |
||
| 453 | if( !empty($field['value']) ) { |
||
| 454 | |||
| 455 | $posts = acf_get_posts(array( |
||
| 456 | 'post_type' => 'attachment', |
||
| 457 | 'post__in' => $field['value'] |
||
| 458 | )); |
||
| 459 | |||
| 460 | } |
||
| 461 | |||
| 462 | |||
| 463 | ?> |
||
| 464 | <div <?php acf_esc_attr_e($atts); ?>> |
||
| 465 | |||
| 466 | <div class="acf-hidden"> |
||
| 467 | <input type="hidden" <?php acf_esc_attr_e(array( 'name' => $field['name'], 'value' => '', 'data-name' => 'ids' )); ?> /> |
||
| 468 | </div> |
||
| 469 | |||
| 470 | <div class="acf-gallery-main"> |
||
| 471 | |||
| 472 | <div class="acf-gallery-attachments"> |
||
| 473 | |||
| 474 | <?php if( !empty($posts) ): ?> |
||
| 475 | |||
| 476 | <?php foreach( $posts as $post ): |
||
| 477 | |||
| 478 | // vars |
||
| 479 | $type = acf_maybe_get(explode('/', $post->post_mime_type), 0); |
||
| 480 | $thumb_id = $post->ID; |
||
| 481 | $thumb_url = ''; |
||
| 482 | $thumb_class = 'acf-gallery-attachment acf-soh'; |
||
| 483 | $filename = wp_basename($post->guid); |
||
| 484 | |||
| 485 | |||
| 486 | // thumb |
||
| 487 | if( $type === 'image' || $type === 'audio' || $type === 'video' ) { |
||
| 488 | |||
| 489 | // change $thumb_id |
||
| 490 | if( $type === 'audio' || $type === 'video' ) { |
||
| 491 | |||
| 492 | $thumb_id = get_post_thumbnail_id( $post->ID ); |
||
| 493 | |||
| 494 | } |
||
| 495 | |||
| 496 | |||
| 497 | // get attachment |
||
| 498 | if( $thumb_id ) { |
||
| 499 | |||
| 500 | $thumb_url = wp_get_attachment_image_src( $thumb_id, $field['preview_size'] ); |
||
| 501 | $thumb_url = acf_maybe_get( $thumb_url, 0 ); |
||
| 502 | |||
| 503 | } |
||
| 504 | |||
| 505 | } |
||
| 506 | |||
| 507 | |||
| 508 | // fallback |
||
| 509 | if( !$thumb_url ) { |
||
| 510 | |||
| 511 | $thumb_url = wp_mime_type_icon( $post->ID ); |
||
| 512 | $thumb_class .= ' is-mime-icon'; |
||
| 513 | |||
| 514 | } |
||
| 515 | |||
| 516 | ?> |
||
| 517 | <div class="<?php echo $thumb_class; ?>" data-id="<?php echo $post->ID; ?>"> |
||
| 518 | <input type="hidden" name="<?php echo $field['name']; ?>[]" value="<?php echo $post->ID; ?>" /> |
||
| 519 | <div class="margin" title="<?php echo $filename; ?>"> |
||
| 520 | <div class="thumbnail"> |
||
| 521 | <img src="<?php echo $thumb_url; ?>"/> |
||
| 522 | </div> |
||
| 523 | <?php if( $type !== 'image' ): ?> |
||
| 524 | <div class="filename"><?php echo acf_get_truncated($filename, 18); ?></div> |
||
| 525 | <?php endif; ?> |
||
| 526 | </div> |
||
| 527 | <div class="actions acf-soh-target"> |
||
| 528 | <a class="acf-icon -cancel dark remove-attachment" data-id="<?php echo $post->ID; ?>" href="#"></a> |
||
| 529 | </div> |
||
| 530 | </div> |
||
| 531 | |||
| 532 | <?php endforeach; ?> |
||
| 533 | |||
| 534 | <?php endif; ?> |
||
| 535 | |||
| 536 | |||
| 537 | </div> |
||
| 538 | |||
| 539 | <div class="acf-gallery-toolbar"> |
||
| 540 | |||
| 541 | <ul class="acf-hl"> |
||
| 542 | <li> |
||
| 543 | <a href="#" class="acf-button blue add-attachment"><?php _e('Add to gallery', 'acf'); ?></a> |
||
| 544 | </li> |
||
| 545 | <li class="acf-fr"> |
||
| 546 | <select class="bulk-actions"> |
||
| 547 | <option value=""><?php _e('Bulk actions', 'acf'); ?></option> |
||
| 548 | <option value="date"><?php _e('Sort by date uploaded', 'acf'); ?></option> |
||
| 549 | <option value="modified"><?php _e('Sort by date modified', 'acf'); ?></option> |
||
| 550 | <option value="title"><?php _e('Sort by title', 'acf'); ?></option> |
||
| 551 | <option value="reverse"><?php _e('Reverse current order', 'acf'); ?></option> |
||
| 552 | </select> |
||
| 553 | </li> |
||
| 554 | </ul> |
||
| 555 | |||
| 556 | </div> |
||
| 557 | |||
| 558 | </div> |
||
| 559 | |||
| 560 | <div class="acf-gallery-side"> |
||
| 561 | <div class="acf-gallery-side-inner"> |
||
| 562 | |||
| 563 | <div class="acf-gallery-side-data"></div> |
||
| 564 | |||
| 565 | <div class="acf-gallery-toolbar"> |
||
| 566 | |||
| 567 | <ul class="acf-hl"> |
||
| 568 | <li> |
||
| 569 | <a href="#" class="acf-button close-sidebar"><?php _e('Close', 'acf'); ?></a> |
||
| 570 | </li> |
||
| 571 | <li class="acf-fr"> |
||
| 572 | <a class="acf-button blue update-attachment"><?php _e('Update', 'acf'); ?></a> |
||
| 573 | </li> |
||
| 574 | </ul> |
||
| 575 | |||
| 576 | </div> |
||
| 577 | |||
| 578 | </div> |
||
| 579 | </div> |
||
| 580 | |||
| 581 | </div> |
||
| 582 | <?php |
||
| 583 | |||
| 584 | } |
||
| 585 | |||
| 586 | |||
| 587 | /* |
||
| 588 | * render_field_settings() |
||
| 589 | * |
||
| 590 | * Create extra options for your field. This is rendered when editing a field. |
||
| 591 | * The value of $field['name'] can be used (like bellow) to save extra data to the $field |
||
| 592 | * |
||
| 593 | * @type action |
||
| 594 | * @since 3.6 |
||
| 595 | * @date 23/01/13 |
||
| 596 | * |
||
| 597 | * @param $field - an array holding all the field's data |
||
| 598 | */ |
||
| 599 | |||
| 600 | function render_field_settings( $field ) { |
||
|
0 ignored issues
–
show
|
|||
| 601 | |||
| 602 | // clear numeric settings |
||
| 603 | $clear = array( |
||
| 604 | 'min', |
||
| 605 | 'max', |
||
| 606 | 'min_width', |
||
| 607 | 'min_height', |
||
| 608 | 'min_size', |
||
| 609 | 'max_width', |
||
| 610 | 'max_height', |
||
| 611 | 'max_size' |
||
| 612 | ); |
||
| 613 | |||
| 614 | foreach( $clear as $k ) { |
||
| 615 | |||
| 616 | if( empty($field[$k]) ) { |
||
| 617 | |||
| 618 | $field[$k] = ''; |
||
| 619 | |||
| 620 | } |
||
| 621 | |||
| 622 | } |
||
| 623 | |||
| 624 | |||
| 625 | // min |
||
| 626 | acf_render_field_setting( $field, array( |
||
| 627 | 'label' => __('Minimum Selection','acf'), |
||
| 628 | 'instructions' => '', |
||
| 629 | 'type' => 'number', |
||
| 630 | 'name' => 'min' |
||
| 631 | )); |
||
| 632 | |||
| 633 | |||
| 634 | // max |
||
| 635 | acf_render_field_setting( $field, array( |
||
| 636 | 'label' => __('Maximum Selection','acf'), |
||
| 637 | 'instructions' => '', |
||
| 638 | 'type' => 'number', |
||
| 639 | 'name' => 'max' |
||
| 640 | )); |
||
| 641 | |||
| 642 | |||
| 643 | // preview_size |
||
| 644 | acf_render_field_setting( $field, array( |
||
| 645 | 'label' => __('Preview Size','acf'), |
||
| 646 | 'instructions' => __('Shown when entering data','acf'), |
||
| 647 | 'type' => 'select', |
||
| 648 | 'name' => 'preview_size', |
||
| 649 | 'choices' => acf_get_image_sizes() |
||
| 650 | )); |
||
| 651 | |||
| 652 | |||
| 653 | // library |
||
| 654 | acf_render_field_setting( $field, array( |
||
| 655 | 'label' => __('Library','acf'), |
||
| 656 | 'instructions' => __('Limit the media library choice','acf'), |
||
| 657 | 'type' => 'radio', |
||
| 658 | 'name' => 'library', |
||
| 659 | 'layout' => 'horizontal', |
||
| 660 | 'choices' => array( |
||
| 661 | 'all' => __('All', 'acf'), |
||
| 662 | 'uploadedTo' => __('Uploaded to post', 'acf') |
||
| 663 | ) |
||
| 664 | )); |
||
| 665 | |||
| 666 | |||
| 667 | // min |
||
| 668 | acf_render_field_setting( $field, array( |
||
| 669 | 'label' => __('Minimum','acf'), |
||
| 670 | 'instructions' => __('Restrict which images can be uploaded','acf'), |
||
| 671 | 'type' => 'text', |
||
| 672 | 'name' => 'min_width', |
||
| 673 | 'prepend' => __('Width', 'acf'), |
||
| 674 | 'append' => 'px', |
||
| 675 | )); |
||
| 676 | |||
| 677 | acf_render_field_setting( $field, array( |
||
| 678 | 'label' => '', |
||
| 679 | 'type' => 'text', |
||
| 680 | 'name' => 'min_height', |
||
| 681 | 'prepend' => __('Height', 'acf'), |
||
| 682 | 'append' => 'px', |
||
| 683 | 'wrapper' => array( |
||
| 684 | 'data-append' => 'min_width' |
||
| 685 | ) |
||
| 686 | )); |
||
| 687 | |||
| 688 | acf_render_field_setting( $field, array( |
||
| 689 | 'label' => '', |
||
| 690 | 'type' => 'text', |
||
| 691 | 'name' => 'min_size', |
||
| 692 | 'prepend' => __('File size', 'acf'), |
||
| 693 | 'append' => 'MB', |
||
| 694 | 'wrapper' => array( |
||
| 695 | 'data-append' => 'min_width' |
||
| 696 | ) |
||
| 697 | )); |
||
| 698 | |||
| 699 | |||
| 700 | // max |
||
| 701 | acf_render_field_setting( $field, array( |
||
| 702 | 'label' => __('Maximum','acf'), |
||
| 703 | 'instructions' => __('Restrict which images can be uploaded','acf'), |
||
| 704 | 'type' => 'text', |
||
| 705 | 'name' => 'max_width', |
||
| 706 | 'prepend' => __('Width', 'acf'), |
||
| 707 | 'append' => 'px', |
||
| 708 | )); |
||
| 709 | |||
| 710 | acf_render_field_setting( $field, array( |
||
| 711 | 'label' => '', |
||
| 712 | 'type' => 'text', |
||
| 713 | 'name' => 'max_height', |
||
| 714 | 'prepend' => __('Height', 'acf'), |
||
| 715 | 'append' => 'px', |
||
| 716 | 'wrapper' => array( |
||
| 717 | 'data-append' => 'max_width' |
||
| 718 | ) |
||
| 719 | )); |
||
| 720 | |||
| 721 | acf_render_field_setting( $field, array( |
||
| 722 | 'label' => '', |
||
| 723 | 'type' => 'text', |
||
| 724 | 'name' => 'max_size', |
||
| 725 | 'prepend' => __('File size', 'acf'), |
||
| 726 | 'append' => 'MB', |
||
| 727 | 'wrapper' => array( |
||
| 728 | 'data-append' => 'max_width' |
||
| 729 | ) |
||
| 730 | )); |
||
| 731 | |||
| 732 | |||
| 733 | // allowed type |
||
| 734 | acf_render_field_setting( $field, array( |
||
| 735 | 'label' => __('Allowed file types','acf'), |
||
| 736 | 'instructions' => __('Comma separated list. Leave blank for all types','acf'), |
||
| 737 | 'type' => 'text', |
||
| 738 | 'name' => 'mime_types', |
||
| 739 | )); |
||
| 740 | |||
| 741 | } |
||
| 742 | |||
| 743 | |||
| 744 | /* |
||
| 745 | * format_value() |
||
| 746 | * |
||
| 747 | * This filter is appied to the $value after it is loaded from the db and before it is returned to the template |
||
| 748 | * |
||
| 749 | * @type filter |
||
| 750 | * @since 3.6 |
||
| 751 | * @date 23/01/13 |
||
| 752 | * |
||
| 753 | * @param $value (mixed) the value which was loaded from the database |
||
| 754 | * @param $post_id (mixed) the $post_id from which the value was loaded |
||
| 755 | * @param $field (array) the field array holding all the field options |
||
| 756 | * |
||
| 757 | * @return $value (mixed) the modified value |
||
| 758 | */ |
||
|
0 ignored issues
–
show
The doc-type
$value could not be parsed: Unknown type name "$value" at position 0. (view supported doc-types)
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types. Loading history...
|
|||
| 759 | |||
| 760 | function format_value( $value, $post_id, $field ) { |
||
|
0 ignored issues
–
show
|
|||
| 761 | |||
| 762 | // bail early if no value |
||
| 763 | if( empty($value) ) { |
||
| 764 | |||
| 765 | // return false as $value may be '' (from DB) which doesn't make much sense |
||
| 766 | return false; |
||
| 767 | |||
| 768 | } |
||
| 769 | |||
| 770 | |||
| 771 | // get posts |
||
| 772 | $posts = acf_get_posts(array( |
||
| 773 | 'post_type' => 'attachment', |
||
| 774 | 'post__in' => $value, |
||
| 775 | )); |
||
| 776 | |||
| 777 | |||
| 778 | |||
| 779 | // update value to include $post |
||
| 780 | foreach( array_keys($posts) as $i ) { |
||
| 781 | |||
| 782 | $posts[ $i ] = acf_get_attachment( $posts[ $i ] ); |
||
| 783 | |||
| 784 | } |
||
| 785 | |||
| 786 | |||
| 787 | // return |
||
| 788 | return $posts; |
||
| 789 | |||
| 790 | } |
||
| 791 | |||
| 792 | |||
| 793 | /* |
||
| 794 | * validate_value |
||
| 795 | * |
||
| 796 | * description |
||
| 797 | * |
||
| 798 | * @type function |
||
| 799 | * @date 11/02/2014 |
||
| 800 | * @since 5.0.0 |
||
| 801 | * |
||
| 802 | * @param $post_id (int) |
||
| 803 | * @return $post_id (int) |
||
| 804 | */ |
||
|
0 ignored issues
–
show
The doc-type
$post_id could not be parsed: Unknown type name "$post_id" at position 0. (view supported doc-types)
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types. Loading history...
|
|||
| 805 | |||
| 806 | View Code Duplication | function validate_value( $valid, $value, $field, $input ){ |
|
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. Loading history...
|
|||
| 807 | |||
| 808 | if( empty($value) || !is_array($value) ) { |
||
| 809 | |||
| 810 | $value = array(); |
||
| 811 | |||
| 812 | } |
||
| 813 | |||
| 814 | |||
| 815 | if( count($value) < $field['min'] ) { |
||
| 816 | |||
| 817 | $valid = _n( '%s requires at least %s selection', '%s requires at least %s selections', $field['min'], 'acf' ); |
||
| 818 | $valid = sprintf( $valid, $field['label'], $field['min'] ); |
||
| 819 | |||
| 820 | } |
||
| 821 | |||
| 822 | |||
| 823 | return $valid; |
||
| 824 | |||
| 825 | } |
||
| 826 | |||
| 827 | |||
| 828 | } |
||
| 829 | |||
| 830 | new acf_field_gallery(); |
||
| 831 | |||
| 832 | endif; |
||
| 833 | |||
| 834 | ?> |
||
| 835 |
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.