diggy / polylang-cli

WP-CLI community package of Polylang commands
https://github.com/diggy/polylang-cli
MIT License
34 stars 19 forks source link

FEATURES : wp pll post create, wp pll post bind #45

Closed gbouteiller closed 7 years ago

gbouteiller commented 7 years ago

Hello @diggy,

Won't it be a good idea to offer the possibility to create posts in a specific language and link them?

  1. wp pll post create would look like wp post create but with the ability to pass the language.
  2. wp pll post bind would mimic pll_save_post_translations to bind posts created in 1.

My use case is to generate for every new website a basic structure with some pages ( Home, Contact, Blog, ... ) that are available in English and French by default and then add Home as frontpage with wp option update page_on_front. This currently doesn't work because, in order to update the option, polylang checks that the given id is translated and bound to a translation in every language.

diggy commented 7 years ago

@gbouteiller An important idea behind this package is that CRUD commands perform batch operations, e.g. wp post delete deletes a post, while wp pll post delete deletes a post and all its translations.

So I guess the same logic would have to be applied to wp pll post create, which would create a post and its "translations". More fine-grained control would be obtained by passing some extra params, e.g. wp pll post create 34 de would create a German translation for the post with ID 34.

Automating a multilingual setup is one of the main reasons this package exists. The use case you're describing (creating and setting home pages) is definitely something I would like to see polylang-cli be able to do. With a little creativity however (and the latest master) I think this is already possible:

$ wp option update show_on_front page
$ wp pll post generate --post_type=page --format=ids --count=1 | xargs -n1 -I {} sh -c 'wp option update page_on_front {}'
gbouteiller commented 7 years ago

At first, I was just thinking about something similar to polylang : a translation is a post, so wp pll post create is for creating only one translation ( I've forked your project to do something like that ). But I understand what you mean and it makes sense in the CRUD logic. So, how would you create a page with title and content in three different languages for example? About your example, wp post generate doesn't give you the possibility to set titles and translations, does it? For now, I'm using the fork to generate translated pages and link them together and after that I'm able to use wp option update page_on_front. Another question on my mind related to an issue I posted on polylang is about menu location assignments, do you want to discuss it here or do I open another issue for that?

diggy commented 7 years ago

Unfortunately, wp post generate allows only to set the post content, title is automatically generated based on the post type object's singular_name label.

So, how would you create a page with title and content in three different languages for example?

I'm not really sure, but very much open to suggestions :) Where would the input come from?

Right now I'm thinking to use wp create post internally and duplicate the post object to the other languages.

Another question on my mind related to an issue I posted on polylang is about menu location assignments, do you want to discuss it here or do I open another issue for that?

Yes, it would be better to discuss this in another ticket.

diggy commented 7 years ago

@gbouteiller I just pushed a wp pll menu create command at #48, we can discuss it there if you like

gbouteiller commented 7 years ago

@digg I answered you about the PR and it's crystal clear as I was trying to do the same but failed with wp menu assign ( I didn't know about the runcommand magic ). About wp pll post create, I don't know if it's possible/advised to use complex arguments ( like JSON for instance ). For now what I'm doing is :

wp pll post create fr --post_type=page --post_title='Accueil' --post_status=publish --porcelain # returns 3
wp pll post create en --post_type=page --post_title='Home' --post_status=publish --porcelain # returns 4
wp pll post bind '{ "en": 3, "fr": 4 }'

with :

public function bind( $args, $assoc_args ) {
  $bindings = json_decode( $args[0], true );
  $this->api->save_post_translations( $bindings );
  $this->cli->success( 'Posts bound.' );
}

public function create( $args, $assoc_args ) {
  list( $language ) = $args;

  $languages = $this->api->languages_list();
  $default_language = $this->api->default_language();

  if ( ! $this->api->is_translated_post_type( $this->cli->flag( $assoc_args, 'post_type' ) ) ) {

    $this->cli->error( 'Polylang does not manage languages and translations for this post type.' );
  }

  $porcelain = isset( $assoc_args['porcelain'] );
  $assoc_args['porcelain'] = true;

  ob_start();

  $this->cli->command( array( 'post', 'create' ), $assoc_args );

  $post_id = ob_get_clean();
  $this->api->set_post_language( $post_id, $language );
  $porcelain ? $this->cli->log( $post_id ) : $this->cli->success( sprintf( 'Created post %s.', $post_id ) );
}

The code is simple as it's just a proof of concept ( missing some error handling, etc ). And the use could be easily simplified as :

wp pll post create --post_type=page --post_title='{ "fr": "Accueil", "en": "Home"}'  --post_status=publish --porcelain

But I don't know if it's best practice, as I mentioned above, to use JSON arg in WP-CLI philosophy. Perhaps something like this instead :

wp pll post create --post_type=page --post_title='Accueil' --post_title_en='Home' --post-status=publish --porcelain