Last active
August 29, 2015 13:55
-
-
Save stephanieleary/8718584 to your computer and use it in GitHub Desktop.
Achievements 3.5 plugin for LetterMo
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: LetterMo Post Types | |
Plugin URI: http://stephanieleary.com/ | |
Version: 1.6 | |
Author: Stephanie Leary | |
Author URI: http://stephanieleary.com/ | |
Description: Setting up the post types and achievements for Month of Letters. | |
License: GPL2 | |
*/ | |
// CPT defined elsewhere is 'letter'; its taxonomy is called 'extras' | |
// Exit if accessed directly | |
if ( ! defined( 'ABSPATH' ) ) exit; | |
add_action('gform_post_submission', 'lettermo_gform_category_achievements', 99, 2); | |
function lettermo_gform_category_achievements( $entry, $form ) { | |
// Check if the submission contains a WordPress post | |
if ( isset ( $entry['post_id'] ) ) { | |
achievements()->extensions->event_name( 'new_to_publish', $entry ); | |
} | |
} | |
function init_lettermo_extension() { | |
achievements()->extensions->lettermo = new Lettermo_Extension; | |
// Tell the world that the LetterMo extension is ready | |
do_action( 'init_lettermo_extension' ); | |
} | |
add_action( 'dpa_ready', 'init_lettermo_extension' ); | |
class Lettermo_Extension extends DPA_CPT_Extension { | |
public function __construct() { | |
$this->actions = array( | |
// Letters | |
'lettermo_new_letter' => __( 'The user sends a letter', 'dpa' ), | |
'lettermo_new_package' => __( 'The user sends a package', 'dpa' ), | |
'lettermo_new_intl' => __( 'The user sends international mail', 'dpa' ), | |
); | |
$this->contributors = array( | |
array( | |
'name' => 'Stephanie Leary', | |
'gravatar_url' => 'http://www.gravatar.com/avatar/4a31c00a18e6b155a4d27c487ef06ce1', | |
'profile_url' => 'http://profiles.wordpress.org/sillybean/', | |
), | |
); | |
$this->generic_cpt_actions = array( | |
'draft_to_publish', | |
'future_to_publish', | |
'new_to_publish', | |
'pending_to_publish', | |
'private_to_publish', | |
); | |
$this->description = __( 'LetterMo is the custom post types and achievements for the Month of Letters site.', 'dpa' ); | |
$this->id = 'lettermo'; | |
$this->image_url = trailingslashit( achievements()->includes_url ) . 'admin/images/lettermo.png'; | |
$this->name = __( 'LetterMo', 'dpa' ); | |
$this->rss_url = 'http://lettermo.org/feed/'; | |
$this->small_image_url = trailingslashit( achievements()->includes_url ) . 'admin/images/lettermo-small.png'; | |
$this->version = 1; | |
$this->wporg_url = 'http://lettermo.org/'; | |
add_filter( 'dpa_filter_events', array( $this, 'get_generic_cpt_actions' ), 1, 1 ); | |
add_filter( 'dpa_handle_event_name', array( $this, 'event_name' ), 10, 2 ); | |
add_filter( 'dpa_handle_event_user_id', array( $this, 'event_user_id' ), 10, 3 ); | |
} | |
function event_name( $event_name, $func_args ) { | |
// Check we're dealing with the right type of event | |
if ( ! in_array( $event_name, $this->generic_cpt_actions ) ) | |
return $event_name; | |
// Check we're dealing with the right post type | |
if ( 'letter' !== $func_args[0]->post_type ) | |
return $event_name; | |
$postid = $func_args[0]->ID; | |
// Switch the event names for Letters | |
if ( has_term( 'package', 'extras', $postid ) ) | |
return 'lettermo_new_package'; | |
if ( has_term( 'international', 'extras', $postid ) ) | |
return 'lettermo_new_intl'; | |
return 'lettermo_new_letter'; | |
} | |
public function event_user_id( $user_id, $action_name, $action_func_args ) { | |
// Only deal with events added by this extension. | |
if ( ! in_array( $action_name, array( 'letter_draft_to_publish', 'letter_draft_to_publish', ) ) ) | |
return $user_id; | |
// New Letter, get the post author | |
if ( in_array( $action_func_args[0]->post_type, array( 'letter', ) ) ) | |
return $this->get_post_author( $user_id, $action_name, $action_func_args ); | |
else | |
return $user_id; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment