Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /** Points for Publishing of Multiple Custom Post Type Module */
- cp_module_register(
- __( 'Publish Custom Post Types', 'cp' ),
- 'mulcustomposttypepoints',
- '2.1',
- 'dbm',
- 'http://www.merovingi.com',
- 'http://www.merovingi.com',
- __('This module awards points for publishing custom post types.', 'cp' ), 1 );
- if ( cp_module_activated( 'mulcustomposttypepoints' ) ) {
- /** Module Configuration */
- add_action( 'cp_config_form', 'cp_multi_customtypes_admin_setting' );
- function cp_multi_customtypes_admin_setting() {
- echo '
- <br />
- <h3>Custom Post Types</h3>
- <table class="form-table">
- <tr valign="top">
- <th scope="row"><label for="cp_customtype_used">All post types give same points?</label></th>
- <td valign="middle">
- <select id="cp_multi_option" name="cp_multi_option">' . "\n";
- $multiple_types = get_option( 'cp_multi_type' );
- // defaults to yes
- if ( empty( $multiple_types ) ) $multiple_types = 'yes';
- $our_option = array( 'yes', 'no' );
- foreach ( $our_option as $option ) {
- echo '<option value="' . $option . '"';
- if ( $option == $multiple_types ) echo ' selected="selected"';
- echo '> ' . ucfirst( $option ) . '</option>' . "\n";
- }
- echo '
- </select>
- </td>
- </tr>';
- // If all custom post types are given the same points we only want to know one thing, how much.
- if ( $multiple_types == 'yes' || empty( $multiple_types ) ) {
- echo '
- <tr valign="top">
- <th scope="row"><label for="cp_customtype_points">Points for Publishing a custom post type:</label></th>
- <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>
- </tr>';
- }
- // Otherwise we list all custom post types with its own points
- else {
- $post_types = cp_module_get_custom_post_types( 'names' );
- foreach ( $post_types as $post_type ) {
- $post_type_points = get_option( 'cp_mctype_' . $post_type );
- $name = 'cp_mctype_' . $post_type;
- echo '
- <tr valign="top">
- <th scope="row"><label for="cp_customtype_points">Publishing <code>' . $post_type . '</code>:</label></th>
- <td valign="middle"><input type="text" id="' . $name . '" name="' . $name . '" value="' . $post_type_points . '" size="30" /></td>
- </tr>';
- }
- }
- $remove_settings = get_option( 'cp_multi_remove_points' );
- echo '
- <tr valign="top">
- <th scope="row"><label for="cp_customtype_points">Remove Points:</label></th>
- <td valign="middle">
- <input type="checkbox" id="cp_multi_unpublish" name="cp_multi_remove_points" value="1" ';
- checked( $remove_settings, 1 ); echo ' />
- <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>
- </td>
- </tr>
- </table>';
- }
- /** Save Module Congif */
- add_action( 'cp_config_process', 'cp_multi_customtypes_save' );
- function cp_multi_customtypes_save() {
- // If all post types get the same points
- if ( $_POST['cp_multi_option'] == 'yes' ) {
- update_option( 'cp_multi_type', 'yes' );
- update_option( 'cp_multi_type_points', (int)$_POST['cp_multi_type_points'] );
- } else {
- update_option( 'cp_multi_type', 'no' );
- delete_option( 'cp_multi_type_points' );
- }
- update_option( 'cp_multi_remove_points', $_POST['cp_multi_remove_points'] );
- // Delete any custom points previously used.
- $post_types = cp_module_get_custom_post_types();
- if ( $_POST['cp_multi_option'] == 'yes' ) {
- foreach ( $post_types as $post_type ) {
- delete_option( 'cp_mctype_' . $post_type );
- }
- } else {
- foreach ( $post_types as $post_type ) {
- update_option( 'cp_mctype_' . $post_type, (int)$_POST['cp_mctype_' . $post_type] );
- }
- }
- }
- // Get custom post types
- function cp_module_get_custom_post_types( $type = 'names' ) {
- $args = array(
- 'public' => true,
- '_builtin' => false
- );
- $post_types = get_post_types( $args, $type, 'and' );
- return $post_types;
- }
- // Check if we have already given a post points
- function cp_module_already_got_points( $post ) {
- global $wpdb;
- $sql = "SELECT count(id) FROM " . CP_DB . " WHERE type = %s AND data = %d ";
- $count = (int) $wpdb->get_var( $wpdb->prepare( $sql, $post->post_type, $post->ID ) );
- if ( $count > 0 )
- return true;
- return false;
- }
- // Check if we want to remove points on unpublishing
- function cp_module_remove_points_unpublish() {
- if ( get_option( 'cp_multi_remove_points' ) )
- return true;
- else
- return false;
- }
- // Hook into Transitions
- add_action( 'transition_post_status', 'cp_module_publish_post_type', 10, 3 );
- // When we add points
- function cp_module_publish_post_type( $new_status, $old_status, $post ) {
- // Bail for built in post types
- if ( $post->post_type == 'post' || $post->post_type == 'page' ) return;
- // Points rule
- if ( get_option( 'cp_multi_type' ) == 'yes' )
- $points = get_option( 'cp_multi_type_points' );
- else
- $points = get_option( 'cp_mctype_' . $post->post_type );
- // Make sure amount is set, not empty and higher then zero
- if ( $points === false || empty( $points ) || $points == 0 ) return;
- // Publishing
- $status = array( 'new', 'auto-draft', 'draft', 'private', 'pending' );
- if ( in_array( $old_status, $status ) && $new_status == 'publish' ) {
- // Make sure this is unique if we do not deduct points
- if ( !cp_module_remove_points_unpublish() && cp_module_already_got_points( $post ) ) return;
- // Award Points
- cp_points( $post->post_type, (int) $post->post_author, (int) $points, $post->ID );
- }
- if ( !cp_module_remove_points_unpublish() ) return;
- // Un-publishing
- if ( in_array( $new_status, $status ) && $old_status == 'publish' ) {
- // Deduct Points
- cp_points( 'negative', (int) $post->post_author, (int) 0-$points, $post->ID );
- }
- }
- /** Adjust the Log. Overwrite custom post type entries. */
- add_action( 'cp_logs_description', 'cp_admin_logs_desc_multi_customtypes', 15, 4 );
- function cp_admin_logs_desc_multi_customtypes( $type, $uid, $points, $data ) {
- // Grab details about the post type and the post itself.
- $post = get_post( $data );
- if ( $post->post_type == $type && $type != 'post' ) {
- $obj = get_post_type_object( $type );
- echo 'Points given for ' . $obj->labels->singular_name . ': <a href="' . get_permalink( $data ) . '">' . $post->post_title . '</a>';
- }
- elseif ( $type == 'negative' ) {
- echo 'Removed points for: <a href="' . get_permalink( $data ) . '">' . $post->post_title . '</a> for getting drafted or pending.';
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement