Skip to content

Instantly share code, notes, and snippets.

@jazzsequence
Created July 14, 2015 19:46
Show Gist options
  • Save jazzsequence/4b2e4835006a7fd3ee22 to your computer and use it in GitHub Desktop.
Save jazzsequence/4b2e4835006a7fd3ee22 to your computer and use it in GitHub Desktop.
Simple plugin that converts all the posts in a comma-separated list of post IDs from one post type to another. In this case, it uses a post type named `roadkill-cars` and imports all `roadkill-cars` _except_ the ones defined to `post`s.
<?php
/**
* Plugin Name: Roadkill One-time Cars to Posts Conversion
* Plugin URI: http://webdevstudios.com
* Description: Converts an array of car posts to WordPress posts.
* Version: 0.1.0
* Author: WebDevStudios
* Author URI: http://webdevstudios.com
* Donate link: http://webdevstudios.com
* License: GPLv2
* Text Domain: roadkill-cars-to-posts
* Domain Path: /languages
*/
/**
* Copyright (c) 2015 WebDevStudios (email : contact@webdevstudios.com)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License, version 2 or, at
* your discretion, any later version, as published by the Free
* Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* Built using generator-plugin-wp
*/
/**
* Autoloads files with classes when needed
*
* @since 0.1.0
* @param string $class_name Name of the class being requested
* @return null
*/
function roadkill_cars_to_posts_autoload_classes( $class_name ) {
if ( 0 !== strpos( $class_name, 'RKCP_' ) ) {
return;
}
$filename = strtolower( str_ireplace(
array( 'RKCP_', '_' ),
array( '', '-' ),
$class_name
) );
RK_Cars_to_Posts::include_file( $filename );
}
spl_autoload_register( 'roadkill_cars_to_posts_autoload_classes' );
/**
* Main initiation class
*
* @since 0.1.0
* @var string $version Plugin version
* @var string $basename Plugin basename
* @var string $url Plugin URL
* @var string $path Plugin Path
*/
class RK_Cars_to_Posts {
/**
* Current version
*
* @var string
* @since 0.1.0
*/
const VERSION = '0.1.0';
/**
* URL of plugin directory
*
* @var string
* @since 0.1.0
*/
protected $url = '';
/**
* Path of plugin directory
*
* @var string
* @since 0.1.0
*/
protected $path = '';
/**
* Plugin basename
*
* @var string
* @since 0.1.0
*/
protected $basename = '';
/**
* Singleton instance of plugin
*
* @var RK_Cars_to_Posts
* @since 0.1.0
*/
protected static $single_instance = null;
/**
* Creates or returns an instance of this class.
*
* @since 0.1.0
* @return RK_Cars_to_Posts A single instance of this class.
*/
public static function get_instance() {
if ( null === self::$single_instance ) {
self::$single_instance = new self();
}
return self::$single_instance;
}
/**
* Sets up our plugin
*
* @since 0.1.0
* @return null
*/
protected function __construct() {
$this->basename = plugin_basename( __FILE__ );
$this->url = plugin_dir_url( __FILE__ );
$this->path = plugin_dir_path( __FILE__ );
$this->plugin_classes();
$this->hooks();
}
/**
* Attach other plugin classes to the base plugin class.
*
* @since 0.1.0
* @return null
*/
function plugin_classes() {
// Attach other plugin classes to the base plugin class.
// $this->admin = new RKCP_Admin( $this );
}
/**
* Add hooks and filters
*
* @since 0.1.0
* @return null
*/
public function hooks() {
register_activation_hook( __FILE__, array( $this, '_activate' ) );
register_deactivation_hook( __FILE__, array( $this, '_deactivate' ) );
add_action( 'init', array( $this, 'init' ) );
}
/**
* Activate the plugin
*
* @since 0.1.0
* @return null
*/
function _activate() {
// Make sure any rewrite functionality has been loaded
flush_rewrite_rules();
}
/**
* Deactivate the plugin
* Uninstall routines should be in uninstall.php
*
* @since 0.1.0
* @return null
*/
function _deactivate() {}
/**
* Init hooks
*
* @since 0.1.0
* @return null
*/
public function init() {
if ( true === $this->convert_cars( roadkill_cars_to_posts_ids() ) ) {
add_action( 'admin_notices', array( $this, 'these_cars_converted' ) );
} else {
add_action( 'admin_notices', array( $this, 'no_cars_converted' ) );
}
deactivate_plugins( __FILE__ );
}
/**
* Magic getter for our object.
*
* @since 0.1.0
* @param string $field
* @throws Exception Throws an exception if the field is invalid.
* @return mixed
*/
public function __get( $field ) {
switch ( $field ) {
case 'version':
return self::VERSION;
case 'basename':
case 'url':
case 'path':
return $this->$field;
default:
throw new Exception( 'Invalid '. __CLASS__ .' property: ' . $field );
}
}
/**
* Include a file from the includes directory
*
* @since 0.1.0
* @param string $filename Name of the file to be included
* @return bool Result of include call.
*/
public static function include_file( $filename ) {
$file = self::dir( 'includes/'. $filename .'.php' );
if ( file_exists( $file ) ) {
return include_once( $file );
}
return false;
}
/**
* This plugin's directory
*
* @since 0.1.0
* @param string $path (optional) appended path
* @return string Directory and path
*/
public static function dir( $path = '' ) {
static $dir;
$dir = $dir ? $dir : trailingslashit( dirname( __FILE__ ) );
return $dir . $path;
}
/**
* This plugin's url
*
* @since 0.1.0
* @param string $path (optional) appended path
* @return string URL and path
*/
public static function url( $path = '' ) {
static $url;
$url = $url ? $url : trailingslashit( plugin_dir_url( __FILE__ ) );
return $url . $path;
}
/**
* Convert an array of IDs of car posts to post posts
* @param array $ids string
* @return void
*/
public function convert_cars( $ids = '' ) {
if ( '' == $ids ) {
// IDs were not passed, deactivate the plugin and bail
return false;
}
global $wpdb;
$wpdb->query(
"
UPDATE $wpdb->posts
SET `post_type` = 'post'
WHERE `post_type` = 'roadkill-car' && `post_status` = 'publish' && `ID` not in ($ids)
"
);
return true;
}
public function no_cars_converted() {
?>
<div class="error">
<p><?php _e( 'No car posts were converted. Plugin has been deactivated.', 'roadkill-cars-to-posts' ); ?></p>
</div>
<?php
}
public function these_cars_converted() {
?>
<div class="updated">
<p><?php echo sprintf( __( 'Converted all cars but these to posts: %s.', 'roadkill-cars-to-posts' ), str_replace( ',', ', ', roadkill_cars_to_posts_ids() ) ); ?></p>
<p><?php _e( 'Plugin has been deactivated.', 'roadkill-cars-to-posts' ); ?></p>
</div>
<?php
}
}
/**
* Grab the RK_Cars_to_Posts object and return it.
* Wrapper for RK_Cars_to_Posts::get_instance()
*
* @since 0.1.0
* @return RK_Cars_to_Posts Singleton instance of plugin class.
*/
function roadkill_cars_to_posts() {
return RK_Cars_to_Posts::get_instance();
}
// Kick it off
roadkill_cars_to_posts();
/**
* Add a comma-separated list of ids to convert those car posts to post posts
* @return string List of IDs to convert
*/
function roadkill_cars_to_posts_ids() {
return '1946,1979,1999'; // comma-separated array of ids
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment