plugin_slug; $locale = apply_filters( 'plugin_locale', get_locale(), $domain ); load_textdomain( $domain, WP_LANG_DIR . '/' . $domain . '/' . $domain . '-' . $locale . '.mo' ); load_plugin_textdomain( $domain, FALSE, dirname( plugin_basename( __FILE__ ) ) . '/lang/' ); } /** * Handle database changes for upgrades * * @since 1.2.0 */ public function archive_control_handle_updates() { include_once('db/update.php' ); } /** * Load up custom css and js used in the admin panels for this plugin * * @since 1.0.0 */ public function archive_control_custom_admin_style_scripts() { if (isset($_GET['page'])) { if ($_GET['page'] == 'archive-control' || strpos($_GET['page'], 'edit-archive-') !== false) { if ( ! did_action( 'wp_enqueue_media' ) ) { wp_enqueue_media(); } wp_register_style( 'archive_control_admin_css', plugin_dir_url( __FILE__ ) . '/css/admin-style.css', false, self::VERSION ); wp_enqueue_style( 'archive_control_admin_css' ); wp_register_script( 'archive_control_admin_js', plugin_dir_url( __FILE__ ) . '/js/admin-scripts.js', 'jquery', self::VERSION, true ); $js_translations = array( 'media_upload_title_text' => __( 'Archive Featured Image', 'archive-control' ), 'media_upload_button_text' => __( 'Set featured image', 'archive-control' ) ); wp_localize_script( 'archive_control_admin_js', 'archive_control', $js_translations ); wp_enqueue_script( 'archive_control_admin_js' ); } } } /** * Used to insert a "Settings" link on the Plugin activation screen * * @since 1.0.0 * * @param array $links The array of existing links for the plugin */ public function archive_control_action_link( $links ) { $mylinks = array( '' . __('Settings', 'archive-control') . '', ); return array_merge( $links, $mylinks ); } /** * Get only the custom post types that we want * * @since 1.2.0 */ public function archive_control_get_cpts() { $custom_post_types = array(); $args = array( 'public' => true, '_builtin' => false ); $post_types = get_post_types($args, 'objects' ); foreach ($post_types as $post_type ) { if($post_type->has_archive) { $custom_post_types[$post_type->name] = $post_type; } } return $custom_post_types; } /** * Get only the custom post types that we want * * @since 1.2.0 */ public function archive_control_get_taxes() { $custom_taxonomies = array(); $args = array( 'public' => true, '_builtin' => false ); $custom_taxonomies = get_taxonomies($args, 'objects' ); return $custom_taxonomies; } /** * Activate the settings screens for the plugin * * @since 1.0.0 */ public function archive_control_menu() { add_options_page( __('Archive Control', 'archive-control'), __('Archive Control', 'archive-control'), 'manage_options', 'archive-control', array($this,'archive_control_options') ); $custom_post_types = $this->archive_control_get_cpts(); if (!empty($custom_post_types)) { foreach($custom_post_types as $post_type) { $options = get_option('archive_control_cpt_' . $post_type->name . '_options'); if ($options) { $title_val = isset($options['title']) ? $options['title'] : null; $image_val = isset($options['image']) ? $options['image'] : null; $before_val = isset($options['before']) ? $options['before'] : null; $after_val = isset($options['after']) ? $options['after'] : null; if ($title_val == 'custom' || $image_val == 'enabled' || $before_val == 'textarea' || $after_val == 'textarea') { if ($post_type == 'post') { $parent_slug = 'edit.php'; } else { $parent_slug = 'edit.php?post_type=' . $post_type->name; } add_submenu_page( $parent_slug, __('Edit Archive Page', 'archive-control'), __('Edit Archive Page', 'archive-control'), 'edit_posts', 'edit-archive-' . $post_type->name, array($this,'archive_control_edit_page_callback') ); } //has title, image, before or after value } //has options } //for each post type } // if not empty } /** * Register the necessary settings to store for plugin use. * * @since 1.0.0 */ public function archive_control_settings() { $custom_post_types = $this->archive_control_get_cpts(); if (!empty($custom_post_types)) { foreach($custom_post_types as $post_type) { register_setting( 'archive-control-cpt-options-group', 'archive_control_cpt_' . $post_type->name . '_options' ); register_setting( 'archive-control-' . $post_type->name . '-group', 'archive_control_cpt_' . $post_type->name . '_title' ); register_setting( 'archive-control-' . $post_type->name . '-group', 'archive_control_cpt_' . $post_type->name . '_image' ); register_setting( 'archive-control-' . $post_type->name . '-group', 'archive_control_cpt_' . $post_type->name . '_before' ); register_setting( 'archive-control-' . $post_type->name . '-group', 'archive_control_cpt_' . $post_type->name . '_after' ); } } $custom_taxonomies = $this->archive_control_get_taxes(); if (!empty($custom_taxonomies)) { foreach($custom_taxonomies as $taxonomy) { register_setting( 'archive-control-tax-options-group', 'archive_control_tax_' . $taxonomy->name . '_options' ); // register_setting( 'archive-control-' . $post_type->name . '-group', 'archive_control_' . $post_type->name . '_image' ); // register_setting( 'archive-control-' . $post_type->name . '-group', 'archive_control_' . $post_type->name . '_before' ); // register_setting( 'archive-control-' . $post_type->name . '-group', 'archive_control_' . $post_type->name . '_after' ); } } } /** * Add an Edit link to the WP Admin Toolbar * * @since 1.1.1 */ public function archive_control_archive_edit_link($wp_admin_bar) { if (is_post_type_archive()) { $post_type = get_query_var('post_type', null); $options = get_option('archive_control_cpt_' . $post_type . '_options'); $edit_url = get_admin_url() . 'edit.php?post_type=' . $post_type . '&page=edit-archive-' . $post_type; $args = array( 'id' => 'edit', 'title' => __('Edit Archive Page', 'archive-control'), 'href' => $edit_url, 'meta' => array( 'class' => 'ab-item', 'title' => __('Edit Custom Post Type Archive', 'archive-control') ) ); $wp_admin_bar->add_node($args); } } /** * The variable settings screen that can be activated per custom post type * * @since 1.0.0 */ public function archive_control_edit_page_callback() { $screen = get_current_screen(); $current_post_type = $screen->post_type; $options = get_option('archive_control_cpt_' . $current_post_type . '_options'); $archive_control_cpt_title = get_option('archive_control_cpt_' . $current_post_type . '_title'); $archive_control_cpt_image = get_option('archive_control_cpt_' . $current_post_type . '_image'); $archive_control_cpt_before = get_option('archive_control_cpt_' . $current_post_type . '_before'); $archive_control_cpt_after = get_option('archive_control_cpt_' . $current_post_type . '_after'); if ($screen->post_type == '' && $screen->parent_file == 'edit.php') { $current_post_type = 'post'; } $current_post_type_object = get_post_type_object($current_post_type); $current_post_type_options = isset($options) ? $options : null; ?>

label); ?>

'archive_control_cpt_' . $current_post_type . '_before', 'textarea_rows' => 10 );?>

'archive_control_cpt_' . $current_post_type . '_after', 'textarea_rows' => 10 );?>

archive_control_get_cpts(); if (!empty($custom_post_types)) { foreach($custom_post_types as $post_type) { $edit_url = get_admin_url() . 'edit.php?post_type=' . $post_type->name . '&page=edit-archive-' . $post_type->name; $options = get_option('archive_control_cpt_' . $post_type->name . '_options'); ?>

label; ?>

/>
/>

archive_control_get_taxes(); if (!empty($custom_taxonomies)) { foreach($custom_taxonomies as $taxonomy) { $options = get_option('archive_control_tax_' . $taxonomy->name . '_options'); ?>

label; ?>

*/ ?> */ ?>
/>
/>
/>

is_main_query() ){ if (is_post_type_archive()) { $post_type = get_query_var('post_type', null); $options = get_option('archive_control_cpt_' . $post_type . '_options'); if ($options) { $order_val = isset($options['order']) ? $options['order'] : null; if ($order_val !== 'default' && $order_val !== null) { $query->set( 'order', $options['order'] ); } //has order value $orderby_val = isset($options['orderby']) ? $options['orderby'] : null; if ($orderby_val !== 'default' && $orderby_val !== null) { $query->set( 'orderby', $options['orderby'] ); if ($options['orderby'] == 'meta_value' || $options['orderby'] == 'meta_value_num') { if ($options['meta_key'] !== null) { $query->set( 'meta_key', $options['meta_key'] ); } //has meta_key value } //meta_key value is needed } //has orderby value $pagination_val = isset($options['pagination']) ? $options['pagination'] : null; if ($pagination_val !== 'default' && $pagination_val !== null) { if ($options['pagination'] == 'custom') { if ($options['posts_per_page'] !== null) { $query->set( 'posts_per_page', $options['posts_per_page'] ); } } if ($options['pagination'] == 'none') { $query->set( 'posts_per_page', -1 ); } } //has pagination value } //has options } // is_post_type_artchive if (is_tax()) { $taxonomy = $query->tax_query->queries[0]['taxonomy']; //$term = $query->tax_query->queries[0]['terms'][0]; //not needed? $options = get_option('archive_control_tax_' . $taxonomy . '_options'); if ($options) { $order_val = isset($options['order']) ? $options['order'] : null; if ($order_val !== 'default' && $order_val !== null) { $query->set( 'order', $options['order'] ); } //has order value $orderby_val = isset($options['orderby']) ? $options['orderby'] : null; if ($orderby_val !== 'default' && $orderby_val !== null) { $query->set( 'orderby', $options['orderby'] ); if ($options['orderby'] == 'meta_value' || $options['orderby'] == 'meta_value_num') { if ($options['meta_key'] !== null) { $query->set( 'meta_key', $options['meta_key'] ); } //has meta_key value } //meta_key value is needed } //has orderby value $pagination_val = isset($options['pagination']) ? $options['pagination'] : null; if ($pagination_val !== 'default' && $pagination_val !== null) { if ($options['pagination'] == 'custom') { if ($options['posts_per_page'] !== null) { $query->set( 'posts_per_page', $options['posts_per_page'] ); } } if ($options['pagination'] == 'none') { $query->set( 'posts_per_page', -1 ); } } //has pagination value } //has options } // is_tax } //is_main_query } //is not admin return; } /** * Insert some content above the WordPress loop on certain archive pages * * @since 1.1.0 * * @param object $query The existing WordPress query object */ public function archive_control_loop_start_content( $query ) { if( $query->is_main_query() ){ $this->archive_control_the_archive_top_content(true,'automatic'); } //is_main_query } /** * Insert some content above the WordPress loop on certain archive pages * * @since 1.1.0 * * @param boolean $html Whether to show the optional html markup surrounding the textarea * @param string $placement Whether the content will be placed automatically or by a theme function */ public static function archive_control_the_archive_top_content($html = true, $placement = 'function') { if (is_post_type_archive()) { $post_type = get_query_var('post_type', null); $options = get_option('archive_control_cpt_' . $post_type . '_options'); if ($options) { if ($options['before'] == 'textarea' && $options['before-placement'] == $placement) { $paged = get_query_var( 'paged', 0); if($options['before-pages'] == 'all-pages' || ($options['before-pages'] == 'first' && $paged == 0)) { Archive_Control::archive_control_archive_top_content(true, $post_type); }//handle page all/first options } //before textarea is enabled } //has options } // is_post_type_archive } /** * Insert the top content * * @since 1.1.0 * * @param boolean $html Whether to show the optional html markup surrounding the textarea * @param string $post_type A post type slug that the content belongs to */ public static function archive_control_archive_top_content($html = true, $post_type = null) { $content = Archive_Control::archive_control_get_archive_top_content($post_type); if ($content) { if ($html === true) { echo "
"; echo "
"; } echo $content; if ($html === true) { echo "
"; echo "
"; } } } /** * Return the top content * * @since 1.1.0 * * @param string $post_type A post type slug that the content belongs to */ public static function archive_control_get_archive_top_content($post_type = null) { if ($post_type === null) { if (is_post_type_archive()) { $post_type = get_query_var('post_type', null); } } $archive_control_cpt_before = get_option('archive_control_cpt_' . $post_type . '_before'); if ($archive_control_cpt_before) { return apply_filters( 'the_content', $archive_control_cpt_before ); } //if has before content } /** * Insert some content above the WordPress loop * * @since 1.1.0 * * @param object $query The existing WordPress query object */ public function archive_control_loop_start_image( $query ){ if( $query->is_main_query() ){ $this->archive_control_display_archive_top_image(); } //is_main_query } /** * Decide if the archive featured image should be automatically placed * * @since 1.1.0 */ public static function archive_control_display_archive_top_image() { if (is_post_type_archive()) { $post_type = get_query_var('post_type', null); $options = get_option('archive_control_cpt_' . $post_type . '_options'); if ($options) { if ($options['image'] == 'enabled' && $options['image-placement'] == 'automatic') { $paged = get_query_var( 'paged', 0); if($options['image-pages'] == 'all-pages' || ($options['image-pages'] == 'first' && $paged == 0)) { Archive_Control::archive_control_the_archive_thumbnail(); }//handle page all/first options } //image is enabled } //has options } // is_post_type_artchive } /** * Echo the archive thumbnail with markup * * @since 1.1.0 * * @param string $size The image size that you want displayed * @param string $post_type A post type slug that the content belongs to */ public static function archive_control_the_archive_thumbnail($size = 'large', $post_type = null) { if ($post_type === null) { if (is_post_type_archive()) { $post_type = get_query_var('post_type', null); } } $featured_img_id = Archive_Control::archive_control_get_archive_thumbnail_id($post_type); if ($featured_img_id) { echo "
"; echo wp_get_attachment_image($featured_img_id, $size); echo "
"; } } /** * Echo the archive thumbnail with html markup * * @since 1.1.0 * * @param string $size The image size that you want displayed * @param string $post_type A post type slug that the content belongs to */ public static function archive_control_get_archive_thumbnail_src($size = 'large', $post_type = null) { if ($post_type === null) { if (is_post_type_archive()) { $post_type = get_query_var('post_type', null); } } $featured_img_id = Archive_Control::archive_control_get_archive_thumbnail_id($post_type); if ($featured_img_id) { $featured_img_src = wp_get_attachment_image_src( $featured_img_id, $size ); if (is_array($featured_img_src)) { $thing = $featured_img_src[0]; return $thing; } } } /** * Get the archive thumbnail id * * @since 1.1.0 * * @param string $post_type A post type slug that the content belongs to */ public static function archive_control_get_archive_thumbnail_id($post_type = null) { if ($post_type === null) { if (is_post_type_archive()) { $post_type = get_query_var('post_type', null); } } $archive_control_cpt_image_id = get_option('archive_control_cpt_' . $post_type . '_image'); if ($archive_control_cpt_image_id) { if ( wp_attachment_is_image( $archive_control_cpt_image_id ) ) { return $archive_control_cpt_image_id; } } } /** * Insert some content below the WordPress loop on certain archive pages * * @since 1.1.0 * * @param object $query The existing WordPress query object */ public function archive_control_loop_end_content( $query ) { if( $query->is_main_query() ){ $this->archive_control_the_archive_bottom_content(true,'automatic'); } //is_main_query } /** * Insert some content below the WordPress loop on certain archive pages * * @since 1.1.0 * * @param boolean $html Whether to show the optional html markup surrounding the textarea * @param string $placement Whether the content will be placed automatically or by a theme function */ public static function archive_control_the_archive_bottom_content($html = true, $placement = 'function') { if (is_post_type_archive()) { $post_type = get_query_var('post_type', null); $options = get_option('archive_control_cpt_' . $post_type . '_options'); if ($options) { if ($options['after'] == 'textarea' && $options['after-placement'] == $placement) { $paged = get_query_var( 'paged', 0); if($options['after-pages'] == 'all-pages' || ($options['after-pages'] == 'first' && $paged == 0)) { Archive_Control::archive_control_archive_bottom_content(true, $post_type); }//handle page all/first options } //before textarea is enabled } //has options } // is_post_type_archive } /** * Insert the bottom content * * @since 1.1.0 * * @param boolean $html Whether to show the optional html markup surrounding the textarea * @param string $post_type A post type slug that the content belongs to */ public static function archive_control_archive_bottom_content($html = true, $post_type = null) { $content = Archive_Control::archive_control_get_archive_bottom_content($post_type); if ($content) { if ($html === true) { echo "
"; echo "
"; } echo $content; if ($html === true) { echo "
"; echo "
"; } } } /** * Return the bottom content * * @since 1.1.0 * * @param string $post_type A post type slug that the content belongs to */ public static function archive_control_get_archive_bottom_content($post_type = null) { if ($post_type === null) { if (is_post_type_archive()) { $post_type = get_query_var('post_type', null); } } $archive_control_cpt_after = get_option('archive_control_cpt_' . $post_type . '_after'); if ($archive_control_cpt_after) { return apply_filters( 'the_content', $archive_control_cpt_after ); } //if has after content } /** * Modify the archive title based on the user settings * * @since 1.0.0 * * @param string $title The existing archive page title */ public function archive_control_title_filter($title) { if (is_post_type_archive()) { $post_type = get_query_var('post_type', null); $options = get_option('archive_control_cpt_' . $post_type . '_options'); if ($options) { if ($options['title'] == 'custom') { $archive_control_cpt_title = get_option('archive_control_cpt_' . $post_type . '_title'); if ($archive_control_cpt_title) { $title = $archive_control_cpt_title; } //has title } //custom title setting if ($options['title'] == 'no-labels') { $title = post_type_archive_title( '', false ); } //custom title set } //has options } // is_post_type_archive if (is_tax()) { $taxonomy = get_query_var('taxonomy'); $options = get_option('archive_control_tax_' . $taxonomy . '_options'); if ($options) { if ($options['title'] == 'no-labels') { $title = single_term_title( '', false ); } } //has options } //is_tax return $title; } } if ( ! function_exists( 'the_archive_top_content' ) ) { function the_archive_top_content($html = true) { Archive_Control::archive_control_the_archive_top_content($html); } } if ( ! function_exists( 'archive_top_content' ) ) { function archive_top_content($html = true, $post_type = null) { Archive_Control::archive_control_archive_top_content($html, $post_type); } } if ( ! function_exists( 'get_archive_top_content' ) ) { function get_archive_top_content($post_type = null) { return Archive_Control::archive_control_get_archive_top_content($post_type); } } if ( ! function_exists( 'the_archive_bottom_content' ) ) { function the_archive_bottom_content($html = true) { Archive_Control::archive_control_the_archive_bottom_content($html); } } if ( ! function_exists( 'archive_bottom_content' ) ) { function archive_bottom_content($html = true, $post_type = null) { Archive_Control::archive_control_archive_bottom_content($html, $post_type); } } if ( ! function_exists( 'get_archive_bottom_content' ) ) { function get_archive_bottom_content($post_type = null) { return Archive_Control::archive_control_get_archive_bottom_content($post_type); } } if ( ! function_exists( 'get_archive_thumbnail_id' ) ) { function get_archive_thumbnail_id($post_type = null) { return Archive_Control::archive_control_get_archive_thumbnail_id($post_type); } } if ( ! function_exists( 'get_archive_thumbnail_src' ) ) { function get_archive_thumbnail_src($size = null, $post_type = null) { return Archive_Control::archive_control_get_archive_thumbnail_src($size, $post_type); } } if ( ! function_exists( 'the_archive_thumbnail' ) ) { function the_archive_thumbnail($size = null, $post_type = null) { Archive_Control::archive_control_the_archive_thumbnail($size, $post_type); } }