NEXT craftinamerica.org. Base setup for headless wordpress https://www.craftinamerica.org
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

command.php 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. WP_CLI::add_command( 'p2p', 'P2P_CLI_Command' );
  3. class P2P_CLI_Command extends WP_CLI_Command {
  4. /**
  5. * List registered connection types.
  6. *
  7. * @subcommand connection-types
  8. */
  9. function connection_types() {
  10. foreach ( P2P_Connection_Type_Factory::get_all_instances() as $p2p_type => $ctype ) {
  11. WP_CLI::line( $p2p_type );
  12. }
  13. }
  14. /**
  15. * Generate connections for a specific connection type.
  16. *
  17. * @subcommand generate-connections
  18. * @synopsis <connection-type> [--items]
  19. */
  20. function generate_connections( $args, $assoc_args ) {
  21. list( $connection_type ) = $args;
  22. $ctype = p2p_type( $connection_type );
  23. if ( !$ctype )
  24. WP_CLI::error( "'$connection_type' is not a registered connection type." );
  25. if ( isset( $assoc_args['items'] ) ) {
  26. foreach ( _p2p_extract_post_types( $ctype->side ) as $ptype ) {
  27. $assoc_args = array( 'post_type' => $ptype );
  28. WP_CLI::launch( 'wp post generate' . \WP_CLI\Utils\assoc_args_to_str( $assoc_args ) );
  29. }
  30. }
  31. $count = $this->_generate_c( $ctype );
  32. WP_CLI::success( "Created $count connections." );
  33. }
  34. private function _generate_c( $ctype ) {
  35. $extra_qv = array( 'p2p:per_page' => 10 );
  36. $candidate = $ctype
  37. ->set_direction( 'from' )
  38. ->get_connectable( 'any', $extra_qv, 'abstract' );
  39. $count = 0;
  40. foreach ( $candidate->items as $from ) {
  41. $eligible = $ctype->get_connectable( $from, array(
  42. 'p2p:per_page' => rand( 0, 5 )
  43. ), 'abstract' );
  44. foreach ( $eligible->items as $to ) {
  45. $r = $ctype->connect( $from, $to );
  46. if ( is_wp_error( $r ) )
  47. WP_CLI::warning( $r );
  48. else
  49. $count++;
  50. }
  51. }
  52. return $count;
  53. }
  54. /**
  55. * Set up the example connections.
  56. *
  57. * @subcommand setup-example
  58. */
  59. function setup_example() {
  60. $ctype = p2p_type( 'actor_movie' );
  61. $data = array(
  62. 'Nicholas Cage' => array( 'Lord Of War', 'Adaptation' ),
  63. 'Jude Law' => array( 'Sherlock Holmes' ),
  64. 'Brad Pitt' => array( '7 Years In Tibet', 'Fight Club' ),
  65. 'Natalie Portman' => array( 'Black Swan', 'Thor' ),
  66. 'Matt Damon' => array( 'The Talented Mr. Ripley' ),
  67. 'Charlize Theron' => array(),
  68. );
  69. foreach ( $data as $actor_name => $movies ) {
  70. $actor = self::titled_post( 'actor', $actor_name );
  71. foreach ( $movies as $movie_title ) {
  72. $movie = self::titled_post( 'movie', $movie_title );
  73. $ctype->connect( $actor, $movie );
  74. }
  75. }
  76. WP_CLI::success( "Set up the actors and movies example." );
  77. }
  78. private static function titled_post( $type, $title ) {
  79. return wp_insert_post( array(
  80. 'post_type' => $type,
  81. 'post_title' => $title,
  82. 'post_status' => 'publish'
  83. ) );
  84. }
  85. }