|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Class used to add custom classes to gallery links |
|
4
|
|
|
* Date: 04/06/2017 |
|
5
|
|
|
*/ |
|
6
|
|
|
if ( ! class_exists( 'FooGallery_Attachment_Custom_Class' ) ) { |
|
7
|
|
|
|
|
8
|
|
|
class FooGallery_Attachment_Custom_Class { |
|
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
function __construct() { |
|
|
|
|
|
|
11
|
|
|
add_filter( 'foogallery_attachment_custom_fields', array( $this, 'add_custom_class_field' ) ); |
|
12
|
|
|
add_filter( 'foogallery_attachment_html_link_attributes', array( $this, 'alter_class_attributes' ), 99, 3 ); |
|
13
|
|
|
} |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Adds a custom class field to the attachments |
|
17
|
|
|
* |
|
18
|
|
|
* @param $fields array |
|
19
|
|
|
* |
|
20
|
|
|
* @return array |
|
21
|
|
|
*/ |
|
22
|
|
|
function add_custom_class_field( $fields ) { |
|
|
|
|
|
|
23
|
|
|
$fields['foogallery_custom_class'] = array( |
|
24
|
|
|
'label' => __( 'Custom Class', 'foogallery' ), |
|
25
|
|
|
'input' => 'text', |
|
26
|
|
|
'helps' => __( 'Add extra classes to the attachment', 'foogallery' ), |
|
27
|
|
|
'exclusions' => array( 'audio', 'video' ), |
|
28
|
|
|
); |
|
29
|
|
|
|
|
30
|
|
|
return $fields; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Alters the actual link output to include the custom class added |
|
35
|
|
|
* |
|
36
|
|
|
* @uses "foogallery_attachment_html_link_attributes" filter |
|
37
|
|
|
* |
|
38
|
|
|
* @param $attr |
|
39
|
|
|
* @param $args |
|
40
|
|
|
* @param object|FooGalleryAttachment $object |
|
41
|
|
|
* |
|
42
|
|
|
* @return array |
|
43
|
|
|
*/ |
|
44
|
|
|
function alter_class_attributes( $attr, $args, $object ) { |
|
|
|
|
|
|
45
|
|
|
$custom_class = get_post_meta( $object->ID, '_foogallery_custom_class', true ); |
|
46
|
|
|
|
|
47
|
|
|
if ( ! isset( $attr[ 'class' ] ) ) { |
|
48
|
|
|
$attr[ 'class' ] = $custom_class; |
|
49
|
|
|
}else{ |
|
50
|
|
|
$attr[ 'class' ] .= ' ' . $custom_class; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
//check for any special class names and do some magic! |
|
54
|
|
|
if ( 'nolink' === $custom_class ) { |
|
55
|
|
|
unset( $attr['href'] ); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
return $attr; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
} |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.