Advertisement
designbymerovingi

CubePoints - Custom Post Type Points v.2

May 20th, 2013
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.71 KB | None | 0 0
  1. <?php
  2. /** Points for Publishing of Multiple Custom Post Type Module */
  3. cp_module_register(
  4.  
  5.     __( 'Publish Custom Post Types', 'cp' ),
  6.     'mulcustomposttypepoints',
  7.     '2.1',
  8.     'dbm',
  9.     'http://www.merovingi.com',
  10.     'http://www.merovingi.com',
  11.     __('This module awards points for publishing custom post types.', 'cp' ), 1 );
  12.  
  13. if ( cp_module_activated( 'mulcustomposttypepoints' ) ) {
  14.  
  15.     /** Module Configuration */
  16.     add_action( 'cp_config_form', 'cp_multi_customtypes_admin_setting' );
  17.     function cp_multi_customtypes_admin_setting() {
  18.  
  19.         echo '
  20.         <br />
  21.         <h3>Custom Post Types</h3>
  22.  
  23.         <table class="form-table">
  24.             <tr valign="top">
  25.                 <th scope="row"><label for="cp_customtype_used">All post types give same points?</label></th>
  26.                 <td valign="middle">
  27.                     <select id="cp_multi_option" name="cp_multi_option">' . "\n";
  28.  
  29.                 $multiple_types = get_option( 'cp_multi_type' );
  30.  
  31.                 // defaults to yes
  32.                 if ( empty( $multiple_types ) ) $multiple_types = 'yes';
  33.                 $our_option = array( 'yes', 'no' );
  34.                 foreach ( $our_option as $option ) {
  35.                     echo '<option value="' . $option . '"';
  36.                     if ( $option == $multiple_types ) echo ' selected="selected"';
  37.                     echo '>  ' . ucfirst( $option ) . '</option>' . "\n";
  38.                 }
  39.         echo '
  40.                     </select>
  41.                 </td>
  42.             </tr>';
  43.  
  44.         // If all custom post types are given the same points we only want to know one thing, how much.
  45.         if ( $multiple_types == 'yes' || empty( $multiple_types ) ) {
  46.             echo '
  47.             <tr valign="top">
  48.                 <th scope="row"><label for="cp_customtype_points">Points for Publishing a custom post type:</label></th>
  49.                 <td valign="middle"><input type="text" id="cp_multi_type_points" name="cp_multi_type_points" value="' . get_option( 'cp_multi_type_points' ) . '" size="30" /></td>
  50.             </tr>';
  51.         }
  52.         // Otherwise we list all custom post types with its own points
  53.         else {
  54.             $post_types = cp_module_get_custom_post_types( 'names' );
  55.             foreach ( $post_types  as $post_type ) {
  56.                 $post_type_points = get_option( 'cp_mctype_' . $post_type );
  57.                 $name = 'cp_mctype_' . $post_type;
  58.                 echo '
  59.             <tr valign="top">
  60.                     <th scope="row"><label for="cp_customtype_points">Publishing <code>' . $post_type . '</code>:</label></th>
  61.                 <td valign="middle"><input type="text" id="' . $name . '" name="' . $name . '" value="' . $post_type_points . '" size="30" /></td>
  62.             </tr>';
  63.             }
  64.         }
  65.         $remove_settings = get_option( 'cp_multi_remove_points' );
  66.         echo '
  67.             <tr valign="top">
  68.                 <th scope="row"><label for="cp_customtype_points">Remove Points:</label></th>
  69.                 <td valign="middle">
  70.                     <input type="checkbox" id="cp_multi_unpublish" name="cp_multi_remove_points" value="1" ';
  71.                     checked( $remove_settings, 1 ); echo ' />
  72.                     <span class="description">Remove points if a </span><code>Published</code><span class="description"> item gets changed to </span><code>Draft</code><span class="description"> or </span><code>Pending</code> or <code>Trashed</code>
  73.                 </td>
  74.             </tr>
  75.         </table>';
  76.     }
  77.  
  78.     /** Save Module Congif */
  79.     add_action( 'cp_config_process', 'cp_multi_customtypes_save' );
  80.     function cp_multi_customtypes_save() {
  81.         // If all post types get the same points
  82.         if ( $_POST['cp_multi_option'] == 'yes' ) {
  83.             update_option( 'cp_multi_type', 'yes' );
  84.             update_option( 'cp_multi_type_points', (int)$_POST['cp_multi_type_points'] );
  85.         } else {
  86.             update_option( 'cp_multi_type', 'no' );
  87.             delete_option( 'cp_multi_type_points' );
  88.         }
  89.  
  90.         update_option( 'cp_multi_remove_points', $_POST['cp_multi_remove_points'] );
  91.  
  92.         // Delete any custom points previously used.
  93.         $post_types = cp_module_get_custom_post_types();
  94.         if ( $_POST['cp_multi_option'] == 'yes' ) {
  95.             foreach ( $post_types  as $post_type ) {
  96.                 delete_option( 'cp_mctype_' . $post_type );
  97.             }
  98.         } else {
  99.             foreach ( $post_types  as $post_type ) {
  100.                 update_option( 'cp_mctype_' . $post_type, (int)$_POST['cp_mctype_' . $post_type] );
  101.             }
  102.         }
  103.     }
  104.  
  105.     // Get custom post types
  106.     function cp_module_get_custom_post_types( $type = 'names' ) {
  107.  
  108.         $args = array(
  109.             'public'   => true,
  110.             '_builtin' => false
  111.         );
  112.         $post_types = get_post_types( $args, $type, 'and' );
  113.  
  114.         return $post_types;
  115.     }
  116.  
  117.     // Check if we have already given a post points
  118.     function cp_module_already_got_points( $post ) {
  119.  
  120.         global $wpdb;
  121.        
  122.         $sql = "SELECT count(id) FROM " . CP_DB . " WHERE type = %s AND data = %d ";
  123.         $count = (int) $wpdb->get_var( $wpdb->prepare( $sql, $post->post_type, $post->ID ) );
  124.  
  125.         if ( $count > 0 )
  126.             return true;
  127.        
  128.         return false;
  129.     }
  130.  
  131.     // Check if we want to remove points on unpublishing
  132.     function cp_module_remove_points_unpublish() {
  133.         if ( get_option( 'cp_multi_remove_points' ) )
  134.             return true;
  135.         else
  136.             return false;
  137.     }
  138.  
  139.     // Hook into Transitions
  140.     add_action( 'transition_post_status', 'cp_module_publish_post_type', 10, 3 );
  141.  
  142.     // When we add points
  143.     function cp_module_publish_post_type( $new_status, $old_status, $post ) {
  144.         // Bail for built in post types
  145.         if ( $post->post_type == 'post' || $post->post_type == 'page' ) return;
  146.        
  147.         // Points rule
  148.         if ( get_option( 'cp_multi_type' ) == 'yes' )
  149.             $points = get_option( 'cp_multi_type_points' );
  150.         else
  151.             $points = get_option( 'cp_mctype_' . $post->post_type );
  152.        
  153.         // Make sure amount is set, not empty and higher then zero
  154.         if ( $points === false || empty( $points ) || $points == 0 ) return;
  155.  
  156.         // Publishing
  157.         $status = array( 'new', 'auto-draft', 'draft', 'private', 'pending' );
  158.         if ( in_array( $old_status, $status ) && $new_status == 'publish' ) {
  159.             // Make sure this is unique if we do not deduct points
  160.             if ( !cp_module_remove_points_unpublish() && cp_module_already_got_points( $post ) ) return;
  161.  
  162.             // Award Points
  163.             cp_points( $post->post_type, (int) $post->post_author, (int) $points, $post->ID );
  164.         }
  165.        
  166.         if ( !cp_module_remove_points_unpublish() ) return;
  167.         // Un-publishing
  168.         if ( in_array( $new_status, $status ) && $old_status == 'publish' ) {
  169.             // Deduct Points
  170.             cp_points( 'negative', (int) $post->post_author, (int) 0-$points, $post->ID );
  171.         }
  172.     }
  173.  
  174.     /** Adjust the Log. Overwrite custom post type entries. */
  175.     add_action( 'cp_logs_description', 'cp_admin_logs_desc_multi_customtypes', 15, 4 );
  176.     function cp_admin_logs_desc_multi_customtypes( $type, $uid, $points, $data ) {
  177.         // Grab details about the post type and the post itself.
  178.         $post = get_post( $data );
  179.         if ( $post->post_type == $type && $type != 'post' ) {
  180.             $obj = get_post_type_object( $type );
  181.             echo 'Points given for ' . $obj->labels->singular_name . ': <a href="' . get_permalink( $data ) . '">' . $post->post_title . '</a>';
  182.         }
  183.         elseif ( $type == 'negative' ) {
  184.             echo 'Removed points for: <a href="' . get_permalink( $data ) . '">' . $post->post_title . '</a> for getting drafted or pending.';
  185.         }
  186.     }
  187. }
  188. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement