Skip to content

Instantly share code, notes, and snippets.

@mbeall
Created August 23, 2013 07:13
Show Gist options
  • Save mbeall/6316386 to your computer and use it in GitHub Desktop.
Save mbeall/6316386 to your computer and use it in GitHub Desktop.
<?php
/*
Plugin Name: Sparks Causes
Plugin URI: //Not yet developed
Description: Part of the Sparks Framework. A plugin that allows creation and management of causes, creation and management of beneficiaries, featuring online donations, progress tracking, and automatic removal of beneficiaries fully sponsored.
Version: 0.9
Author: Star Verte LLC
Author URI: http://www.starverte.com
License: GPLv2 or later
Copyright 2013 Star Verte LLC (email : info@starverte.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, 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
You should have received a copy of the GNU General Public License
along with Sparks Causes. If not, see <http://www.gnu.org/licenses/>.
*/
// Register Custom Post Type
function sparks_causes_init() {
$labels = array(
'name' => _x( 'Causes', 'Post Type General Name', 'sparks' ),
'singular_name' => _x( 'Cause', 'Post Type Singular Name', 'sparks' ),
'menu_name' => __( 'Causes', 'sparks' ),
'all_items' => __( 'All Causes', 'sparks' ),
'view_item' => __( 'View Cause', 'sparks' ),
'add_new_item' => __( 'Add New Cause', 'sparks' ),
'add_new' => __( 'Add New Cause', 'sparks' ),
'edit_item' => __( 'Edit Cause', 'sparks' ),
'update_item' => __( 'Update Cause', 'sparks' ),
'search_items' => __( 'Search causes', 'sparks' ),
'not_found' => __( 'No causes found', 'sparks' ),
'not_found_in_trash' => __( 'No causes found in Trash. Did you check recycling?', 'sparks' ),
);
$rewrite = array(
'slug' => 'cause',
'with_front' => true,
'pages' => true,
'feeds' => true,
);
$args = array(
'label' => __( 'sp_cause', 'sparks' ),
'description' => __( 'Causes used for raising money', 'sparks' ),
'labels' => $labels,
'supports' => array( 'title', 'editor', 'excerpt', 'thumbnail', 'comments', 'revisions', ),
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus' => true,
'show_in_admin_bar' => true,
'menu_position' => 5,
'menu_icon' => '',
'can_export' => true,
'has_archive' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'rewrite' => $rewrite,
'capability_type' => 'page',
);
register_post_type( 'sp_cause', $args );
$labels2 = array(
'name' => _x( 'Causes', 'Taxonomy General Name', 'sparks' ),
'singular_name' => _x( 'Cause Type', 'Taxonomy Singular Name', 'sparks' ),
'menu_name' => __( 'Causes', 'sparks' ),
'all_items' => __( 'All Cause Types', 'sparks' ),
'parent_item' => __( 'Parent Cause Type', 'sparks' ),
'parent_item_colon' => __( 'Parent Cause Type:', 'sparks' ),
'new_item_name' => __( 'New Cause Type', 'sparks' ),
'add_new_item' => __( 'Add New Cause Type', 'sparks' ),
'edit_item' => __( 'Edit Cause Type', 'sparks' ),
'update_item' => __( 'Update Cause Type', 'sparks' ),
'separate_items_with_commas' => __( 'Separate cause types with commas', 'sparks' ),
'search_items' => __( 'Search cause types', 'sparks' ),
'add_or_remove_items' => __( 'Add or remove cause types', 'sparks' ),
'choose_from_most_used' => __( 'Choose from the most used cause types', 'sparks' ),
);
$rewrite2 = array(
'slug' => 'causes',
'with_front' => true,
'hierarchical' => false,
);
$args2 = array(
'labels' => $labels2,
'hierarchical' => false,
'public' => true,
'show_ui' => false,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => false,
'rewrite' => $rewrite2,
);
register_taxonomy( 'sp_cause_type', 'sp_cause', $args2 );
wp_insert_term( 'Projects', 'sp_cause_type', array( 'slug'=>'projects', 'description' => 'A project with a fundraising goal that has been or could be met.' ) );
wp_insert_term( 'Beneficiaries', 'sp_cause_type', array( 'slug'=>'beneficiaries', 'description' => 'A person who has been, is, or could be sponsored.' ) );
}
// Hook into the 'init' action
add_action( 'init', 'sparks_causes_init', 0 );
// BEGIN - Create custom fields
add_action( 'add_meta_boxes', 'sp_cause_add_custom_boxes' );
function sp_cause_add_custom_boxes() {
add_meta_box('sp_cause_meta', 'Cause Details', 'sp_cause_meta', 'sp_cause', 'side', 'high');
}
/* Cause Details */
function sp_cause_meta() {
global $post;
$custom = get_post_custom($post->ID);
?>
<p><select name="cause_type">
<option value="NULL">Select Type</option>
<option value="Projects" <?php if (has_term('Projects', 'sp_cause_type')) { echo 'selected'; } ?>>Project</option>
<option value="Beneficiaries" <?php if (has_term('Beneficiaries', 'sp_cause_type')) { echo 'selected'; } ?>>Beneficiary</option>
</select></p>
<?php if (has_term('Projects', 'sp_cause_type')) { ?>
<p><label>Goal</label>
<input type="number" size="10" step="1" name="cause_goal" value="<?php if (isset($custom['cause_goal'])) { echo $custom["cause_goal"] [0]; } ?>" /></p>
<p><label>Raised</label>
<input type="number" size="5" step="1" name="cause_raised" value="<?php if (isset($custom['cause_raised'])) { echo $custom["cause_raised"] [0]; } ?>" /></p><?php } ?>
<?php if (has_term('Beneficiaries', 'sp_cause_type')) { ?>
<p><label>Age</label>
<input type="number" size="10" step="1" name="cause_age" value="<?php if (isset($custom['cause_age'])) { echo $custom["cause_age"] [0]; } ?>" /></p>
<p><select name="cause_gender" id="cause_gender">
<option value="">Gender</option>
<option value="male" <?php selected( $custom['cause_gender'][0], "male" ); ?>>Male</option>
<option value="female" <?php selected( $custom['cause_gender'][0], "female" ); ?>>Female</option>
</select></p><?php } ?>
<?php
}
/* Save Details */
add_action('save_post', 'save_cause_details');
function save_cause_details(){
global $post;
$custom = get_post_custom($post->ID);
if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE && (isset($post_id)) ) {
return $post_id;
}
if( defined('DOING_AJAX') && DOING_AJAX && (isset($post_id)) ) { //Prevents the metaboxes from being overwritten while quick editing.
return $post_id;
}
if( ereg('/\edit\.php', $_SERVER['REQUEST_URI']) && (isset($post_id)) ) { //Detects if the save action is coming from a quick edit/batch edit.
return $post_id;
}
// save all meta data
if (isset($_POST['cause_goal'])) {
update_post_meta($post->ID, "cause_goal", $_POST["cause_goal"]);
}
if (isset($_POST['cause_raised'])) {
update_post_meta($post->ID, "cause_raised", $_POST["cause_raised"]);
}
if (isset($_POST['cause_age'])) {
update_post_meta($post->ID, "cause_age", $_POST["cause_age"]);
}
if (isset( $_POST['cause_gender'] ) ) {
update_post_meta( $post->ID, 'cause_gender', esc_attr( $_POST['cause_gender'] ) );
}
if (isset($_POST['cause_type'])) {
update_post_meta($post->ID, "cause_type", $_POST["cause_type"]);
}
if (isset($custom['cause_type'])) {
$cause_type = $custom["cause_type"] [0];
if ($cause_type != 'NULL') {
wp_set_post_terms( $post->ID, $cause_type, 'sp_cause_type' );
}
}
}
// END - Custom Fields
function cause_goal( $before = '<div class="cause-goal">Goal: <span>$' , $after = '</span></div>' ) {
$custom = get_post_custom();
if (isset($custom['cause_goal']) && !empty($custom['cause_goal'] [0])) {
$cause_goal = $custom["cause_goal"] [0];
printf( $before . $cause_goal . $after);
}
}
function cause_raised( $args, $format = 'money', $return = '0' ) {
$custom = get_post_custom();
$defaults = array (
'before' => '<div class="cause-raised">Amount Raised to Date: <span>',
'after' => '</span></div>',
'currency' => '$',
'decimal' => 3,
'percent' => 1
);
$args = wp_parse_args($args, $defaults);
$args = (object) $args;
switch ($format) {
case 'money':
if (isset($custom['cause_raised']) && !empty($custom['cause_raised'] [0])) {
$cause_raised = $custom["cause_raised"] [0];
if ($return = '0') {
printf( $args->before . $args->currency . $cause_raised . $args->after);
}
if ($return = '1') {
return $args->before . $args->currency . $cause_raised . $args->after;
}
}
break;
case 'decimal':
if (isset($custom['cause_goal']) && !empty($custom['cause_goal'] [0]) && isset($custom['cause_raised']) && !empty($custom['cause_raised'] [0])) {
$cause_goal = $custom["cause_goal"] [0];
$cause_raised = $custom["cause_raised"] [0];
$cause_value = $cause_raised/$cause_goal;
$cause_decimal = round($cause_value, $args->decimal);
if ($return = '0') {
printf( $args->before . $cause_decimal . $args->after);
}
if ($return = '1') {
return $args->before . $cause_decimal . $args->after;
}
}
break;
case 'percent':
if (isset($custom['cause_goal']) && !empty($custom['cause_goal'] [0]) && isset($custom['cause_raised']) && !empty($custom['cause_raised'] [0])) {
$cause_goal = $custom["cause_goal"] [0];
$cause_raised = $custom["cause_raised"] [0];
$cause_value = $cause_raised/$cause_goal*100;
$cause_percent = round($cause_value, $args->percent);
if ($return = '0') {
printf( $args->before . $cause_percent . '&#37;' . $args->after);
}
if ($return = '1') {
return $args->before . $cause_percent . '&#37;' . $args->after;
}
}
break;
}
}
function cause_age( $before = '<div class="cause-age">' , $after = ' yrs old</div>' ) {
$custom = get_post_custom();
if (isset($custom['cause_age']) && !empty($custom['cause_age'] [0])) {
$cause_age = $custom["cause_age"] [0];
printf( $before . $cause_age . $after);
}
}
function cause_gender( $before = '<div class="cause-gender">' , $after = '</div>' ) {
$custom = get_post_custom();
if (isset($custom['cause_gender']) && !empty($custom['cause_gender'] [0])) {
$cause_gender = $custom["cause_gender"] [0];
if ($cause_gender != 'none') {
if ($cause_gender != 'male') {
printf( $before . 'Female' . $after);
}
else {
printf( $before . 'Male' . $after);
}
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment