is_dfads_page() ) {
wp_register_style( 'dfads-admin-style', DFADS_PLUGIN_URL.'css/admin.css', array(), DFADS_VERSION );
wp_enqueue_style( 'dfads-admin-style' );
}
}
// Load admin specific Javascript files.
function load_js() {
if ( $this->is_dfads_page() ) {
wp_register_script( 'dfads-admin-script', DFADS_PLUGIN_URL.'js/admin.js', array( 'jquery' ), DFADS_VERSION, true );
wp_enqueue_script( 'dfads-admin-script' );
}
}
// Add link to settings page to the "Settings" menu.
public function add_plugin_page() {
global $dfads_plugin_hook;
$dfads_plugin_hook = add_options_page('Ads Settings', 'Ads', 'manage_options', 'dfads-settings', array( $this, 'create_admin_page' ) );
}
// This is the HTML of the "Settings" page.
public function create_admin_page() {
?>
Check this box to enable shortcode rendering in Text modules so you can add your ads to your widgetized areas. By default, WordPress Text widgets do not render shortcodes.
';
}
// Admin impression count checkbox option.
public function dfads_enable_count() {
$output = get_option( 'dfads-settings' );
echo '
';
}
// Loads the shortcode generator script.
public function include_generator() {
return require_once( DFADS_PLUGIN_PATH . 'inc/generator.php' );
}
// This adds custom fields to the list of ads here: edit.php?post_type=dfads
function set_custom_edit_dfads_columns($columns) {
return $columns + array(
'start_date' => __('Start Date'),
'end_date' => __('End Date'),
'impression_count' => __('Impression Count'),
'impression_limit' => __('Impression Limit'),
'ad' => __('Ad'),
);
}
// Only returns true if we're on a DFADS admin config page.
function is_dfads_page() {
$request = ( isset( $_REQUEST ) ) ? $_REQUEST : false;
if ( !$request ) {
return false;
}
if ( $request['post_type'] == 'dfads') {
return true;
}
if ( $request['page'] == 'dfads-settings' ) {
return true;
}
return false;
}
// This determines what to show in each column on the edit.php?post_type=dfads page.
function posts_custom_column( $column, $post_id ) {
switch ( $column ) {
case 'ad':
echo get_post($post_id)->post_content;
break;
case 'start_date':
$start_date = get_post_meta( $post_id , '_dfads_start_date' , true );
if ( $start_date != '') {
echo date_i18n( 'm/d/Y', $start_date );
} else {
echo 'Immediately';
}
break;
case 'end_date':
$end_date = get_post_meta( $post_id , '_dfads_end_date' , true );
if ( $end_date != '') {
echo date_i18n( 'm/d/Y', $end_date );
} else {
echo 'Never';
}
break;
case 'impression_count':
$count = get_post_meta( $post_id , '_dfads_impression_count' , true );
$count = intval( $count );
echo number_format( $count );
break;
case 'impression_limit':
$limit = get_post_meta( $post_id , '_dfads_impression_limit' , true );
if ( $limit > 0 ) {
echo number_format( $limit );
} else {
echo 'Unlimited';
}
break;
} // switch ( $column ) {
}
// This initializes sorting on the custom field columns.
function dfads_sortable_columns( $columns ) {
$columns['start_date'] = '_dfads_start_date';
$columns['end_date'] = '_dfads_end_date';
$columns['impression_count'] = '_dfads_impression_count';
$columns['impression_limit'] = '_dfads_impression_limit';
return $columns;
}
// This captures the $_REQUEST so we can modify the query for sortable columns.
function edit_dfads_load() {
add_filter( 'request', array( $this, 'dfads_sort_custom_fields' ) );
}
// Figure out what columns we are sorting on so we can update the query.
function dfads_sort_custom_fields( $vars ) {
if ( isset( $vars['post_type'] ) && 'dfads' == $vars['post_type'] ) {
// start date
if ( isset( $vars['orderby'] ) && '_dfads_start_date' == $vars['orderby'] ) {
$vars = array_merge(
$vars,
array(
'meta_key' => '_dfads_start_date',
'orderby' => 'meta_value_num'
)
);
}
// end date
if ( isset( $vars['orderby'] ) && '_dfads_end_date' == $vars['orderby'] ) {
$vars = array_merge(
$vars,
array(
'meta_key' => '_dfads_end_date',
'orderby' => 'meta_value_num'
)
);
}
// impression count
if ( isset( $vars['orderby'] ) && '_dfads_impression_count' == $vars['orderby'] ) {
$vars = array_merge(
$vars,
array(
'meta_key' => '_dfads_impression_count',
'orderby' => 'meta_value_num'
)
);
}
// impression limit
if ( isset( $vars['orderby'] ) && '_dfads_impression_limit' == $vars['orderby'] ) {
$vars = array_merge(
$vars,
array(
'meta_key' => '_dfads_impression_limit',
'orderby' => 'meta_value_num'
)
);
}
}
return $vars;
}
}
new DFADS_Admin();