Created
June 30, 2014 12:50
-
-
Save topher1kenobe/a40ae647ffbfcc64b6de to your computer and use it in GitHub Desktop.
Example of cross posting from a WordPress subsite to the parent site
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Plugin Name: PACC Cross Post | |
Plugin URI: http://thepacc.com | |
Description: Allows subsites to cross post to the main site | |
Author: Topher | |
Version: 1.0 | |
Author URI: http://codeventure.net | |
*/ | |
/** | |
* Allows subsites to cross post to the main site | |
* | |
* @package PACC_Cross_Post | |
* @since PACC_Cross_Post 1.0 | |
* @author Topher | |
*/ | |
/** | |
* Instantiate the PACC_Cross_Post instance | |
* @since PACC_Cross_Post 1.0 | |
*/ | |
add_action( 'plugins_loaded', array( 'PACC_Cross_Post', 'instance' ) ); | |
/** | |
* Main Cross Post Class | |
* | |
* Contains the main functions for the admin side of Cross Post | |
* | |
* @class PACC_Cross_Post | |
* @version 1.0.0 | |
* @since 1.0 | |
* @package PACC_Cross_Post | |
* @author Topher | |
*/ | |
class PACC_Cross_Post { | |
/** | |
* Instance handle | |
* | |
* @static | |
* @since 1.2 | |
* @var string | |
*/ | |
private static $__instance = null; | |
/** | |
* PACC_Cross_Post Constructor, actually contains nothing | |
* | |
* @access public | |
* @return void | |
*/ | |
private function __construct() {} | |
/** | |
* Instance initiator, runs setup etc. | |
* | |
* @access public | |
* @return self | |
*/ | |
public static function instance() { | |
if ( ! is_a( self::$__instance, __CLASS__ ) ) { | |
self::$__instance = new self; | |
self::$__instance->setup(); | |
} | |
return self::$__instance; | |
} | |
/** | |
* Runs things that would normally be in __construct | |
* | |
* @access private | |
* @return void | |
*/ | |
private function setup() { | |
// only do this in the admin area | |
if ( is_admin() ) { | |
add_action( 'save_post', array( $this, 'save' ) ); | |
add_action( 'add_meta_boxes', array( $this, 'menu_meta_box' ) ); | |
} | |
} | |
/** | |
* Make meta box holding select menu of Menus | |
* | |
* @access public | |
* @return void | |
*/ | |
public function menu_meta_box( $post_type ) { | |
// limit meta box to certain post types | |
$post_types = array( 'articles' ); | |
if ( in_array( $post_type, $post_types ) ) { | |
add_meta_box( | |
'wp-fetaured-menu', | |
esc_html__( 'Cross Post', 'thepacc' ), | |
array( $this, 'render_menu_meta_box_contents' ), | |
$post_type, | |
'side', | |
'high' | |
); | |
} | |
} | |
/** | |
* Render select box of WP Menus | |
* | |
* @access public | |
* @return void | |
*/ | |
public function render_menu_meta_box_contents() { | |
global $post; | |
// Add an nonce field so we can check for it later. | |
wp_nonce_field( 'thepacc', 'cross_post_nonce' ); | |
// Use get_post_meta to retrieve an existing value from the database. | |
$cross_post_value = get_post_meta( $post->ID, '_cross_post_value', true ); | |
// Display the form, using the current value. | |
// only show the checkbox if this post has NOT been pushed to the front | |
//if ( 1 != absint( $cross_post_value ) ) { | |
if ( 1 ) { | |
echo '<p>'; | |
esc_html_e( 'Would you like to request that this post appear on the main PACC site? ', 'thepacc' ); | |
echo '</p>'; | |
echo '<input type="checkbox" name="_cross_post_value" id="_cross_post_value" value="1"' . checked( $cross_post_value, 1, false ) . '/>' . "\n"; | |
echo '<label for="_cross_post_value">' . __( 'Yes', 'thepacc' ) . '</label>' . "\n"; | |
} else { | |
echo '<p>This Article has already been published to the main PACC site.</p>' . "\n"; | |
} | |
} | |
/** | |
* Updates the options table with the form data | |
* | |
* @access public | |
* @param int $post_id | |
* @return void | |
*/ | |
public function save( $post_id ) { | |
// Check if the current user is authorised to do this action. | |
$post_type = get_post_type_object( get_post( $post_id )->post_type ); | |
if ( ! current_user_can( $post_type->cap->edit_post, $post_id ) ) { | |
return; | |
} | |
// Check if our nonce is set. | |
if ( ! isset( $_POST['cross_post_nonce'] ) ) { | |
return; | |
} | |
// Verify that the nonce is valid. | |
if ( ! wp_verify_nonce( $_POST['cross_post_nonce'], 'thepacc' ) ) { | |
return; | |
} | |
// don't run on autosave | |
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { | |
return $post_id; | |
} | |
// Don't save revisions | |
if ( is_int( wp_is_post_revision( $post_id ) ) ) { | |
return $post_id; | |
} | |
// Sanitize the user input. | |
$cross_post_value = sanitize_text_field( $_POST['_cross_post_value'] ); | |
// Update the meta field. | |
update_post_meta( $post_id, '_cross_post_value', absint( $cross_post_value ) ); | |
if ( 1 == absint( $cross_post_value ) && $_POST['original_publish'] == 'Publish' ) { // This doesn't work if the site is not English, e.g. Publish might be Pubblica | |
// get the original post | |
$post = get_post( $post_id, 'ARRAY_A' ); | |
$terms = $_POST[ 'post_category' ]; | |
// get the term names in an array | |
foreach ( $terms as $key => $term_id ) { | |
$term = get_term( absint( $term_id ), 'category' ); | |
if ( $term && ! is_wp_error( $term ) ) { | |
$term_names[] = $term->name; | |
} | |
} | |
// We need to add a parent category | |
$term_names[] = 'Article'; | |
// reset some post data | |
$post['post_type'] = 'post'; | |
$post['post_status'] = 'pending'; | |
$post['ID'] = ''; | |
// get some info about the current post for feeding into meta on the cross post | |
$site_url = get_site_url(); | |
$post_url = $site_url . '/' . $_POST['post_type'] . '/' . $post['post_name'] . '/'; | |
$blog_id = get_current_blog_id(); | |
// switch to the main blog for posting | |
switch_to_blog( 1 ); | |
// get the term names in an array | |
foreach ( $term_names as $key => $term_name ) { | |
wp_create_category( wp_kses_post( $term_name ) ); | |
} | |
// remove the save action to avoid a loop | |
remove_action( 'save_post', array( $this, 'save' ) ); | |
// make the cross post | |
$new_post_id = wp_insert_post( $post ); | |
// add meta data to the cross post | |
add_post_meta( $new_post_id, 'cross_posted', absint( $cross_post_value ), true ); | |
add_post_meta( $new_post_id, 'remote_url', esc_url( $post_url ), true ); | |
add_post_meta( $new_post_id, 'source_site_id', absint( $blog_id ), true ); | |
// send the categories with the cross post | |
$set_terms = wp_set_object_terms( $new_post_id, $term_names, 'category' ); | |
// go back to the original blog | |
restore_current_blog(); | |
} | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment