-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',
'add_calendar' => '0',
'separate_galleries' => '0',
'show_month_galleries' => '0',
'version' => $twobc_image_gallery_version,
);
add_action('plugins_loaded', 'twobc_image_gallery_plugins_loaded');
/**
* Plugin bootstrap
* * Define constants
* * Load textdomain
*
* @action plugins_loaded
*/
function twobc_image_gallery_plugins_loaded() {
//define constants for plugin use
define('TWOBC_IMAGEGALLERY_URL', plugin_dir_url(__FILE__));
define('TWOBC_IMAGEGALLERY_TEXT_DOMAIN', 'TwoBCImageGallery');
load_plugin_textdomain(TWOBC_IMAGEGALLERY_TEXT_DOMAIN, false, basename(dirname(__FILE__)) . '/languages');
// handle install and upgrade
global $twobc_image_gallery_version;
global $twobc_image_gallery_default_values;
$plugin_options = twobc_image_gallery_get_options();
// install check
if ( empty($plugin_options) ) {
// init with default values
update_option('twobc_image_gallery_options', $twobc_image_gallery_default_values);
$plugin_options = $twobc_image_gallery_default_values;
}
// upgrade check
if ( $twobc_image_gallery_version != $plugin_options['version'] ) {
// init any empty db fields to catch any new additions
foreach ($twobc_image_gallery_default_values as $_name => $_value) {
if ( !isset($plugin_options[$_name]) ) {
$plugin_options[$_name] = $_value;
}
}
// set the updated settings
update_option('twobc_image_gallery_options', $plugin_options);
}
}
add_action('admin_enqueue_scripts', 'twobc_image_gallery_admin_enqueue');
/**
* Admin script enqueue functions - 2bc-image-gallery-admin.js
* Adds Gallery Featured Image option
*
* @action admin_enqueue_scripts
*/
function twobc_image_gallery_admin_enqueue() {
// only need to load JS on attachment pages
global $typenow;
if (
!empty($typenow)
&& 'attachment' == $typenow
) {
wp_enqueue_media();
global $twobc_image_gallery_version;
wp_register_script(
'twobc_galleries_js_admin', // handle
TWOBC_IMAGEGALLERY_URL . 'includes/js/2bc-image-gallery-admin.js', // path
array ('jquery'), // dependencies
$twobc_image_gallery_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', TWOBC_IMAGEGALLERY_TEXT_DOMAIN),
'button' => __('Use this image', TWOBC_IMAGEGALLERY_TEXT_DOMAIN),
)
);
wp_enqueue_script('twobc_galleries_js_admin');
}
}
add_action('wp_enqueue_scripts', 'twobc_image_gallery_front_enqueue');
/**
* Front script and CSS enqueue functions
*
* @action wp_enqueue_scripts
*/
function twobc_image_gallery_front_enqueue() {
$plugin_options = twobc_image_gallery_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']
)
) {
global $twobc_image_gallery_version;
wp_register_script(
'twobc_galleries_js_front', // handle
TWOBC_IMAGEGALLERY_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
TWOBC_IMAGEGALLERY_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
TWOBC_IMAGEGALLERY_URL . 'includes/css/2bc-image-gallery-core.css' // url source
);
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
TWOBC_IMAGEGALLERY_URL . 'includes/css/2bc-image-gallery-cosmetic.css' // url source
);
wp_enqueue_style('twobc_galleries_css_cosmetic');
}
}
/***********************/
/*** CUSTOM TAXONOMY ***/
/***********************/
add_action('init', 'twobc_image_gallery_register_taxonomy');
/**
* Register custom taxonomy - twobc_img_galleries
*
* @action init
*
* @see register_taxonomy()
*/
function twobc_image_gallery_register_taxonomy() {
$tax_galleries_labels = apply_filters(
'twobc_image_gallery_tax_labels', // filter name
array( // filter argument
'name' => _x('Galleries', 'taxonomy general name', TWOBC_IMAGEGALLERY_TEXT_DOMAIN),
'singular_name' => _x('Gallery', 'taxonomy singular name', TWOBC_IMAGEGALLERY_TEXT_DOMAIN),
'menu_name' => __('Galleries', TWOBC_IMAGEGALLERY_TEXT_DOMAIN),
'search_items' => __('Search Galleries', TWOBC_IMAGEGALLERY_TEXT_DOMAIN),
'all_items' => __('All Galleries', TWOBC_IMAGEGALLERY_TEXT_DOMAIN),
'edit_item' => __('Edit Gallery', TWOBC_IMAGEGALLERY_TEXT_DOMAIN),
'view_item' => __('View Gallery', TWOBC_IMAGEGALLERY_TEXT_DOMAIN),
'update_item' => __('Update Gallery', TWOBC_IMAGEGALLERY_TEXT_DOMAIN),
'add_new_item' => __('Add New Gallery', TWOBC_IMAGEGALLERY_TEXT_DOMAIN),
'new_item_name' => __('New Gallery Name', TWOBC_IMAGEGALLERY_TEXT_DOMAIN),
'popular_items' => __('Popular Galleries', TWOBC_IMAGEGALLERY_TEXT_DOMAIN), // non-hierarchical only
'separate_items_with_commas' => __('Separate galleries with commas', TWOBC_IMAGEGALLERY_TEXT_DOMAIN), // non-hierarchical only
'add_or_remove_items' => __('Add or remove galleries', TWOBC_IMAGEGALLERY_TEXT_DOMAIN), // JS disabled, non-hierarchical only
'choose_from_most_used' => __('Choose from the most used galleries', TWOBC_IMAGEGALLERY_TEXT_DOMAIN),
'not_found' => __('No galleries found', TWOBC_IMAGEGALLERY_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);
}
/******************/
/*** ADMIN PAGE ***/
/******************/
add_action('admin_menu', 'twobc_image_gallery_settings_menu');
/**
* Add 2BC Image Gallery options page
*
* @action admin_menu
*/
function twobc_image_gallery_settings_menu() {
add_options_page(
'2BC Image Gallery', // page title
'2BC Image Gallery', // menu title
'manage_options', // capability required
'twobc_imagegallery', // page slug
'twobc_image_gallery_settings_page_cb' // display callback
);
}
add_action('admin_init', 'twobc_image_gallery_admin_init');
/**
* Register all plugin options on settings page
*
* @action admin_init
*/
function twobc_image_gallery_admin_init() {
// 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
'twobc_image_gallery_options_sanitize_cb' // sanitization callback
);
// SECTION - GENERAL
$section = 'general';
add_settings_section(
'twobc_image_gallery_options_' . $section, // section HTML id
__('Gallery Options', TWOBC_IMAGEGALLERY_TEXT_DOMAIN), // section title
'twobc_image_gallery_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 (
__('Select a page…', TWOBC_IMAGEGALLERY_TEXT_DOMAIN) => '-1'
);
$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[sanitize_text_field($page_obj->post_title . ' - ID: ' . $page_obj->ID)] = $page_obj->ID;
}
}
add_settings_field(
$field, // field HTML id
__('Gallery Page', TWOBC_IMAGEGALLERY_TEXT_DOMAIN), // field title
'twobc_image_gallery_create_dropdown', // display callback
'twobc_imagegallery', // page to display on
'twobc_image_gallery_options_' . $section, // section to display in
array ( // additional arguments
'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.', TWOBC_IMAGEGALLERY_TEXT_DOMAIN),
'options' => $gallery_page_options,
)
);
// Gallery Page Content
$field = 'page_content';
add_settings_field(
$field, // field HTML id
__('Page Content', TWOBC_IMAGEGALLERY_TEXT_DOMAIN), // field title
'twobc_image_gallery_create_radio', // display callback
'twobc_imagegallery', // page to display on
'twobc_image_gallery_options_' . $section, // section to display in
array ( // additional arguments
'name' => $field,
'description' => __('Control how page content is handled on the Gallery Page', TWOBC_IMAGEGALLERY_TEXT_DOMAIN),
'options' => array (
__('Before page content', TWOBC_IMAGEGALLERY_TEXT_DOMAIN) => 'before',
__('Replace %%2bc_image_gallery%% template tag', TWOBC_IMAGEGALLERY_TEXT_DOMAIN) => 'templatetag',
__('After page content', TWOBC_IMAGEGALLERY_TEXT_DOMAIN) => 'after',
__('Replace all page content with gallery', TWOBC_IMAGEGALLERY_TEXT_DOMAIN) => 'replace',
),
)
);
// Sort Method
$field = 'sort_method';
add_settings_field(
$field, // field HTML id
__('Sort Method', TWOBC_IMAGEGALLERY_TEXT_DOMAIN), // field title
'twobc_image_gallery_create_dropdown', // display callback
'twobc_imagegallery', // page to display on
'twobc_image_gallery_options_' . $section, // section to display in
array( // additional arguments
'name' => $field,
'description' => __('Select how gallery images are sorted: by uploaded date, alphbetically by filename, or random', TWOBC_IMAGEGALLERY_TEXT_DOMAIN),
'options' => array(
__('Date uploaded', TWOBC_IMAGEGALLERY_TEXT_DOMAIN) => 'date',
__('Filename', TWOBC_IMAGEGALLERY_TEXT_DOMAIN) => 'title',
__('Random', TWOBC_IMAGEGALLERY_TEXT_DOMAIN) => 'rand',
),
)
);
// Sort Order
$field = 'sort_order';
add_settings_field(
$field, // field HTML id
__('Sort Order', TWOBC_IMAGEGALLERY_TEXT_DOMAIN), // field title
'twobc_image_gallery_create_dropdown', // display callback
'twobc_imagegallery', // page to display on
'twobc_image_gallery_options_' . $section, // section to display in
array ( // additional arguments
'name' => $field,
'description' => __('Which direction to sort, ascending (1, 2, 3) or descending (9, 8, 7)', TWOBC_IMAGEGALLERY_TEXT_DOMAIN),
'options' => array(
__('Descending', TWOBC_IMAGEGALLERY_TEXT_DOMAIN) => 'desc',
__('Ascending', TWOBC_IMAGEGALLERY_TEXT_DOMAIN) => 'asc',
),
)
);
// Paginate Galleries
$field = 'paginate_galleries';
add_settings_field(
$field, // field HTML id
__('Paginate Galleries', TWOBC_IMAGEGALLERY_TEXT_DOMAIN), // field title
'twobc_image_gallery_create_checkbox', // display callback
'twobc_imagegallery', // page to display on
'twobc_image_gallery_options_' . $section, // section to display in
array ( // additional arguments
'name' => $field,
'description' => __('Break up galleries into pages', TWOBC_IMAGEGALLERY_TEXT_DOMAIN),
)
);
// Images Per Page
$field = 'images_per_page';
add_settings_field(
$field, // field HTML id
__('Images Per Page', TWOBC_IMAGEGALLERY_TEXT_DOMAIN), // field title
'twobc_image_gallery_create_number', // display callback
'twobc_imagegallery', // page to display on
'twobc_image_gallery_options_' . $section, // section to display in
array ( // additional arguments
'name' => $field,
'description' => __('How many images to display per page, only applies if pagination is enabled', TWOBC_IMAGEGALLERY_TEXT_DOMAIN),
)
);
// Default Gallery Thumb
$field = 'default_gallery_thumb';
add_settings_field(
$field, // field HTML id
__('Gallery Thumb Source', TWOBC_IMAGEGALLERY_TEXT_DOMAIN), // field title
'twobc_image_gallery_create_dropdown', // display callback
'twobc_imagegallery', // page to display on
'twobc_image_gallery_options_' . $section, // section to display in
array ( // additional arguments
'name' => $field,
'description' => __('If the gallery does not have a custom thumbnail, decide how to generate one', TWOBC_IMAGEGALLERY_TEXT_DOMAIN),
'options' => array(
__('Last image added', TWOBC_IMAGEGALLERY_TEXT_DOMAIN) => 'last',
__('First image added', TWOBC_IMAGEGALLERY_TEXT_DOMAIN) => 'first',
__('Random', TWOBC_IMAGEGALLERY_TEXT_DOMAIN) => 'random',
),
)
);
// Hide Style
$field = 'hide_style';
add_settings_field(
$field, // field HTML id
__('Hide Default Stying', TWOBC_IMAGEGALLERY_TEXT_DOMAIN), // field title
'twobc_image_gallery_create_checkbox', // display callback
'twobc_imagegallery', // page to display on
'twobc_image_gallery_options_' . $section, // section to display in
array ( // additional arguments
'name' => $field,
'description' => __('Check to not load the default style sheet, and create the styling in the theme CSS instead', TWOBC_IMAGEGALLERY_TEXT_DOMAIN),
)
);
// Disable AJAX
$field = 'disable_ajax';
add_settings_field(
$field, // field HTML id
__('Disable AJAX', TWOBC_IMAGEGALLERY_TEXT_DOMAIN), // field title
'twobc_image_gallery_create_checkbox', // display callback
'twobc_imagegallery', // page to display on
'twobc_image_gallery_options_' . $section, // section to display in
array ( // additional arguments
'name' => $field,
'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.', TWOBC_IMAGEGALLERY_TEXT_DOMAIN),
)
);
// SECTION - DATE GALLERIES
$section = 'calendar';
add_settings_section(
'twobc_image_gallery_options_' . $section, // section HTML id
__('Calendar Based Galleries', TWOBC_IMAGEGALLERY_TEXT_DOMAIN), // section title
'twobc_image_gallery_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', TWOBC_IMAGEGALLERY_TEXT_DOMAIN), // field title
'twobc_image_gallery_create_checkbox', // display callback
'twobc_imagegallery', // page to display on
'twobc_image_gallery_options_' . $section, // section to display in
array ( // additional arguments
'name' => $field,
'description' => __('Add calendar galleries (i.e. January, 2012) to uploaded images', TWOBC_IMAGEGALLERY_TEXT_DOMAIN),
)
);
// Separate Calendar Galleries
$field = 'separate_galleries';
add_settings_field(
$field, // field HTML id
__('Separate Calendar Galleries', TWOBC_IMAGEGALLERY_TEXT_DOMAIN), // field title
'twobc_image_gallery_create_checkbox', // display callback
'twobc_imagegallery', // page to display on
'twobc_image_gallery_options_' . $section, // section to display in
array ( // additional arguments
'name' => $field,
'description' => __('Show calendar galleries in their own separate section', TWOBC_IMAGEGALLERY_TEXT_DOMAIN),
)
);
// Show Month Galleries
$field = 'show_months';
add_settings_field(
$field, // field HTML id
__('Show Month Galleries', TWOBC_IMAGEGALLERY_TEXT_DOMAIN), // field title
'twobc_image_gallery_create_checkbox', // display callback
'twobc_imagegallery', // page to display on
'twobc_image_gallery_options_' . $section, // section to display in
array ( // additional arguments
'name' => $field,
'description' => __('Display month-based galleries on the main page, only applies if Separate Calendar Galleries is checked', TWOBC_IMAGEGALLERY_TEXT_DOMAIN),
)
);
}
/**
* CSS for admin options page
*
* @return string
*/
function twobc_image_gallery_get_admin_css() {
$output = '';
return $output;
}
/**
* Javascript for admin options page
*
* @return string
*/
function twobc_image_gallery_get_admin_js() {
$output = '';
return $output;
}
/**
* 2BC Image Gallery options callback
*/
function twobc_image_gallery_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.', TWOBC_IMAGEGALLERY_TEXT_DOMAIN));
}
$image_gallery_css = twobc_image_gallery_get_admin_css();
if (!empty($image_gallery_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 '
';
$custom_div_open = false;
// if separate calendar galleries is active
if (
isset($args['separate_galleries'])
&& '1' == $args['separate_galleries']
) {
// get all year-based galleries into their own container
$years_output = array();
foreach ($term_array as $key => $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
$years_output[] = ' ' . twobc_image_gallery_get_thumb_html($term_obj, $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', TWOBC_IMAGEGALLERY_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', TWOBC_IMAGEGALLERY_TEXT_DOMAIN),
__('February', TWOBC_IMAGEGALLERY_TEXT_DOMAIN),
__('March', TWOBC_IMAGEGALLERY_TEXT_DOMAIN),
__('April', TWOBC_IMAGEGALLERY_TEXT_DOMAIN),
__('May', TWOBC_IMAGEGALLERY_TEXT_DOMAIN),
__('June', TWOBC_IMAGEGALLERY_TEXT_DOMAIN),
__('July', TWOBC_IMAGEGALLERY_TEXT_DOMAIN),
__('August', TWOBC_IMAGEGALLERY_TEXT_DOMAIN),
__('September', TWOBC_IMAGEGALLERY_TEXT_DOMAIN),
__('October', TWOBC_IMAGEGALLERY_TEXT_DOMAIN),
__('November', TWOBC_IMAGEGALLERY_TEXT_DOMAIN),
__('December', TWOBC_IMAGEGALLERY_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
$month_output[$_month_key] = twobc_image_gallery_get_thumb_html($_term_obj, $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', TWOBC_IMAGEGALLERY_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 .= '