| %POST_THUMBNAIL|50x50% |
%TITLE%
%DATE% by %AUTHOR%
Filed as: %CATEGORIES|, %
|
');
define( 'PA_TEMPLATE_BEFORE_DEFAULT', '
| Thumb |
Title |
' );
define( 'PA_TEMPLATE_AFTER_DEFAULT', '
' );
define( 'PA_THUMB_DEFAULT', '' );
if ( ! class_exists( 'archivist' ) ) {
if ( function_exists( 'add_action' ) && function_exists( 'register_activation_hook' ) ) {
add_action( 'plugins_loaded', array( 'archivist', 'get_object' ) );
add_action( 'activate_archivist-custom-archive-templates/archivist.php', array( 'archivist', 'activation_hook' ) );
}
class archivist {
static private $classobj = NULL;
// current template settings
static $settings = NULL;
public function __construct() {
$this->load_textdomain();
add_shortcode( 'archivist', array( $this, 'shortcode' ) );
add_action( 'admin_menu', array( $this, 'add_menu_entry' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'load_scripts' ) );
// only run update hooks if the plugin is already active
$active_plugins = get_option( 'active_plugins' );
if ( in_array( 'archivist-custom-archive-templates/archivist.php', $active_plugins ) ) {
$this->keep_backwards_compatibility();
}
}
public function load_scripts() {
if (filter_input(INPUT_GET, 'page') === 'archivist_options_handle') {
wp_enqueue_script( 'archivist-ace', plugins_url('vendor/ace/ace.js', __FILE__) );
}
}
static function activation_hook() {
global $wp_version;
// Load Text-Domain
$obj = archivist::get_object();
$obj->load_textdomain();
// check wp version
if ( ! version_compare( $wp_version, '3.0', '>=' ) ) {
deactivate_plugins( __FILE__ );
wp_die( wp_sprintf( '%s: ' . __( 'Sorry, This plugin requires WordPress 3.0+', 'archivist' ), self::get_plugin_data( 'Name' ) ) );
}
// check php version
if ( ! version_compare( PHP_VERSION, '5.2.0', '>=' ) ) {
deactivate_plugins( __FILE__ ); // Deactivate ourself
wp_die( wp_sprintf( '%1s: ' . __( 'Sorry, This plugin has taken a bold step in requiring PHP 5.3.0+, Your server is currently running PHP %2s, Please bug your host to upgrade to a recent version of PHP which is less bug-prone. At last count, <strong>over 80%% of WordPress installs are using PHP 5.2+</strong>.', 'archivist' ), self::get_plugin_data( 'Name' ), PHP_VERSION ) );
}
// set default template name
add_option( 'archivist_default_template_name', 'default' );
// create default template
$obj->create_default_template();
}
static function get_default_template_name() {
$name = get_option( 'archivist_default_template_name' );
return ( strlen( $name ) > 0 ) ? $name : 'default';
}
public function create_default_template() {
$default_name = self::get_default_template_name();
$settings = $this->get_template_options();
if ( ! isset( $settings[ $default_name ] ) ) {
// TODO: refactor model archivist_settings::new
// TODO: refactor model archivist_settings::new_with_defaults
$settings[ $default_name ] = array(
'name' => $default_name,
'css' => PA_CSS_DEFAULT,
'default_thumb' => PA_THUMB_DEFAULT,
'template' => PA_TEMPLATE_DEFAULT,
'template_after' => PA_TEMPLATE_AFTER_DEFAULT,
'template_before' => PA_TEMPLATE_BEFORE_DEFAULT
);
update_option( 'archivist', $settings );
}
}
private function do_plugin_update( $old_version, $current_version ) {
// all updates before introduction of version number
if ( ! $old_version ) {
$this->update_from_zero();
}
// if ( $old_version == 20 ) ...
// if ( $old_version < 30 && $current_version == 40 ) ...
// ...
}
private function update_from_zero() {
// v1.1.0 -> v1.2.0
// move from single template to multiple templates
// if single template stuff exists, create a 'default'
// template entry based on those values.
// When finished, delete the old data
$default_name = self::get_default_template_name();
$option = get_option( 'archivist_template' );
if ( $option ) {
$settings = array();
$settings[ $default_name ] = array(
'name' => $default_name,
'css' => get_option( 'archivist_css', PA_CSS_DEFAULT ),
'template' => get_option( 'archivist_template', PA_TEMPLATE_DEFAULT ),
'default_thumb' => get_option( 'archivist_default_thumb', PA_THUMB_DEFAULT ),
'template_after' => get_option( 'archivist_template_after', PA_TEMPLATE_AFTER_DEFAULT ),
'template_before' => get_option( 'archivist_template_before', PA_TEMPLATE_BEFORE_DEFAULT )
);
update_option( 'archivist', $settings);
delete_option( 'archivist_css' );
delete_option( 'archivist_template' );
delete_option( 'archivist_default_thumb' );
delete_option( 'archivist_template_after' );
delete_option( 'archivist_template_before' );
}
// v1.2.3 -> 1.3.0
// default template name is now an option in the database
// if it's not set, it should be 'default' like in the prior versions
add_option( 'archivist_default_template_name', 'default' );
// 1.3.x revalidate all settings
$settings = $this->get_template_options();
$new_settings = array();
foreach ( $settings as $template_name => $template ) {
if ( $template_name != $template[ 'name' ] ) {
die($template_name . $template_name['name']);
continue; // skip this setting
}
// now fix missing template parts
if ( ! isset( $template[ 'css' ] ) ) {
$template[ 'css' ] = PA_CSS_DEFAULT;
}
if ( ! isset( $template[ 'template' ] ) ) {
$template[ 'template' ] = PA_TEMPLATE_DEFAULT;
}
if ( ! isset( $template[ 'default_thumb' ] ) ) {
$template[ 'default_thumb' ] = PA_THUMB_DEFAULT;
}
if ( ! isset( $template[ 'template_after' ] ) ) {
$template[ 'template_after' ] = PA_TEMPLATE_AFTER_DEFAULT;
}
if ( ! isset( $template[ 'template_before' ] ) ) {
$template[ 'template_before' ] = PA_TEMPLATE_BEFORE_DEFAULT;
}
// adopt template
$new_settings[ $template[ 'name' ] ] = $template;
}
update_option( 'archivist', $new_settings );
// check if default template still exists
$default_template = get_option( 'archivist_default_template_name' );
if ( ! isset( $new_settings[ $default_template ] ) ) {
$first_template_name = array_shift( array_keys( $new_settings ) );
update_option( 'archivist_default_template_name', $first_template_name );
}
// strip slashes in front of quotes
for ( $i = 0; $i < 5; $i ++ ) {
$new_settings = array_map( 'stripslashes_deep' , $new_settings );
}
update_option( 'archivist', $new_settings );
}
private function get_template_options() {
$settings = get_option( 'archivist' );
if ( ! is_array( $settings) ) {
$settings = array();
}
return array_map( 'stripslashes_deep', $settings );
}
private function keep_backwards_compatibility() {
if ( ! defined( 'ARCHIVIST_VERSION' ) ) {
return;
}
$current_version = (int) ARCHIVIST_VERSION;
$old_version = (int) get_option( __CLASS__ . '_version' );
// if versions are equal, there is nothing to do
if ( $current_version === $old_version ) {
return;
}
// do the updates based on old and current version
$this->do_plugin_update( $old_version, $current_version );
// update internal version
update_option( __CLASS__ . '_version', $current_version );
}
public function shortcode( $atts ) {
extract( shortcode_atts( array(
'query' => '',
'category' => '',
'tag' => '',
'template' => self::get_default_template_name()
), $atts ) );
if ( $query !== '' ) {
return $this->display_by_query( $query, $template );
}
elseif ( $category !== '' ) {
return $this->display_by_category( $category, $template );
}
else {
return $this->display_by_tag( $tag, $template );
}
}
public function add_menu_entry() {
add_submenu_page( 'options-general.php', 'Archivist', 'Archivist', 'manage_options', 'archivist_options_handle', array( $this, 'settings_page' ) );
}
function render_element( $post, $template ) {
require_once dirname( __FILE__ ) . '/parser.php';
$parser = new Archivist_Parser( $post, $template );
return $parser->render();
}
public function display_by_category( $category, $template = false ) {
$parameters = array(
'posts_per_page' => -1,
'category_name' => $category
);
$loop = new WP_Query( $parameters );
if ( ! $template ) {
$template = self::get_default_template_name();
}
return $this->display_by_loop( $loop, $template );
}
public function display_by_tag( $tag, $template = false ) {
$parameters = array(
'posts_per_page' => -1,
'tag' => $tag
);
$loop = new WP_Query( $parameters );
if ( ! $template ) {
$template = self::get_default_template_name();
}
return $this->display_by_loop( $loop, $template );
}
public function display_by_query( $query, $template = false ) {
// sometimes WordPress does stupid stuff with ampersands
$query = str_replace( "&", "&", $query );
$query = str_replace( "#038;", "&", $query );
$query = str_replace( "&&", "&", $query );
if ( ! stristr( $query, "posts_per_page" ) ) {
$query .= "&posts_per_page=-1";
}
$loop = new WP_Query( $query );
if ( ! $template ) {
$template = self::get_default_template_name();
}
return $this->display_by_loop( $loop, $template );
}
private function display_by_loop( $loop, $template = false ) {
global $post;
$all_settings = $this->get_template_options();
if ( ! $template ) {
$template = self::get_default_template_name();
}
$settings = $all_settings[ $template ];
archivist::$settings = $settings;
if ( ! $settings ) {
return '' . wp_sprintf( __( 'Archivist Error: Unknown template "%1s"', 'archivist' ), $template ) . '
';
}
ob_start();
?>
have_posts() ) : ?>
the_post(); ?>
render_element( $post, $settings[ 'template' ] ); ?>
get_current_template_name();
$settings = $this->get_template_options();
if ( get_magic_quotes_gpc() ) {
// strip slashes so HTML won't be escaped
$_POST = array_map( 'stripslashes_deep', $_POST );
$_GET = array_map( 'stripslashes_deep', $_GET );
$_REQUEST = array_map( 'stripslashes_deep', $_REQUEST );
}
// CHANGE DEFAULT action
if ( isset( $_POST[ 'change_default' ] ) && strlen( $_POST[ 'choose_template_name' ] ) > 0 ) {
update_option( 'archivist_default_template_name', $_POST[ 'choose_template_name' ] );
?>
0 ) {
unset( $settings[ $current_template ] );
update_option( 'archivist', $settings );
// if default template is deleted, make another one default
if ( $current_template == self::get_default_template_name() && count( $settings ) > 0 ) {
$first_template_name = array_shift(array_keys($settings));
update_option( 'archivist_default_template_name', $first_template_name );
}
?>
$value ) {
$template_name = $key;
// update name
if ( $value[ 'name' ] != $template_name ) {
$template_name = $value[ 'name' ];
// remove old settings enry
unset( $settings[ $key ] );
// update default_template_name setting
update_option( 'archivist_default_template_name', $template_name );
}
// update all options
$settings[ $template_name ] = $value;
}
update_option( 'archivist', $settings);
}
// CREATE action
elseif ( isset( $_POST[ 'archivist_new_template_name' ] ) ) {
if ( isset( $settings[ $_POST[ 'archivist_new_template_name' ] ] ) ) {
$success = false;
} else {
$settings[ $_POST[ 'archivist_new_template_name' ] ] = array(
'name' => $_POST[ 'archivist_new_template_name' ], // FIXME: do I have to safeify this or does WP take care?
'css' => PA_CSS_DEFAULT,
'default_thumb' => PA_THUMB_DEFAULT,
'template' => PA_TEMPLATE_DEFAULT,
'template_after' => PA_TEMPLATE_AFTER_DEFAULT,
'template_before' => PA_TEMPLATE_BEFORE_DEFAULT
);
update_option( 'archivist', $settings);
// if it is the only template setting, that means the default has been deleted
// so we make the newly created one the new default
if ( count( $settings ) === 1 ) {
update_option( 'archivist_default_template_name', $_POST[ 'archivist_new_template_name' ] );
}
$success = true;
}
if ( $success ) {
$tab = 'edit'; // display edit-template-form for this template
?>
get_template_options();;
$settings = $all_settings[ $name ];
// if the setting does not exist, take the first you can get
if ( ! $settings ) {
$name = array_shift(array_keys($all_settings));
}
return $name;
}
private function settings_page_edit() {
$name = $this->get_current_template_name();
$field_name = 'archivist[' . $name . ']';
$all_template_settings = $settings = $this->get_template_options();;
$settings = $all_template_settings[ $name ];
$default_template = get_option( 'archivist_default_template_name' );
?>