wp-posts-to-posts icon indicating copy to clipboard operation
wp-posts-to-posts copied to clipboard

Set connections required in certain custom post type

Open Cezarion opened this issue 10 years ago • 10 comments

I need to set some connection required in certains custom post type. I don't know if it's the best way, perhaps this code will help you and can be better :

Class p2pRequiredConnections
{
    public static $_postType = null;
    public static $requiredConnection = array(
                       'cpt-name' => array(
                            'connection_name',
                            'another_connection_name'
                        ),
    );

    public static function getRequiredConnections(){
        $requiredConnections = self::$requiredConnection;
        if( isset($requiredConnections[self::$_postType]) )
            return $requiredConnections[self::$_postType];

        return false;
    }

    public static function hasRequiredConnections($post_type){
        $requiredConnections = self::$requiredConnection;
        return array_key_exists($post_type, $requiredConnections);
    }

    public static function builConnectionsArray($p2p_ids){
        $existings = [];
        foreach($p2p_ids as $p2p_id){
            $p2p_object = p2p_get_connection($p2p_id);
            $existings[$p2p_id] = $p2p_object->p2p_type;
        }
        return $existings;
    }

    public static function checkRequiredConnection($p2p_ids){
        $requireds = self::getRequiredConnections();
        if( !$requireds ){
            return FALSE;
        }
        $existings = self::builConnectionsArray($p2p_ids);
        $missings  = array_diff($requireds, $existings);

        return count($missings) > 0 ? FALSE : TRUE;
    }

    public static function validate($post_type, $_post){
        self::$_postType = $post_type;

        if(!array_key_exists('p2p_connections', $_post) || empty($_post['p2p_connections'])){
            return FALSE;
        }

        return self::checkRequiredConnection($_post['p2p_connections']);
    }
}

add_filter('wp_insert_post_data', 'connection_data_validator', '99');
function connection_data_validator($data) {
    if (p2pRequiredConnections::hasRequiredConnections($data['post_type']) &&
        p2pRequiredConnections::validate($data['post_type'],$_POST) === FALSE) {
        // If post data is invalid then
        $data['post_status'] = 'pending';
        add_filter('redirect_post_location', 'p2p_redirect_filter', '99');
    }
    return $data;
}

function p2p_redirect_filter($location) {
  remove_filter('redirect_post_location', 'p2p_redirect_filter', '99');
  return add_query_arg('p2p_required', 1, $location);
}

add_action('admin_notices', 'p2p_required_admin_notices');
function p2p_required_admin_notices() {
  if (!isset($_GET['p2p_required'])) return;

  switch (absint($_GET['p2p_required'])) {
    case 1:
      $message ='All connections are required';
      break;
    default:
      $message = 'Unexpected error';
  }
  echo '<div id="notice" class="error"><p>' . $message . '</p></div>';
}

Cezarion avatar Oct 06 '15 20:10 Cezarion

Hello,

Thank you for your code. I try to make the plugin working with CPT so I added these lines in the file function.php but it returns an error. I'm new with Wordpress, what could be the problem ?

Kinzowa avatar Jan 15 '16 12:01 Kinzowa

Can you give more informations ?

Cezarion avatar Jan 15 '16 13:01 Cezarion

Yes for sure, this is the error : Parse error: syntax error, unexpected 'requiredConnection' (T_STRING), expecting variable (T_VARIABLE) in /htdocs/wp-content/themes/kleo-child/functions.php on line 16

Line 16 :

public static requiredConnection = 'ecoles' => array(

With ecoles the name of my CPT.

Kinzowa avatar Jan 15 '16 13:01 Kinzowa

@HornKild don't take me wrong, but it will be better if you employ a developer to setup it for you, try to ask first @Cezarion as he is already trying to help you.

meloniq avatar Jan 15 '16 13:01 meloniq

It works with the basic code, just need to specify the CPT name. My mistake For my information what is the purpose of your code Cezarion ?

Tx

On Fri, Jan 15, 2016 at 2:57 PM, Andrzej Piotrowski < [email protected]> wrote:

@HornKild https://github.com/HornKild don't take me wrong, but it will be better if you employ a developer to setup it for you, try to ask first @Cezarion https://github.com/Cezarion as he is already trying to help you.

— Reply to this email directly or view it on GitHub https://github.com/scribu/wp-posts-to-posts/issues/498#issuecomment-171967273 .

Kinzowa avatar Jan 15 '16 14:01 Kinzowa

@HornKild it's an error in my code.

public static requiredConnection = 'cpt-name' => array(
                            'connection_name',
                            'another_connection_name'
                        )

2 erros at least. Comma is missing, $ is missing. Give me some time and I make the correction

Cezarion avatar Jan 15 '16 14:01 Cezarion

Can you try with this update :

 public static $requiredConnection = array(
                       'cpt-name' => array(
                            'connection_name',
                            'another_connection_name'
                        ),
    );

Cezarion avatar Jan 15 '16 14:01 Cezarion

@HornKild : in my cpt some connections are required and I don't want that the cpt is published while there's not set.

Cezarion avatar Jan 15 '16 14:01 Cezarion

Ok, I have no error now

Kinzowa avatar Jan 15 '16 15:01 Kinzowa

Cool !

Cezarion avatar Jan 15 '16 15:01 Cezarion