post_type, 'side', 'high'); add_meta_box('views_template', __('View Template Settings', 'wpv-views'), array($this,'view_settings_meta_box'), $post->post_type, 'side', 'high'); } function view_settings_meta_box() { global $post; $output_mode = get_post_meta($post->ID, '_wpv_view_template_mode', true); if (!$output_mode) { $output_mode = 'WP_mode'; } if ($output_mode == 'raw_mode') { // Simulate a click to the HTML button to get it to show the // control buttons. ?>

»

»

»

', ''); } /** * Add admin css to the view template edit page * */ function include_admin_css() { global $pagenow; $found = false; if (($pagenow == 'edit.php' || $pagenow == 'post-new.php') && isset($_GET['post_type']) && $_GET['post_type'] == 'view-template') { $found = true; } if ($pagenow == 'post.php') { global $post; if ($post->post_type == 'view-template') { $found = true; } } if ($found) { $link_tag = ''; echo $link_tag; } } function save_post_actions($pidd, $post) { if ($post->post_type == 'view-template') { if (isset($_POST['_wpv_view_template_mode'][0])) { update_post_meta($pidd, '_wpv_view_template_mode', $_POST['_wpv_view_template_mode'][0]); wpv_view_template_update_field_values($pidd); } } // pass to the base class. parent::save_post_actions($pidd, $post); } /** * If the post has a view template * add an view template edit link to post. */ function edit_post_link($link, $post_id) { $template_selected = get_post_meta($post_id, '_views_template', true); if ($template_selected) { remove_filter('edit_post_link', array($this, 'edit_post_link'), 10, 2); ob_start(); edit_post_link(__('Edit view template', 'wpv-views'), '', '', $template_selected); $link = $link . ' ' . ob_get_clean(); add_filter('edit_post_link', array($this, 'edit_post_link'), 10, 2); } return $link; } /** * Ajax function to set the current view template to posts of a type * set in $_POST['type'] * */ function ajax_action_callback() { global $wpdb; if ( empty($_POST) || !wp_verify_nonce('set_view_template', $_POST['wpnonce']) ) { $view_template_id = $_POST['view_template_id']; $type = $_POST['type']; list($join, $cond) = $this->_get_wpml_sql($type, $_POST['lang']); $posts = $wpdb->get_col("SELECT {$wpdb->posts}.ID FROM {$wpdb->posts} {$join} WHERE post_type='{$type}' {$cond}"); $count = sizeof($posts); $updated_count = 0; if ($count > 0) { foreach($posts as $post) { $template_selected = get_post_meta($post, '_views_template', true); if ($template_selected != $view_template_id) { update_post_meta($post, '_views_template', $view_template_id); $updated_count += 1; } } } echo $updated_count; } die(); // this is required to return a proper result } function clear_legacy_view_settings() { global $wpdb; $wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE meta_key='_views_template_new_type'"); } function legacy_view_settings($options) { global $wpdb; $view_tempates_new = $wpdb->get_results("SELECT post_id, meta_value FROM {$wpdb->postmeta} WHERE meta_key='_views_template_new_type'"); foreach($view_tempates_new as $template_for_new) { $value = unserialize($template_for_new->meta_value); if ($value) { foreach($value as $type => $status) { if ($status) { $options['views_template_for_' . $type] = $template_for_new->post_id; } } } } return $options; } function admin_settings($options) { global $wpdb; $items_found = array(); $options = $this->legacy_view_settings($options); if (!isset($options['wpv-theme-function'])) { $options['wpv-theme-function'] = ''; } if (!isset($options['wpv-theme-function-debug'])) { $options['wpv-theme-function-debug'] = false; } ?>

$category) { if ($category_slug == 'nav_menu' || $category_slug == 'link_category' || $category_slug == 'post_format') { continue; } $name = $category->name; ?>
get_view_template_select_box('', $options['views_template_loop_' . $name ]); $template = str_replace('name="views_template" id="views_template"', 'name="views_template_loop_' . $name . '" id="views_template_loop_' . $name . '"', $template); echo $template; $most_popular_term = $wpdb->get_var("SELECT term_id FROM {$wpdb->term_taxonomy} WHERE taxonomy = '{$name}' AND count = (SELECT MAX(count) FROM {$wpdb->term_taxonomy} WHERE taxonomy = '{$name}')"); if ($most_popular_term) { $link = get_term_link(intval($most_popular_term), $name); ?>
true), 'objects'); ?>

name; ?>
get_view_template_select_box('', $options['views_template_for_' . $type ]); $template = str_replace('name="views_template" id="views_template"', 'name="views_template_for_' . $type . '" id="views_template_for_' . $type . '"', $template); echo $template; // add a preview button // preview the latest post of this type. list($join, $cond) = $this->_get_wpml_sql($type); $post_id = $wpdb->get_var("SELECT MAX({$wpdb->posts}.ID) FROM {$wpdb->posts} {$join} WHERE post_type='{$type}' AND post_status in ('publish') {$cond}"); if ($post_id) { $link = get_permalink($post_id); ?> _get_wpml_sql($type); $posts = $wpdb->get_col("SELECT {$wpdb->posts}.ID FROM {$wpdb->posts} {$join} WHERE post_type='{$type}' {$cond}"); $count = sizeof($posts); if ($count > 0) { $posts = "'" . implode("','", $posts) . "'"; $set_count = $wpdb->get_var("SELECT COUNT(post_id) FROM {$wpdb->postmeta} WHERE meta_key='_views_template' AND meta_value='{$options['views_template_for_' . $type ]}' AND post_id IN ({$posts})"); if ($set_count < $count) { echo '
'; echo '

'; echo sprintf(__('%d %ss use a different template:', 'wpv-views'), $count - $set_count, $type); echo ''; echo ''; echo '

'; echo ''; echo '
'; $items_found[] = $type; } else { echo '

' . sprintf(__('All %s are using this template', 'wpv-views'), $post_type->labels->name) . '

'; } } else { echo '

' . sprintf(__('There are no %s', 'wpv-views'), $post_type->labels->name) . '

'; } } else { echo '

' . sprintf(__('No template selected for %s', 'wpv-views'), $post_type->labels->name) . '

'; } ?>
get_view_template_select_box('', $options['views_template_archive_for_' . $type ]); $template = str_replace('name="views_template" id="views_template"', 'name="views_template_archive_for_' . $type . '" id="views_template_archive_for_' . $type . '"', $template); echo $template; ?>


0) { wp_nonce_field( 'set_view_template', 'set_view_template'); // we need to add some javascript ?> clear_legacy_view_settings(); foreach($_POST as $index => $value) { if (strpos($index, 'views_template_loop_') === 0) { $options[$index] = $value; } if (strpos($index, 'views_template_for_') === 0) { $options[$index] = $value; } if (strpos($index, 'views_template_archive_for_') === 0) { $options[$index] = $value; } } $options['wpv-theme-function'] = $_POST['wpv-theme-function']; $options['wpv-theme-function-debug'] = isset($_POST['wpv-theme-function-debug']) && $_POST['wpv-theme-function-debug']; return $options; } function hide_view_template_author() { global $pagenow, $post; if (($pagenow == 'post-new.php' && isset($_GET['post_type']) && $_GET['post_type'] == 'view-template') || ($pagenow == 'post.php' && isset($_GET['action']) && $_GET['action'] == 'edit')) { $post_type = $post->post_type; if($pagenow == 'post.php' && $post_type != 'view-template') { return; } ?> post_type; if($pagenow == 'post.php' && $post_type != 'view-template') { return; } $open_tags = substr_count($post->post_content, '[types'); $close_tags = substr_count($post->post_content, '[/types'); if ($close_tags < $open_tags) { echo '
'; echo sprintf(__('This template includes single-ended shortcodes. Pleae close all shortcodes to avoid processing errors. %sRead more%s', 'wpv-views'), '', ' »'); echo '
'; } } } } /** * Update custom fields array for view template on save * @param unknown_type $pidd post ID * @param unknown_type $post post reference */ function wpv_view_template_update_field_values($pidd, $post = null) { if($post == null) { $post = get_post($pidd); } $content = $post->post_content; $shortcode_expression = "/\\[(wpv-|types).*?\\]/i"; // search for shortcodes $counts = preg_match_all($shortcode_expression, $content, $matches); // iterate 0-level shortcode elements if($counts > 0) { $_wpv_view_template_fields = serialize($matches[0]); update_post_meta($pidd, '_wpv_view_template_fields', $_wpv_view_template_fields); } }