Files
display-featured-image-genesis/includes/class-displayfeaturedimagegenesis.php
T

343 lines
9.8 KiB
PHP
Raw Normal View History

2014-09-16 14:23:40 -04:00
<?php
/**
2014-09-16 22:24:53 -04:00
* Display Featured Image for Genesis
2014-09-16 14:23:40 -04:00
*
* @package DisplayFeaturedImageGenesis
* @author Robin Cornett <hello@robincornett.com>
* @link https://github.com/robincornett/display-featured-image-genesis/
2016-04-01 20:18:20 -04:00
* @copyright 2014-2016 Robin Cornett
2014-09-16 14:23:40 -04:00
* @license GPL-2.0+
*/
/**
* Main plugin class.
*
* @package DisplayFeaturedImageGenesis
*/
class Display_Featured_Image_Genesis {
2014-10-21 18:16:00 -04:00
2016-01-12 09:13:25 -05:00
/**
* Admin area class: handles columns.
* @var Display_Featured_Image_Genesis_Admin $admin
*/
protected $admin;
/**
* Adds new author meta.
* @var Display_Featured_Image_Genesis_Author $author
*/
protected $author;
/**
* Common class: sets image ID, post title, handles database query
* @var Display_Featured_Image_Genesis_Common $common
*/
protected $common;
2016-07-02 09:17:46 -04:00
/**
* @var $customizer Display_Featured_Image_Genesis_Customizer
*/
protected $customizer;
2016-01-12 09:13:25 -05:00
/**
* All archive description functions.
* @var Display_Featured_Image_Genesis_Description $description
*/
protected $description;
2016-07-02 10:17:38 -04:00
/**
* Manages help tabs for settings page.
* @var $helptabs Display_Featured_Image_Genesis_HelpTabs
*/
protected $helptabs;
2016-01-12 09:13:25 -05:00
/**
* Handles all image output functionality
* @var Display_Featured_Image_Genesis_Output $output
*/
protected $output;
/**
* Updates metabox on post edit page
2016-04-04 15:50:47 -04:00
* @var Display_Featured_Image_Genesis_Post_Meta $post_meta
*/
2016-04-04 15:50:47 -04:00
protected $post_meta;
2016-01-12 09:13:25 -05:00
/**
* Handles RSS feed output
* @var Display_Featured_Image_Genesis_RSS $rss
*/
protected $rss;
/**
* Sets up settings page for the plugin.
* @var Display_Featured_Image_Genesis_Settings $settings
*/
protected $settings;
/**
* Handles term meta.
* @var Display_Featured_Image_Genesis_Taxonomies $taxonomies
*/
protected $taxonomies;
/**
* Display_Featured_Image_Genesis constructor.
*
* @param $admin
* @param $author
* @param $common
2016-07-02 09:17:46 -04:00
* @param $customizer
2016-01-12 09:13:25 -05:00
* @param $description
* @param $output
* @param $rss
* @param $settings
* @param $taxonomies
*/
2016-07-02 10:17:38 -04:00
function __construct( $admin, $author, $common, $customizer, $description, $helptabs, $output, $post_meta, $rss, $settings, $taxonomies ) {
2015-06-03 14:36:16 -04:00
$this->admin = $admin;
2015-06-07 12:04:36 -04:00
$this->author = $author;
2015-06-03 14:36:16 -04:00
$this->common = $common;
2016-07-02 09:17:46 -04:00
$this->customizer = $customizer;
2015-06-03 14:36:16 -04:00
$this->description = $description;
2016-07-02 10:17:38 -04:00
$this->helptabs = $helptabs;
2015-06-03 14:36:16 -04:00
$this->output = $output;
2016-04-04 15:50:47 -04:00
$this->post_meta = $post_meta;
2015-06-03 14:36:16 -04:00
$this->rss = $rss;
$this->settings = $settings;
$this->taxonomies = $taxonomies;
2014-09-16 14:23:40 -04:00
}
2016-01-12 09:13:25 -05:00
/**
* Main plugin function. Starts up all the things.
*/
2014-09-17 22:09:05 -04:00
public function run() {
2014-11-01 20:40:42 -04:00
if ( 'genesis' !== basename( get_template_directory() ) ) {
2014-09-26 15:43:53 -04:00
add_action( 'admin_init', array( $this, 'deactivate' ) );
2014-09-16 22:24:53 -04:00
return;
}
2015-02-16 13:23:45 -05:00
require plugin_dir_path( __FILE__ ) . 'helper-functions.php';
2014-10-21 18:16:00 -04:00
2015-07-26 16:21:14 -04:00
add_action( 'after_setup_theme', array( $this, 'add_plugin_supports' ) );
2014-12-11 17:33:29 -05:00
add_action( 'admin_init', array( $this, 'check_settings' ) );
2015-01-09 12:54:12 -05:00
add_action( 'widgets_init', array( $this, 'register_widgets' ) );
2015-01-03 15:41:49 -05:00
add_action( 'admin_init', array( $this->admin, 'set_up_columns' ) );
2014-09-17 22:09:05 -04:00
add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) );
2014-12-30 16:36:33 -05:00
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
2015-07-12 12:26:20 -04:00
add_filter( 'plugin_action_links_' . DISPLAYFEATUREDIMAGEGENESIS_BASENAME, array( $this, 'add_settings_link' ) );
2016-07-02 10:25:59 -04:00
// Taxonomies, Author, Post Meta
add_action( 'admin_init', array( $this->taxonomies, 'set_taxonomy_meta' ) );
add_action( 'admin_init', array( $this->author, 'set_author_meta' ) );
2016-06-21 10:51:58 -04:00
add_filter( 'admin_post_thumbnail_html', array( $this->post_meta, 'meta_box' ), 10, 2 );
2016-04-04 15:50:47 -04:00
add_action( 'save_post', array( $this->post_meta, 'save_meta' ) );
2014-12-10 17:04:31 -05:00
2016-07-02 10:25:59 -04:00
// Settings
add_action( 'admin_menu', array( $this->settings, 'do_submenu_page' ) );
add_filter( 'displayfeaturedimagegenesis_get_setting', array( $this->settings, 'get_display_setting' ) );
add_action( 'load-appearance_page_displayfeaturedimagegenesis', array( $this->helptabs, 'help' ) );
// Customizer
2016-07-02 09:17:46 -04:00
add_action( 'customize_register', array( $this->customizer, 'customizer' ) );
2016-07-02 10:25:59 -04:00
// Front End Output
add_action( 'get_header', array( $this->output, 'manage_output' ) );
add_filter( 'genesis_get_image_default_args', array( $this->output, 'change_thumbnail_fallback' ) );
// RSS
add_action( 'template_redirect', array( $this->rss, 'maybe_do_feed' ) );
2014-09-16 14:23:40 -04:00
}
2014-09-26 15:43:53 -04:00
/**
* deactivates the plugin if Genesis isn't running
*
* @since 1.1.2
*
*/
public function deactivate() {
2015-07-12 12:26:20 -04:00
deactivate_plugins( DISPLAYFEATUREDIMAGEGENESIS_BASENAME );
add_action( 'admin_notices', array( $this, 'error_message' ) );
2014-09-26 15:43:53 -04:00
}
2014-09-16 14:23:40 -04:00
/**
2014-09-17 22:09:05 -04:00
* Error message if we're not using the Genesis Framework.
2014-09-16 14:23:40 -04:00
*
2014-09-17 22:09:05 -04:00
* @since 1.1.0
2014-09-16 14:23:40 -04:00
*/
2014-09-17 22:09:05 -04:00
public function error_message() {
2014-11-11 13:16:11 -05:00
2015-04-24 17:02:47 -04:00
$error = sprintf( __( 'Sorry, Display Featured Image for Genesis works only with the Genesis Framework. It has been deactivated.', 'display-featured-image-genesis' ) );
2014-11-11 13:16:11 -05:00
if ( version_compare( PHP_VERSION, '5.3', '<' ) ) {
$error = $error . sprintf(
__( ' But since we\'re talking anyway, did you know that your server is running PHP version %1$s, which is outdated? You should ask your host to update that for you.', 'display-featured-image-genesis' ),
2014-09-26 15:43:53 -04:00
PHP_VERSION
2014-11-11 13:16:11 -05:00
);
2014-09-26 15:43:53 -04:00
}
2015-04-24 17:02:47 -04:00
echo '<div class="error"><p>' . esc_attr( $error ) . '</p></div>';
2014-11-11 13:16:11 -05:00
2014-09-26 15:43:53 -04:00
if ( isset( $_GET['activate'] ) ) {
unset( $_GET['activate'] );
}
2014-11-11 13:16:11 -05:00
2014-09-16 14:23:40 -04:00
}
/**
* add plugin support for new image size and excerpts on pages, if move excerpts option is enabled
*
* @since 1.3.0
*/
2015-07-26 16:21:14 -04:00
public function add_plugin_supports() {
$args = apply_filters( 'displayfeaturedimagegenesis_custom_image_size', array(
'width' => 2000,
'height' => 2000,
'crop' => false,
) );
add_image_size( 'displayfeaturedimage_backstretch', (int) $args['width'], (int) $args['height'], (bool) $args['crop'] );
$displaysetting = displayfeaturedimagegenesis_get_setting();
if ( $displaysetting['move_excerpts'] ) {
add_post_type_support( 'page', 'excerpt' );
}
}
2014-12-11 17:33:29 -05:00
/**
* check existing settings array to see if a setting is in the array
* @return updated setting updates to default (0)
2014-12-13 10:03:55 -05:00
* @since 1.5.0
2014-12-11 17:33:29 -05:00
*/
public function check_settings() {
$displaysetting = displayfeaturedimagegenesis_get_setting();
2014-12-11 17:33:29 -05:00
2015-05-30 10:27:33 -04:00
// return early if the option doesn't exist yet
2014-12-11 17:33:29 -05:00
if ( empty( $displaysetting ) ) {
return;
}
if ( empty( $displaysetting['feed_image'] ) ) {
$this->update_settings( array(
2015-05-30 10:27:33 -04:00
'feed_image' => 0,
2014-12-11 17:33:29 -05:00
) );
}
2015-04-09 22:16:36 -04:00
// new setting for titles added in 2.0.0
if ( empty( $displaysetting['keep_titles'] ) ) {
$this->update_settings( array(
2015-05-30 10:27:33 -04:00
'keep_titles' => 0,
) );
}
2015-04-09 22:16:36 -04:00
// new setting for subsequent pages added in 2.2.0
if ( empty( $displaysetting['is_paged'] ) ) {
$this->update_settings( array(
2015-05-30 10:27:33 -04:00
'is_paged' => 0,
2015-04-09 22:16:36 -04:00
) );
}
2014-12-11 17:33:29 -05:00
}
/**
* Takes an array of new settings, merges them with the old settings, and pushes them into the database.
*
2014-12-13 10:03:55 -05:00
* @since 1.5.0
2014-12-11 17:33:29 -05:00
*
* @param string|array $new New settings. Can be a string, or an array.
* @param string $setting Optional. Settings field name. Default is displayfeaturedimagegenesis.
*/
protected function update_settings( $new = '', $setting = 'displayfeaturedimagegenesis' ) {
return update_option( $setting, wp_parse_args( $new, get_option( $setting ) ) );
}
2014-09-16 14:23:40 -04:00
/**
2014-09-17 22:09:05 -04:00
* Set up text domain for translations
2014-09-16 14:23:40 -04:00
*
2014-09-17 22:09:05 -04:00
* @since 1.1.0
2014-09-16 14:23:40 -04:00
*/
2014-09-17 22:09:05 -04:00
public function load_textdomain() {
load_plugin_textdomain( 'display-featured-image-genesis', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
2014-09-16 14:23:40 -04:00
}
2014-12-30 16:36:33 -05:00
/**
* enqueue admin scripts
* @return scripts to use image uploader
*
* @since 1.2.1
*/
public function enqueue_scripts() {
2015-01-03 15:42:20 -05:00
2015-06-03 14:36:16 -04:00
$version = $this->common->version;
2014-12-30 16:36:33 -05:00
wp_register_script( 'displayfeaturedimage-upload', plugins_url( '/includes/js/settings-upload.js', dirname( __FILE__ ) ), array( 'jquery', 'media-upload', 'thickbox' ), $version );
2015-01-05 23:26:44 -05:00
wp_register_script( 'widget_selector', plugins_url( '/includes/js/widget-selector.js', dirname( __FILE__ ) ), array( 'jquery' ), $version );
2014-12-30 16:36:33 -05:00
2015-11-17 14:03:21 -05:00
$screen = get_current_screen();
$screen_ids = array(
'appearance_page_displayfeaturedimagegenesis',
'profile',
2015-11-17 16:36:15 -05:00
'user-edit',
2015-11-17 14:03:21 -05:00
);
2015-11-17 16:36:15 -05:00
if ( in_array( $screen->id, $screen_ids, true ) || ! empty( $screen->taxonomy ) ) {
2014-12-30 16:36:33 -05:00
wp_enqueue_media();
wp_enqueue_script( 'displayfeaturedimage-upload' );
wp_localize_script( 'displayfeaturedimage-upload', 'objectL10n', array(
2015-01-03 15:42:20 -05:00
'text' => __( 'Select Image', 'display-featured-image-genesis' ),
2014-12-30 16:36:33 -05:00
) );
2015-01-03 15:42:20 -05:00
}
2014-12-30 16:36:33 -05:00
2015-08-17 11:42:12 -04:00
if ( function_exists( 'is_customize_preview' ) && is_customize_preview() && ! function_exists( 'genesis' ) ) {
2015-07-12 11:37:54 -04:00
return;
}
2015-11-17 16:36:15 -05:00
if ( in_array( $screen->id, array( 'widgets', 'customize' ), true ) ) {
2015-01-05 23:26:44 -05:00
wp_enqueue_script( 'widget_selector' );
wp_localize_script( 'widget_selector', 'displayfeaturedimagegenesis_ajax_object', array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
2015-01-05 20:32:18 -05:00
}
2014-12-30 16:36:33 -05:00
}
2015-07-12 11:37:54 -04:00
/**
* Register widgets for plugin
* @return widgets Taxonomy/term, CPT, and Author widgets
*
* @since 2.0.0
*/
public function register_widgets() {
2015-01-09 12:54:12 -05:00
2015-06-03 14:36:16 -04:00
$files = array(
2015-06-07 17:03:09 -04:00
'author',
2015-06-03 14:36:16 -04:00
'cpt-archive',
'taxonomy',
);
foreach ( $files as $file ) {
2015-07-27 13:00:11 -04:00
require_once plugin_dir_path( __FILE__ ) . 'widgets/displayfeaturedimagegenesis-' . $file . '-widget.php';
2015-06-03 14:36:16 -04:00
}
if ( function_exists( 'is_customize_preview' ) && is_customize_preview() && ! function_exists( 'genesis' ) ) {
return;
}
2015-06-07 17:03:09 -04:00
register_widget( 'Display_Featured_Image_Genesis_Author_Widget' );
2015-02-03 08:44:23 -05:00
register_widget( 'Display_Featured_Image_Genesis_Widget_Taxonomy' );
register_widget( 'Display_Featured_Image_Genesis_Widget_CPT' );
2015-01-05 17:54:56 -05:00
}
2015-07-12 12:28:21 -04:00
/**
* Add link to plugin settings page in plugin table
* @param $links link to settings page
*
* @since 2.3.0
*/
public function add_settings_link( $links ) {
$links[] = sprintf( '<a href="%s">%s</a>', esc_url( admin_url( 'themes.php?page=displayfeaturedimagegenesis' ) ), esc_attr__( 'Settings', 'display-featured-image-genesis' ) );
return $links;
}
2014-09-16 14:23:40 -04:00
}