Skip to content

Commit

Permalink
Add an activation hook to set default settings and remove unused filt…
Browse files Browse the repository at this point in the history
…er and function.
  • Loading branch information
thefrosty committed Jun 25, 2017
1 parent 1d1d701 commit b94ffe3
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions featured-image-column.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,27 @@ class Featured_Image_Column {
/**
* Ensures that the rest of the code only runs on edit.php pages
*
* @since 0.1
* @since 0.3
*/
public function add_hooks() {
add_action( 'admin_menu', [ $this, 'admin_menu' ] );
add_action( 'admin_init', [ $this, 'admin_init' ] );
add_action( 'admin_enqueue_scripts', [ $this, 'style' ] );
}

/**
* Activation hook, to add our default settings.
*/
public function activation_hook() {
$post_types = [];
foreach ( $this->get_post_types() as $key => $post_type ) {
if ( post_type_supports( $post_type, 'thumbnail' ) ) {
$post_types[ $post_type ] = $post_type;
}
}
update_option( 'featured_image_column', $post_types );
}

/**
* Register our settings page.
*
Expand Down Expand Up @@ -82,8 +95,6 @@ public function admin_init() {
return;
}

add_filter( 'featured_image_column_post_types', [ $this, 'add_setting_post_types' ] );

// Add out custom column and column data
foreach ( $post_types as $post_type ) {
if ( ! post_type_supports( $post_type, 'thumbnail' ) ) {
Expand Down Expand Up @@ -133,36 +144,23 @@ public function sanitize_callback( $input ) {
}

/**
* Filter our settings into our $post_type array and add our new Post Types.
*
* @since 0.2.2
*
* @param array $post_types
*
* @return array
*/
public function add_setting_post_types( array $post_types ) {
return array_merge( $post_types, array_keys( $this->get_settings() ) );
}

/**
* Enqueue our stylesheet.
* Enqueue our stylesheet on the edit.php page.
*
* @since 0.1
*
* @param string $hook
*/
public function style( $hook ) {
if ( $hook != 'edit.php' ) {
return;
}
wp_register_style(
'featured-image-column',
apply_filters( 'featured_image_column_css', plugin_dir_url( __FILE__ ) . 'css/column.css' ),
[],
self::VERSION
);
wp_enqueue_style( 'featured-image-column' );

if ( $hook === 'edit.php' ) {
wp_enqueue_style( 'featured-image-column' );
}
}

/**
Expand Down Expand Up @@ -248,6 +246,8 @@ protected function get_the_image( $post_id ) {
}

/**
* Get our settings from the DB with the defaults set to an array.
*
* @return array
*/
protected function get_settings() {
Expand All @@ -266,4 +266,5 @@ protected function get_post_types() {
}
}

( new Featured_Image_Column() )->add_hooks();
( $featured_image_column = new Featured_Image_Column() )->add_hooks();
register_activation_hook( __FILE__, [ $featured_image_column, 'activation_hook' ] );

0 comments on commit b94ffe3

Please sign in to comment.