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

178 lines
5.4 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/
* @copyright 2014 Robin Cornett
* @license GPL-2.0+
*/
/**
* Main plugin class.
*
* @package DisplayFeaturedImageGenesis
*/
class Display_Featured_Image_Genesis {
2014-10-21 18:16:00 -04:00
2015-01-02 20:28:33 -05:00
function __construct( $admin, $common, $cpt, $description, $output, $rss, $settings, $taxonomies ) {
$this->admin = $admin;
2014-12-30 16:27:56 -05:00
$this->common = $common;
2015-01-02 20:28:33 -05:00
$this->cpt = $cpt;
2014-12-30 16:27:56 -05:00
$this->archive = $description;
$this->output = $output;
$this->rss = $rss;
$this->settings = $settings;
$this->taxonomies = $taxonomies;
2014-09-16 14:23:40 -04:00
}
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-17 22:09:05 -04:00
add_action( 'admin_notices', array( $this, 'error_message' ) );
2014-09-16 22:24:53 -04:00
return;
}
2014-10-21 18:16:00 -04:00
add_action( 'init', array( $this, 'add_plugin_supports' ) );
2014-12-11 17:33:29 -05:00
add_action( 'admin_init', array( $this, 'check_settings' ) );
2015-01-01 17:26:51 -05:00
add_action( 'admin_init', array( $this->taxonomies, 'set_taxonomy_meta' ) );
add_action( 'admin_init', array( $this->admin, 'set_up_taxonomy_columns' ) );
add_action( 'admin_init', array( $this->admin, 'set_up_post_type_columns' ) );
2014-09-17 22:09:05 -04:00
add_action( 'plugins_loaded', array( $this, 'load_textdomain' ) );
2014-11-02 19:20:45 -05:00
add_action( 'admin_menu', array( $this->settings, 'do_submenu_page' ) );
2015-01-02 20:28:33 -05:00
add_action( 'admin_menu', array( $this->cpt, 'set_up_cpts' ) );
2014-09-30 14:19:47 -04:00
add_action( 'get_header', array( $this->output, 'manage_output' ) );
2014-12-12 10:08:00 -05:00
add_action( 'template_redirect', array( $this->rss, 'maybe_do_feed' ) );
2014-12-30 16:36:33 -05:00
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
2014-12-10 17:04:31 -05:00
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() {
if ( version_compare( PHP_VERSION, '5.3', '>=' ) ) {
deactivate_plugins( plugin_basename( dirname( __DIR__ ) ) . '/display-featured-image-genesis.php' ); // __DIR__ is a magic constant introduced in PHP 5.3
}
else {
deactivate_plugins( plugin_basename( dirname( dirname( __FILE__ ) ) ) . '/display-featured-image-genesis.php' );
}
}
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
$error = sprintf(
__( 'Sorry, Display Featured Image for Genesis works only with the Genesis Framework. It has been deactivated.', 'display-featured-image-genesis' ) );
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
}
2014-11-11 13:16:11 -05:00
echo '<div class="error"><p>' . $error . '</p></div>';
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
*/
function add_plugin_supports() {
add_image_size( 'displayfeaturedimage_backstretch', 2000, 2000, false );
$displaysetting = get_option( 'displayfeaturedimagegenesis' );
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 = get_option( 'displayfeaturedimagegenesis' );
//* return early if the option doesn't exist yet
if ( empty( $displaysetting ) ) {
return;
}
if ( empty( $displaysetting['feed_image'] ) ) {
$this->update_settings( array(
2014-12-13 10:03:55 -05:00
'feed_image' => 0
2014-12-11 17:33:29 -05:00
) );
}
//* new setting for titles added in x.y.z
if ( empty( $displaysetting['keep_titles'] ) ) {
$this->update_settings( array(
'keep_titles' => 0
) );
}
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() {
$version = Display_Featured_Image_Genesis_Common::$version;
wp_register_script( 'displayfeaturedimage-upload', plugins_url( '/includes/js/settings-upload.js', dirname( __FILE__ ) ), array( 'jquery', 'media-upload', 'thickbox' ), $version );
2015-01-02 20:28:33 -05:00
// if ( 'appearance_page_displayfeaturedimagegenesis' === get_current_screen()->id || ! empty( get_current_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(
'text' => __( 'Choose Image', 'display-featured-image-genesis' ),
) );
2015-01-02 20:28:33 -05:00
// }
2014-12-30 16:36:33 -05:00
}
2014-09-16 14:23:40 -04:00
}