$_value ) {
if ( !isset($plugin_options[$_name]) ) {
$plugin_options[$_name] = $_value;
}
}
// set the updated settings
$update_options = true;
}
if ( $update_options )
update_option('twobc_image_gallery_options', $plugin_options);
// add twobc_wpadmin_input_fields for option fields
require_once(self::$plugin_path . 'includes/class_twobc_wpadmin_input_fields_1_0_0.php');
}
/**
* Enqueue CSS and JS - Admin
*/
public static function hook_admin_enqueue_scripts() {
global $typenow;
$current_screen = get_current_screen();
if ( !empty($typenow) && 'attachment' == $typenow ) {
wp_register_script(
'twobc_galleries_js_admin', // handle
self::$plugin_url . 'includes/js/2bc-image-gallery-admin.js', // path
array('jquery'), // dependencies
self::get_version() // version
);
wp_localize_script(
'twobc_galleries_js_admin', // script handle
'meta_image', // variable name
array( // values to pass
'title' => __('Choose or Upload an Image', self::$plugin_text_domain),
'button' => __('Use this image', self::$plugin_text_domain),
)
);
wp_enqueue_media();
wp_enqueue_script(
'twobc_galleries_js_admin' // handle
);
}
// option page specific enqueues
if ( 'settings_page_twobc_imagegallery' == $current_screen->id ) {
wp_enqueue_style('wp-color-picker');
wp_register_script(
'twobc_galleries_js_admin', // handle
self::$plugin_url . 'includes/js/2bc-image-gallery-admin.js', // path
array('wp-color-picker'), // dependencies
self::get_version(), // version
true // in footer
);
wp_enqueue_script(
'twobc_galleries_js_admin' // handle
);
}
}
/**
* Enqueue CSS and JS - Main
*/
public static function hook_wp_enqueue_scripts() {
$plugin_options = self::get_options();
global $post;
// scripts
if (
is_a($post, 'WP_Post')
&& (
( // media gallery page
isset($plugin_options['gallery_page'])
&& -1 != $plugin_options['gallery_page']
&& $post->ID == $plugin_options['gallery_page']
)
|| ( // shortcode found in post content
has_shortcode($post->post_content, '2bc_image_gallery')
)
)
&& ( // ajax is not disabled
empty($plugin_options['disable_ajax'])
|| '1' != $plugin_options['disable_ajax']
)
) {
$twobc_image_gallery_version = self::get_version();
wp_register_script(
'twobc_galleries_js_front', // handle
self::$plugin_url . 'includes/js/2bc-image-gallery-front.js', // source
array('jquery'), // dependencies
$twobc_image_gallery_version, // version
true // load in footer
);
// localize script
$script_options = array(
'gallery' => null,
'page_num' => 1,
'page_id' => get_the_ID(),
'sort_method' => esc_attr($plugin_options['sort_method']),
'sort_order' => esc_attr($plugin_options['sort_order']),
'paginate_galleries' => esc_attr($plugin_options['paginate_galleries']),
'images_per_page' => esc_attr($plugin_options['images_per_page']),
'separate_galleries' => esc_attr($plugin_options['separate_galleries']),
'show_months' => esc_attr($plugin_options['show_months']),
'parents' => '',
);
wp_localize_script('twobc_galleries_js_front', 'script_options', $script_options);
// localize for ajax
$ajax_options = array(
'ajax_url' => admin_url('admin-ajax.php'),
'ajax_nonce' => wp_create_nonce('twobc_image_gallery_ajax'),
);
wp_localize_script('twobc_galleries_js_front', 'ajax_object', $ajax_options);
wp_enqueue_script('twobc_galleries_js_front');
wp_register_script(
'twobc_galleries_js_picomodal', // handle
self::$plugin_url . 'includes/js/2bc-image-gallery-picomodal.js', // source
array('jquery'), // dependencies
$twobc_image_gallery_version // version
);
wp_enqueue_script('twobc_galleries_js_picomodal');
// styles
wp_register_style(
'twobc_galleries_css_core', // handle
self::$plugin_url . 'includes/css/2bc-image-gallery-core.css', // url source
array(), // dependencies
$twobc_image_gallery_version // version
);
wp_enqueue_style('twobc_galleries_css_core');
// cosmetic style
if (
empty($plugin_options['hide_style'])
|| '1' != $plugin_options['hide_style']
) {
wp_register_style(
'twobc_galleries_css_cosmetic', // handle
self::$plugin_url . 'includes/css/2bc-image-gallery-cosmetic.css', // url source
array('twobc_galleries_css_core'), // dependencies
$twobc_image_gallery_version // version
);
wp_enqueue_style('twobc_galleries_css_cosmetic');
}
}
}
/**
* Register taxonomy - twobc_img_galleries
*/
public static function hook_init() {
load_plugin_textdomain(self::$plugin_text_domain, false, self::$plugin_path . 'lang');
$tax_galleries_labels = apply_filters(
'twobc_image_gallery_tax_labels', // filter name
array( // filter argument
'name' => _x('Galleries', 'taxonomy general name', self::$plugin_text_domain),
'singular_name' => _x('Gallery', 'taxonomy singular name', self::$plugin_text_domain),
'menu_name' => __('Galleries', self::$plugin_text_domain),
'search_items' => __('Search Galleries', self::$plugin_text_domain),
'all_items' => __('All Galleries', self::$plugin_text_domain),
'edit_item' => __('Edit Gallery', self::$plugin_text_domain),
'view_item' => __('View Gallery', self::$plugin_text_domain),
'update_item' => __('Update Gallery', self::$plugin_text_domain),
'add_new_item' => __('Add New Gallery', self::$plugin_text_domain),
'new_item_name' => __('New Gallery Name', self::$plugin_text_domain),
'popular_items' => __('Popular Galleries', self::$plugin_text_domain), // non-hierarchical only
'separate_items_with_commas' => __('Separate galleries with commas', self::$plugin_text_domain), // non-hierarchical only
'add_or_remove_items' => __('Add or remove galleries', self::$plugin_text_domain), // JS disabled, non-hierarchical only
'choose_from_most_used' => __('Choose from the most used galleries', self::$plugin_text_domain),
'not_found' => __('No galleries found', self::$plugin_text_domain),
)
);
$tax_galleries_args = apply_filters(
'twobc_image_gallery_tax_args', // filter name
array( // filter argument
'hierarchical' => false,
'labels' => $tax_galleries_labels,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'update_count_callback' => '_update_generic_term_count',
'query_var' => true,
'rewrite' => false,
'sort' => false,
)
);
register_taxonomy('twobc_img_galleries', array('attachment'), $tax_galleries_args);
}
/**
* Add admin menu - twobc_imagegallery
*/
public static function hook_admin_menu() {
add_options_page(
__('2BC Image Gallery', self::$plugin_text_domain), // page title
__('2BC Image Gallery', self::$plugin_text_domain), // menu title
'manage_options', // capability required
'twobc_imagegallery', // page slug
array(self::get_instance(), 'settings_page_cb') // display callback
);
}
/**
* Add settings page and settings - twobc_image_gallery_options
*/
public static function hook_admin_init() {
$checkbox_value = '1';
$core = self::get_instance();
// admin menu and pages
register_setting(
'twobc_image_gallery_options', // option group name, declared with settings_fields()
'twobc_image_gallery_options', // option name, best to set same as group name
array($core, 'options_sanitize_cb') // sanitization callback
);
// SECTION - GENERAL
$section = 'general';
add_settings_section(
'twobc_image_gallery_options_' . $section, // section HTML id
__('Gallery Options', self::$plugin_text_domain), // section title
array($core, 'options_general_cb'), // display callback
'twobc_imagegallery' // page to display on
);
// Gallery Page
$field = 'gallery_page';
$current_pages_args = array(
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
'post_type' => 'page',
);
// get all current pages to populate dropdown
$gallery_page_options = array(
'-1' => __('Select a page…', self::$plugin_text_domain),
);
$current_pages = get_posts($current_pages_args);
if ( !empty($current_pages) && !is_wp_error($current_pages) ) {
foreach ( $current_pages as $page_obj ) {
$gallery_page_options[$page_obj->ID] = esc_html($page_obj->post_title . ' - ID: ' . $page_obj->ID);
}
}
add_settings_field(
$field, // field HTML id
__('Gallery Page', self::$plugin_text_domain), // field title
array($core, 'option_field'), // display callback
'twobc_imagegallery', // page to display on
'twobc_image_gallery_options_' . $section, // section to display in
array( // additional arguments
'type' => 'select',
'name' => $field,
'description' => __('Select a page to display galleries on. The shortcode [2bc_image_gallery] can be used for manual display instead of setting a page here.', self::$plugin_text_domain),
'options' => $gallery_page_options,
)
);
// Gallery Page Content
$field = 'page_content';
add_settings_field(
$field, // field HTML id
__('Page Content', self::$plugin_text_domain), // field title
array($core, 'option_field'), // display callback
'twobc_imagegallery', // page to display on
'twobc_image_gallery_options_' . $section, // section to display in
array( // additional arguments
'type' => 'radio',
'name' => $field,
'description' => __('Control how page content is handled on the Gallery Page', self::$plugin_text_domain),
'options' => array(
'before' => __('Before page content', self::$plugin_text_domain),
'templatetag' => __('Replace %%2bc_image_gallery%% template tag', self::$plugin_text_domain),
'after' => __('After page content', self::$plugin_text_domain),
'replace' => __('Replace all page content with gallery', self::$plugin_text_domain),
),
)
);
// Sort Method
$field = 'sort_method';
add_settings_field(
$field, // field HTML id
__('Sort Method', self::$plugin_text_domain), // field title
array($core, 'option_field'), // display callback
'twobc_imagegallery', // page to display on
'twobc_image_gallery_options_' . $section, // section to display in
array( // additional arguments
'type' => 'select',
'name' => $field,
'description' => __('Select how gallery images are sorted: by uploaded date, alphbetically by filename, or random', self::$plugin_text_domain),
'options' => array(
'date' => __('Date uploaded', self::$plugin_text_domain),
'title' => __('Filename', self::$plugin_text_domain),
'rand' => __('Random', self::$plugin_text_domain),
),
)
);
// Sort Order
$field = 'sort_order';
add_settings_field(
$field, // field HTML id
__('Sort Order', self::$plugin_text_domain), // field title
array($core, 'option_field'), // display callback
'twobc_imagegallery', // page to display on
'twobc_image_gallery_options_' . $section, // section to display in
array( // additional arguments
'type' => 'select',
'name' => $field,
'description' => __('Which direction to sort, ascending (1, 2, 3) or descending (9, 8, 7)', self::$plugin_text_domain),
'options' => array(
'desc' => __('Descending', self::$plugin_text_domain),
'asc' => __('Ascending', self::$plugin_text_domain),
),
)
);
// Paginate Galleries
$field = 'paginate_galleries';
add_settings_field(
$field, // field HTML id
__('Paginate Galleries', self::$plugin_text_domain), // field title
array($core, 'option_field'), // display callback
'twobc_imagegallery', // page to display on
'twobc_image_gallery_options_' . $section, // section to display in
array( // additional arguments
'type' => 'checkbox',
'name' => $field,
'value' => $checkbox_value,
'description' => __('Break up galleries into pages', self::$plugin_text_domain),
)
);
// Images Per Page
$field = 'images_per_page';
add_settings_field(
$field, // field HTML id
__('Images Per Page', self::$plugin_text_domain), // field title
array($core, 'option_field'), // display callback
'twobc_imagegallery', // page to display on
'twobc_image_gallery_options_' . $section, // section to display in
array( // additional arguments
'type' => 'number',
'name' => $field,
'description' => __('How many images to display per page, only applies if pagination is enabled', self::$plugin_text_domain),
)
);
// Default Gallery Thumb
$field = 'default_gallery_thumb';
add_settings_field(
$field, // field HTML id
__('Gallery Thumb Source', self::$plugin_text_domain), // field title
array($core, 'option_field'), // display callback
'twobc_imagegallery', // page to display on
'twobc_image_gallery_options_' . $section, // section to display in
array( // additional arguments
'type' => 'select',
'name' => $field,
'description' => __('If the gallery does not have a custom thumbnail, decide how to generate one', self::$plugin_text_domain),
'options' => array(
'last' => __('Last image added', self::$plugin_text_domain),
'first' => __('First image added', self::$plugin_text_domain),
'random' => __('Random', self::$plugin_text_domain),
),
)
);
// Background Color - Thumb
$field = 'bg_thumb';
add_settings_field(
$field, // field HTML id
__('Background Color: Image Thumbs', self::$plugin_text_domain), // field title
array($core, 'color_picker_field'), // display callback
'twobc_imagegallery', // page to display on
'twobc_image_gallery_options_' . $section, // section to display in
array( // additional arguments
'type' => 'colorpicker',
'name' => $field,
'value' => $checkbox_value,
'description' => __('Choose an optional custom color for the image thumbnail backgrounds', self::$plugin_text_domain),
'default_color' => '#cccccc',
)
);
// Hide Style
$field = 'hide_style';
add_settings_field(
$field, // field HTML id
__('Hide Default Stying', self::$plugin_text_domain), // field title
array($core, 'option_field'), // display callback
'twobc_imagegallery', // page to display on
'twobc_image_gallery_options_' . $section, // section to display in
array( // additional arguments
'type' => 'checkbox',
'name' => $field,
'value' => $checkbox_value,
'description' => __('Check to not load the default style sheet, and create the styling in the theme CSS instead', self::$plugin_text_domain),
)
);
// Disable AJAX
$field = 'disable_ajax';
add_settings_field(
$field, // field HTML id
__('Disable AJAX', self::$plugin_text_domain), // field title
array($core, 'option_field'), // display callback
'twobc_imagegallery', // page to display on
'twobc_image_gallery_options_' . $section, // section to display in
array( // additional arguments
'type' => 'checkbox',
'name' => $field,
'value' => $checkbox_value,
'description' => __('Check to disable AJAX calls. Do this if galleries are not loading when clicked. This will mean that the page will refresh with the new content.', self::$plugin_text_domain),
)
);
// SECTION - MODAL OPTIONS
$section = 'modal';
add_settings_section(
'twobc_image_gallery_options_' . $section, // section HTML id
__('Modal Options', self::$plugin_text_domain), // section title
array($core, 'options_general_cb'), // display callback
'twobc_imagegallery' // page to display on
);
// Display Title
$field = 'display_title';
add_settings_field(
$field, // field HTML id
__('Display Image Title', self::$plugin_text_domain), // field title
array($core, 'option_field'), // display callback
'twobc_imagegallery', // page to display on
'twobc_image_gallery_options_' . $section, // section to display in
array( // additional arguments
'type' => 'checkbox',
'name' => $field,
'value' => $checkbox_value,
'description' => __('Display the image title when viewing a single image in the lightbox', self::$plugin_text_domain),
)
);
// Slideshow Delay
$field = 'slideshow_delay';
add_settings_field(
$field, // field HTML id
__('Slideshow Delay', self::$plugin_text_domain), // field title
array($core, 'option_field'), // display callback
'twobc_imagegallery', // page to display on
'twobc_image_gallery_options_' . $section, // section to display in
array( // additional arguments
'type' => 'number',
'name' => $field,
'description' => __('Enter how many milliseconds to wait before displaying the next slide, when the slideshow is playing', self::$plugin_text_domain),
)
);
// Background Color - Thumb
$field = 'bg_modal';
add_settings_field(
$field, // field HTML id
__('Background Color: Modal Window', self::$plugin_text_domain), // field title
array($core, 'color_picker_field'), // display callback
'twobc_imagegallery', // page to display on
'twobc_image_gallery_options_' . $section, // section to display in
array( // additional arguments
'type' => 'colorpicker',
'name' => $field,
'value' => $checkbox_value,
'description' => __('Choose an optional custom color for the modal window background', self::$plugin_text_domain),
'default_color' => '#fefefe',
)
);
/*
// Display Description
$field = 'display_description';
add_settings_field(
$field, // field HTML id
__('Display Image Description', self::$plugin_text_domain), // field title
array($core, 'option_field'), // display callback
'twobc_imagegallery', // page to display on
'twobc_image_gallery_options_' . $section, // section to display in
array( // additional arguments
'type' => 'checkbox',
'name' => $field,
'value' => $checkbox_value,
'description' => __('Display the image description when viewing a single image in the lightbox', self::$plugin_text_domain),
)
);
*/
/*
// Display Galleries
$field = 'display_galleries';
add_settings_field(
$field, // field HTML id
__('Display Share Buttons', self::$plugin_text_domain), // field title
array($core, 'option_field'), // display callback
'twobc_imagegallery', // page to display on
'twobc_image_gallery_options_' . $section, // section to display in
array( // additional arguments
'type' => 'checkbox',
'name' => $field,
'value' => $checkbox_value,
'description' => __('Display social share buttons when viewing a single image in the lightbox', self::$plugin_text_domain),
)
);
*/
/*
// Display Sharing Buttons
$field = 'display_share_buttons';
add_settings_field(
$field, // field HTML id
__('Display Share Buttons', self::$plugin_text_domain), // field title
array($core, 'option_field'), // display callback
'twobc_imagegallery', // page to display on
'twobc_image_gallery_options_' . $section, // section to display in
array( // additional arguments
'type' => 'checkbox',
'name' => $field,
'value' => $checkbox_value,
'description' => __('Display social share buttons when viewing a single image in the lightbox', self::$plugin_text_domain),
)
);
*/
// SECTION - DATE GALLERIES
$section = 'calendar';
add_settings_section(
'twobc_image_gallery_options_' . $section, // section HTML id
__('Calendar Based Galleries', self::$plugin_text_domain), // section title
array($core, 'options_general_cb'), // display callback
'twobc_imagegallery' // page to display on
);
// Add Calendar Galleries
$field = 'add_calendar';
add_settings_field(
$field, // field HTML id
__('Add Calendar Based Galleries', self::$plugin_text_domain), // field title
array($core, 'option_field'), // display callback
'twobc_imagegallery', // page to display on
'twobc_image_gallery_options_' . $section, // section to display in
array( // additional arguments
'type' => 'checkbox',
'name' => $field,
'value' => $checkbox_value,
'description' => __('Add calendar galleries (i.e. January, 2012) to uploaded images', self::$plugin_text_domain),
)
);
// Separate Calendar Galleries
$field = 'separate_galleries';
add_settings_field(
$field, // field HTML id
__('Separate Calendar Galleries', self::$plugin_text_domain), // field title
array($core, 'option_field'), // display callback
'twobc_imagegallery', // page to display on
'twobc_image_gallery_options_' . $section, // section to display in
array( // additional arguments
'type' => 'checkbox',
'name' => $field,
'value' => $checkbox_value,
'description' => __('Show calendar galleries in their own separate section', self::$plugin_text_domain),
)
);
// Show Month Galleries
$field = 'show_months';
add_settings_field(
$field, // field HTML id
__('Show Month Galleries', self::$plugin_text_domain), // field title
array($core, 'option_field'), // display callback
'twobc_imagegallery', // page to display on
'twobc_image_gallery_options_' . $section, // section to display in
array( // additional arguments
'type' => 'checkbox',
'name' => $field,
'value' => $checkbox_value,
'description' => __('Display month-based galleries on the main page, only applies if Separate Calendar Galleries is checked', self::$plugin_text_domain),
)
);
}
/**
* Build a form field
*
* @param $field_args
*/
public static function option_field($field_args) {
$twobc_image_gallery_wpadmin_fields = new twobc_wpadmin_input_fields_1_0_0(
array(
'nonce' => false,
)
);
// parse field args
$field_args = array_merge($twobc_image_gallery_wpadmin_fields->field_default_args(), $field_args);
// get current value
$current_value = self::get_options();
$field_args['current_value'] = (isset($current_value[$field_args['name']]) ? $current_value[$field_args['name']] : '');
// field nonce
wp_nonce_field(
'twobc_image_gallery_options_nonce', // action
'twobc_image_gallery_options_' . $field_args['name'] // custom name
);
// fix name
$field_args['name'] = 'twobc_image_gallery_options[' . $field_args['name'] . ']';
echo '
';
}
public static function color_picker_field($field_args) {
$current_value = self::get_options();
$default_color = ( !empty($field_args['default_color']) ? $field_args['default_color'] : '' );
wp_nonce_field(
'twobc_image_gallery_options_nonce', // action
'twobc_image_gallery_options_' . $field_args['name'] // custom name
);
echo '
';
}
/**
* Get current plugin options
*
* @return mixed|void
*/
public static function get_options() {
if ( empty(self::$plugin_options) )
self::$plugin_options = get_option('twobc_image_gallery_options');
return self::$plugin_options;
}
/**
* Get CSS for admin options screen
*
* @return string
*/
private static function get_admin_css() {
$return = 'h3 {
background: #ccc;
margin: 1em -10px;
padding: 20px 10px;
}
input[type="number"].small-text {
width: 80px;
max-width: 80px;
}
';
return $return;
}
/**
* Settings page callback
*/
public static function settings_page_cb() {
//must check that the user has the required capability
if ( !current_user_can('manage_options') ) {
wp_die(__('You do not have sufficient permissions to access this page.', self::$plugin_text_domain));
}
$image_gallery_admin_css = self::get_admin_css();
if ( !empty($image_gallery_admin_css) ) {
echo '
';
}
echo '
';
settings_errors(
'twobc_image_gallery_options', // settings group name
false, // re-sanitize values on errors
true // hide on update - set to true to get rid of duplicate Updated messages
);
echo '
';
// $image_gallery_js = self::get_admin_js();
if ( !empty($image_gallery_js) ) {
echo '
';
}
}
/**
* Settings section callback
*/
public static function options_general_cb($section) {
// intentionally left blank
}
/**
* Settings page sanitization callback
*
* @param $saved_settings
*
* @return mixed
*/
public static function options_sanitize_cb($saved_settings) {
$settings_errors = array(
'updated' => false,
'error' => array(),
);
$known_fields = array(
'gallery_page' => 'dropdown',
'page_content' => 'radio',
'sort_method' => 'dropdown',
'sort_order' => 'dropdown',
'paginate_galleries' => 'checkbox',
'images_per_page' => 'number',
'default_gallery_thumb' => 'dropdown',
'hide_style' => 'checkbox',
'disable_ajax' => 'checkbox',
'display_title' => 'checkbox',
'slideshow_delay' => 'number',
//'display_description' => 'checkbox',
'display_galleries' => 'checkbox',
//'display_share_buttons' => 'checkbox',
'add_calendar' => 'checkbox',
'separate_galleries' => 'checkbox',
'show_months' => 'checkbox',
'bg_thumb' => 'colorpicker',
'bg_modal' => 'colorpicker',
);
foreach ( $saved_settings as $setting_key => $setting_val ) {
// security checks - nonce, capability
if (
isset($known_fields[$setting_key])
&& check_admin_referer(
'twobc_image_gallery_options_nonce', // nonce action
'twobc_image_gallery_options_' . $setting_key // query arg, nonce name
)
&& current_user_can('manage_options')
) {
switch ( $known_fields[$setting_key] ) {
case 'checkbox' :
$saved_settings[$setting_key] = '1';
$settings_errors['updated'] = true;
break;
case 'number' :
if ( is_numeric($setting_val) ) {
$saved_settings[$setting_key] = intval($setting_val);
$settings_errors['updated'] = true;
} else {
unset($saved_settings[$setting_key]);
}
break;
case 'dropdown' :
case 'radio' :
$saved_settings[$setting_key] = sanitize_text_field($setting_val);
$settings_errors['updated'] = true;
break;
case 'colorpicker' :
if ( preg_match('|^#([A-Fa-f0-9]{3}){1,2}$|', $setting_val) )
$saved_settings[$setting_key] = ($setting_val);
break;
default :
// unknown field type? Shouldn't happen, but unset to be safe
unset($saved_settings[$setting_key]);
}
} else { // unknown field or security fail, unset to be safe
unset($saved_settings[$setting_key]);
}
}
// separate validation for un-checked checkboxes
foreach ( $known_fields as $field_name => $field_type ) {
if (
'checkbox' == $field_type
&& !isset($saved_settings[$field_name])
) {
$saved_settings[$field_name] = '0';
$settings_errors['updated'] = true;
}
}
// register errors
if ( !empty($settings_errors['errors']) && is_array($settings_errors['errors']) ) {
foreach ( $settings_errors['errors'] as $error ) {
add_settings_error(
'twobc_image_gallery_options', // Slug title of the setting
'twobc_image_gallery_options_error', // Slug of error
$error, // Error message
'error' // Type of error (**error** or **updated**)
);
}
}
if ( true === $settings_errors['updated'] ) {
add_settings_error(
'twobc_image_gallery_options', // Slug title of the setting
'twobc_image_gallery_options_error', // Slug of error
__('Settings saved.', self::$plugin_text_domain), // Error message
'updated' // Type of error (**error** or **updated**)
);
}
// update the static class property
self::$plugin_options = $saved_settings;
// set plugin version number
$saved_settings['version'] = self::get_version();
return $saved_settings;
}
/**
* Add admin notice to Image Gallery page
*/
public static function hook_admin_notices() {
$image_gallery_options = self::get_options();
//global $post;
$post_id = (!empty($_GET['post']) ? intval($_GET['post']) : '');
$current_screen = get_current_screen();
if (
isset($image_gallery_options['gallery_page'])
&& 'page' == $current_screen->id
&& 'edit' == $current_screen->parent_base
&& $post_id == $image_gallery_options['gallery_page']
) {
echo '
';
_e('This page is currently being used to display the 2BC Media Gallery', self::$plugin_text_domain);
echo '
';
}
}
/**
* Add custom fields to Add Gallery page - featured gallery image picker
*/
public static function hook_taxonomy_add_form_fields() {
echo '
';
echo '
';
echo '
';
echo '
';
_e('Choose or upload a picture to be the galleries featured image', self::$plugin_text_domain);
echo '
';
echo '
';
}
/**
* Add custom fields to Edit Gallery page - featured gallery image picker
*
* @param $term
*/
public static function hook_taxonomy_edit_form_fields($term) {
// retrieve the existing value(s) for this meta field. This returns an array
$term_meta = get_option('taxonomy_' . $term->term_id);
echo '
';
echo '
';
echo '
';
echo '
';
echo '
';
_e('Choose or upload a picture to be the galleries featured image', self::$plugin_text_domain);
echo '
';
echo '
';
echo '
';
}
/**
* Save custom fields on Gallery pages
*
* @param $term_id
*/
public static function hook_save_twobc_img_galleries($term_id) {
if ( isset($_REQUEST['twobc_img_galleries_meta']) ) {
$term_meta = (get_option('taxonomy_' . $term_id));
$tax_keys = array_keys($_REQUEST['twobc_img_galleries_meta']);
foreach ( $tax_keys as $a_key ) {
$term_meta[sanitize_text_field($a_key)] = sanitize_text_field($_POST['twobc_img_galleries_meta'][$a_key]);
}
// save the option array
update_option('taxonomy_' . $term_id, $term_meta);
}
}
/**
* Add ID Column to taxonomy page - new column
*
* @param $columns
*
* @return mixed
*/
public static function hook_edit_twobc_img_galleries_cols($columns) {
$columns['id'] = __('ID', self::$plugin_text_domain);
return $columns;
}
/**
* Add ID Column to taxonomy page - value
*
* @param $value
* @param $col_name
* @param $id
*
* @return int|null
*/
public static function hook_twobc_img_galleries_custom_col($value, $col_name, $id) {
$return = null;
if ( 'id' == $col_name )
$return = intval($id);
return $return;
}
/**
* Add ID Column to taxonomy page - sortable (numeric)
*
* @param $columns
*
* @return mixed
*/
public static function hook_twobc_img_galleries_sortable_cols($columns) {
$columns['id'] = 'id';
return $columns;
}
/**
* Filter the_content on Image Gallery page according to plugin options
*
* @param $content
*
*
* @return mixed|string|void
*/
public static function hook_the_content($content) {
$plugin_options = self::get_options();
if (
isset($plugin_options['gallery_page'])
&& '-1' != $plugin_options['gallery_page']
&& is_page($plugin_options['gallery_page'])
&& isset($plugin_options['page_content'])
) {
// prepare arguments
$twobc_image_gallery_args = self::get_display_args_default();
$twobc_image_gallery_args['page_id'] = $plugin_options['gallery_page'];
switch ( $plugin_options['page_content'] ) {
// before
case 'before' :
$content = self::get_display($twobc_image_gallery_args) . $content;
break;
// replace
case 'replace' :
$content = self::get_display($twobc_image_gallery_args);
break;
// templatetag - %%2bc_image_gallery%%
case 'templatetag' :
$limit = 1;
$content = str_replace(
'%%2bc_image_gallery%%', // needle
self::get_display($twobc_image_gallery_args), // replacement
$content, // haystack
$limit // limit
);
break;
// after
// default
case 'after' :
default :
$content .= self::get_display($twobc_image_gallery_args);
}
}
return $content;
}
/**
* Get the URL for the page the gallery is being displayed on
*
* @param null $page_id
* @param array $query_args
*
* @return bool|string
*/
public static function get_gallery_url($page_id = null, $query_args = array()) {
if ( empty($page_id) ) {
global $post;
$page_id = $post->ID;
}
$gallery_url = get_permalink($page_id);
// optional query args
if (
!empty($query_args)
&& is_array($query_args)
) {
$gallery_url = add_query_arg(
$query_args, // query args to add
$gallery_url // old query or uri
);
}
return $gallery_url;
}
/**
* Get the default plugin options
*
* @return array
*/
private static function get_options_default() {
$default_options = array(
'gallery_page' => -1,
'page_content' => 'after',
'sort_method' => 'date',
'sort_order' => 'DESC',
'paginate_galleries' => '1',
'images_per_page' => '60',
'default_gallery_thumb' => 'last',
'hide_style' => '0',
'disable_ajax' => '0',
'display_title' => '1',
'slideshow_delay' => '5000',
//'display_description' => '0',
'display_galleries' => '0',
//'display_share_buttons' => '0',
'add_calendar' => '0',
'separate_galleries' => '0',
'show_month_galleries' => '0',
'bg_thumb' => '#cccccc',
'bg_modal' => '#fefefe',
'version' => self::get_version(),
);
return $default_options;
}
/**
* Get the default arguments for get_display and the shortcode
*
* @return array
*/
public static function get_display_args_default() {
$image_gallery_options = self::get_options();
$default_args = array(
'display_gallery' => '',
'page_num' => '1',
'page_id' => get_the_ID(),
'parents' => '',
'galleries' => '',
'sort_method' => $image_gallery_options['sort_method'],
'sort_order' => $image_gallery_options['sort_order'],
'paginate_galleries' => $image_gallery_options['paginate_galleries'],
'images_per_page' => $image_gallery_options['images_per_page'],
'default_gallery_thumb' => $image_gallery_options['default_gallery_thumb'],
'display_title' => $image_gallery_options['display_title'],
'slideshow_delay' => $image_gallery_options['slideshow_delay'],
'separate_galleries' => $image_gallery_options['separate_galleries'],
'show_months' => $image_gallery_options['show_months'],
'back_button' => '',
'noajax' => '',
);
return $default_args;
}
/**
* Add calendar based galleries to new uploads
*
* @param $file
* @param $attachment_id
*
* @return mixed
*/
public static function hook_update_attached_file($file, $attachment_id) {
$attachment_obj = get_post($attachment_id);
if ( !empty($attachment_obj) && false !== strpos($attachment_obj->post_mime_type, 'image') ) {
$plugin_options = self::get_options();
// image uploaded
if ( !empty($plugin_options['add_calendar']) ) {
// get month and year from post_date
$post_date = strtotime($attachment_obj->post_date);
$post_month = apply_filters('twobc_image_gallery_add_month', date('F', $post_date));
$post_year = apply_filters('twobc_image_gallery_add_year', date('Y', $post_date));
// add galleries to attachment
wp_add_object_terms(
$attachment_id, // attachment id
array( // terms
$post_month,
$post_year,
),
'twobc_img_galleries' // taxonomy
);
}
}
return $file;
}
/**
* Main Image Gallery display function
*
* @param $args
*
* @return mixed|string|void
*/
public static function get_display($args) {
$output = '';
//$args = $this->display_args_current;
$default_args = self::get_display_args_default();
// set args over-ride flag
$args_override = false;
if ( !empty($args) ) {
$args_override = $args;
}
// set GET over-ride flag
if ( !empty($_GET) ) {
foreach ( $_GET as $_get_name => $_get_value ) {
$get_name = esc_attr($_get_name);
$get_value = esc_attr($_get_value);
if ( isset($default_args[$get_name]) ) {
$args_override[$get_name] = $get_value;
$args[$get_name] = $get_value;
}
}
}
$args = wp_parse_args($args, $default_args);
// update display_args_current with any changes
//$this->display_args_current = $args;
// optional nested galleries - build array from args if present
$parent_galleries = array();
if (
!empty($args['parents'])
&& is_string($args['parents'])
) {
$parent_galleries = explode(',', $args['parents']);
if ( 1 == count($parent_galleries) ) {
$parent_galleries = reset($parent_galleries);
}
}
// shortcut to display a gallery - for non-JS cases
if (
isset($args['display_gallery'])
&& is_numeric($args['display_gallery'])
) {
$shortcut_args = array(
'page_id' => get_the_ID(),
'term_id' => $args['display_gallery'],
'page_num' => $args['page_num'],
'sort_order' => $args['sort_order'],
'sort_method' => $args['sort_method'],
'paginate_galleries' => $args['paginate_galleries'],
'images_per_page' => $args['images_per_page'],
'back_button' => $args['back_button'],
);
$term_obj = get_term($shortcut_args['term_id'], 'twobc_img_galleries');
$shortcut_args['term_obj'] = $term_obj;
$shortcut_args['term_title'] = $term_obj->name;
$shortcut_args['parents'] = $args['parents'];
$output = '
';
$output .= '
';
$output .= '
' . self::get_gallery_html($shortcut_args) . '
';
$output .= '
count;
// add custom data attribute as JSON object
if ( !empty($custom_data_attb) )
$output .= ' data-twobcig-args=\'' . json_encode($custom_data_attb) . '\'';
$output .= '>
';
$output .= '
';
return $output;
}
// find out if we were passed galleries to display
$optional_galleries = array();
if (
!empty($args['galleries'])
&& is_string($args['galleries'])
) {
$optional_galleries = explode(',', $args['galleries']);
if (
!empty($optional_galleries)
&& is_array($optional_galleries)
) {
foreach ( $optional_galleries as &$gallery_id ) {
$gallery_id = esc_attr(trim($gallery_id));
}
}
}
$get_terms_args = array();
if ( !empty($optional_galleries) ) {
foreach ( $optional_galleries as $a_term_id ) {
$get_terms_args['include'][] = $a_term_id;
}
}
// get all current terms
$term_array = get_terms('twobc_img_galleries', $get_terms_args);
// Begin display
$output .= '
$_term_obj ) {
// if name matches a 4 digit number, between 1900 and 2099
if ( 1 == preg_match('/^(19|20)\d{2}$/', $_term_obj->name) ) {
// output this term
$get_thumb_args = array(
'term_id' => $_term_obj->term_id,
'term_name' => $_term_obj->name,
);
if ( !empty($args['parents']) )
$get_thumb_args['parents'] = $args['parents'];
$years_output[] = ' ' . self::get_thumb_html($get_thumb_args);
// remove from $term_array
unset($term_array[$key]);
}
}
if ( !empty($years_output) ) {
// add opening html
array_unshift($years_output, '
' . apply_filters('twobc_image_gallery_year_title', __('Galleries by year', self::$plugin_text_domain)) . '
');
// add closing html
$years_output[] = '
';
foreach ( $years_output as $_output ) {
$output .= $_output;
}
}
if (
isset($args['show_months'])
&& '1' == $args['show_months']
) {
$display_months = true;
} else {
$display_months = false;
}
// build array of calendar months
$calendar_months = apply_filters(
'twobc_image_gallery_calendar_months', // filter name
array( // filter argument
__('January', self::$plugin_text_domain),
__('February', self::$plugin_text_domain),
__('March', self::$plugin_text_domain),
__('April', self::$plugin_text_domain),
__('May', self::$plugin_text_domain),
__('June', self::$plugin_text_domain),
__('July', self::$plugin_text_domain),
__('August', self::$plugin_text_domain),
__('September', self::$plugin_text_domain),
__('October', self::$plugin_text_domain),
__('November', self::$plugin_text_domain),
__('December', self::$plugin_text_domain),
)
);
$month_output = array();
foreach ( $term_array as $_key => $_term_obj ) {
// if name matches a calendar month
foreach ( $calendar_months as $_month_key => $_month_name ) {
if ( $_term_obj->name == $_month_name ) {
if ( $display_months ) {
// store this entry in the correct order
$get_thumb_args = array(
'term_id' => $_term_obj->term_id,
'term_name'=> $_term_obj->name,
);
if (!empty($args['parents']))
$get_thumb_args['parents'] = $args['parents'];
$month_output[$_month_key] = self::get_thumb_html($get_thumb_args);
}
// remove from $term_array
unset($term_array[$_key]);
}
}
}
if (
!empty($month_output)
&& $display_months
) {
// sort array by key
ksort($month_output, SORT_NUMERIC);
// add opening html
array_unshift($month_output, '
' . apply_filters('twobc_image_gallery_month_title', __('Galleries by month', self::$plugin_text_domain)) . '
');
// add closing html
$month_output[] = '
';
foreach ( $month_output as $_output ) {
$output .= $_output;
}
}
// check to see if we have any custom galleries left to display
// for custom section title
if ( !empty($term_array) ) {
$custom_div_open = true;
$output .= '