Skip to content

api : pp_save-status_ppp

Home Forums Feature Requests api : pp_save-status_ppp

Tagged: ,

This topic contains 1 reply, has 2 voices, and was last updated by 4 months, 3 weeks ago.

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #24843 Reply

    As I needed to programmatically create and edit custom statuses, I derived a function which I called ppp_save_statusfrom PPP_StatusSave::save( $status, $new ). It works but of course I’m not sure it will continue to work with future versions. Hereunder is the code.

    <?php
    /*************************************************
    * Adapté de PPP_StatusSave::save($status, $new) *
    * comme API de Press Permit Custom Statuses *
    *************************************************/
    function ppp_save_status( $ltc_args ) {
    $new = $ltc_args['new_status'];
    $status = $ltc_args['status_slug'];
    $arr_return = array( 'retval' => false, 'redirect' => '' );

    if ( strlen($status) > 20 )
    $status = substr( $status, 0, 20 );

    $status_obj = get_post_status_object( $status );

    if ( $new && $status_obj || in_array( $status, array( 'public', 'password' ) ) ) {
    $errors = new WP_Error();
    $errors->add( 'status_name', __( 'ERROR: That status name is already registered. Please choose another one.', 'pps' ) );
    $arr_return['retval'] = $errors;
    return $arr_return;
    }

    if ( $status_obj || $new ) {
    if ( empty($ltc_args['status_label']) && ! in_array( $status, array( 'pending', 'future', 'draft' ) ) ) {
    $errors = new WP_Error();
    $errors->add( 'status_label', __( 'ERROR: Please enter a label for the status.', 'pps' ) );
    $arr_return['retval'] = $errors;
    } else {
    $custom_conditions = (array) get_option( "pp_custom_conditions_post_status" );

    if ( ! isset($custom_conditions[$status]) )
    $custom_conditions[$status] = array();

    $custom_conditions[$status]['label'] = sanitize_text_field($ltc_args['status_label']);
    $custom_conditions[$status]['save_as_label'] = ( ! empty($ltc_args['status_save_as_label']) ) ? sanitize_text_field($ltc_args['status_save_as_label']) : '';
    $custom_conditions[$status]['publish_label'] = ( ! empty($ltc_args['status_publish_label']) ) ? sanitize_text_field($ltc_args['status_publish_label']) : '';

    if ( $new ) {
    $attrib_type = ( isset( $ltc_args['attrib_type'] ) ) ? pp_sanitize_key($ltc_args['attrib_type']) : '';
    if ( $attrib_type )
    $custom_conditions[$status][$attrib_type] = true;
    }

    pp_update_option( "custom_conditions_post_status", $custom_conditions );

    $arr_return['redirect'] = ( $new ) ? str_replace( 'pp-status-new', 'pp-stati', $_SERVER['REQUEST_URI'] ) : $_SERVER['REQUEST_URI'];

    // Edit Flow integration
    if ( ! empty($ltc_args['attrib_type']) && ( 'moderation' == $ltc_args['attrib_type'] ) && taxonomy_exists('post_status') && ! defined('PP_DISABLE_EF_STATUS_SYNC') ) {
    if ( ! $term = get_term_by( 'slug', $status, 'post_status' ) ) {
    // if another taxonomy already has a term with this slug, don't get implicated in that mess
    $term = (object) array( 'taxonomy' => 'post_status', 'alias_of' => '', 'description' => '', 'parent' => 0, 'slug' => $status, 'name' => $custom_conditions[$status]['label']);
    $slug = wp_unique_term_slug($status, $term);
    if ( $slug == $status )
    wp_insert_term( $custom_conditions[$status]['label'], 'post_status', array( 'slug' => $status ) );
    } else {
    if ( $custom_conditions[$status]['label'] != $term->name ) {
    wp_update_term( $term->term_id, 'post_status', array( 'name' => $custom_conditions[$status]['label'] ) );
    }
    }
    }
    }

    // === store status post types ===
    if ( ! $status_post_types = pp_get_option( 'status_post_types' ) )
    $status_post_types = array();

    if ( ! empty( $ltc_args['pp_status_all_types'] ) ) {
    $status_post_types[$status] = array();

    } elseif ( isset( $ltc_args['pp_status_post_types'] ) ) {
    if ( ! isset( $status_post_types[$status] ) )
    $status_post_types[$status] = array();

    if ( $add_types = array_intersect( $ltc_args['pp_status_post_types'], array('1', true, 1) ) )
    $status_post_types[$status] = array_unique( array_merge( $status_post_types[$status], array_map( 'pp_sanitize_key', array_keys($add_types) ) ) );

    if ( $remove_types = array_diff( $ltc_args['pp_status_post_types'], array('1', true, 1) ) )
    $status_post_types[$status] = array_diff( $status_post_types[$status], array_keys($remove_types) );
    }

    pp_update_option( 'status_post_types', $status_post_types );

    // === store status order ===
    if ( ! $status_order = pp_get_option( 'status_order' ) )
    $status_order = array();

    if ( ! empty($ltc_args['status_order']) && is_numeric( $ltc_args['status_order'] ) || ! empty( $status_order[$status] ) ) { // don't store value if no entry and not already stored
    $status_order[$status] = (int) $ltc_args['status_order'];
    pp_update_option( "status_order", $status_order );
    }
    } else {
    $errors = new WP_Error();
    $errors->add( 'condition_name', __( 'ERROR: The specified status does not exist.', 'pps' ) );
    $arr_return['retval'] = $errors;
    }

    return $arr_return;
    }
    ?>

    May you adopt it, this would give a new API to your users. Just, then, replace status-save_ppp.php by the following code :

    <?php
    if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

    class PPP_StatusSave {
    public static function save( $status, $new = false ) {
    require_once( dirname(__FILE__).'/ltc_pp_custom_status.php' ); // To be put in the same directory as modified_save-status_ppp.php
    $args = array (
    'new_status' => $new, // c'est une création
    'attrib_type' => $_REQUEST['attrib_type'],
    'status_slug' => $status,
    'status_label' => $_REQUEST['status_label'],
    'pp_status_all_types' => $_REQUEST['pp_status_all_types'],
    'pp_status_post_types' => $_REQUEST['pp_status_post_types'],

    'status_save_as_label' => $_REQUEST['status_save_as_label'], // Only for workflow statuses
    'status_publish_label' => $_REQUEST['status_publish_label'], // Only for workflow statuses
    'status_order' => $_REQUEST['status_order'], // Only for workflow statuses
    );
    $ss_result = ppp_save_status ($args);
    }
    ?>

    I have cheked it works with privacy statuses, not workflow. So, that’s little time to spend for you.

    Thanks in advance to include it in the next version !

    #25368 Reply

    [ login required ]

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.

Registered members have full access to Pro support forums here.

Feel free to log in or purchase a support key.