jmlapam

Insert terms dynamically

Jan 30th, 2016
62
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php if ( php_sapi_name() !== 'cli' || isset( $_SERVER['REMOTE_ADDR'] ) ) {
  2.     die( 'CLI Only' );
  3. }
  4.  
  5. // Get first arg
  6. if ( ! isset( $argv ) || count( $argv ) < 3 ) {
  7.     echo "Missing parameters.\n";
  8.     echo "script usage: php bea-insert-terms.php [taxonomy](required, must be already registered) [terms](required, list of terms to be inserted comma separated) \n";
  9.     die();
  10. }
  11.  
  12. //Domain
  13. $domain   = ( isset( $argv[1] ) ) ? $argv[1] : '';
  14. $taxonomy = ( isset( $argv[2] ) ) ? $argv[2] : '';
  15. $terms    = ( isset( $argv[3] ) ) ? explode( ",", $argv[3] ) : array();
  16.  
  17. // Fake WordPress, build server array
  18. $_SERVER = array(
  19.     'HTTP_HOST'       => $domain,
  20.     'SERVER_NAME'     => $domain,
  21.     'REQUEST_URI'     => '',
  22.     'REQUEST_METHOD'  => 'GET',
  23.     'SCRIPT_NAME'     => basename( __FILE__ ),
  24.     'SCRIPT_FILENAME' => basename( __FILE__ ),
  25.     'PHP_SELF'        => basename( __FILE__ )
  26. );
  27.  
  28. // Load WordPress
  29. $bootstrap = 'wp-load.php';
  30. while ( ! is_file( $bootstrap ) ) {
  31.     if ( is_dir( '..' ) ) {
  32.         chdir( '..' );
  33.     } else {
  34.         die( 'EN: Could not find WordPress! FR : Impossible de trouver WordPress !' );
  35.     }
  36. }
  37.  
  38. require_once( $bootstrap );
  39.  
  40. /**
  41.  * Class BEA_Insert_Terms
  42.  * The purpose is to pass a list of terms to insert in specified taxonomy
  43.  */
  44. class BEA_Insert_Terms {
  45.  
  46.     /**
  47.      * Insert terms dynamically
  48.      *
  49.      * @author Julien Maury
  50.      * @param string $taxonomy targeted taxonomy
  51.      * @param array $terms list of terms to be inserted
  52.      * @return void
  53.      */
  54.     public static function insert_terms( $taxonomy, $terms = array() ) {
  55.  
  56.         if ( ! is_string( $taxonomy ) ) {
  57.             echo sprintf( 'This parameter should be a string please !' ) . PHP_EOL;
  58.             exit( 1 );
  59.         }
  60.  
  61.         if ( ! taxonomy_exists( $taxonomy ) ) {
  62.             echo sprintf( 'This taxonomy is not even registered here, break your legs !' ) . PHP_EOL;
  63.             exit( 1 );
  64.         }
  65.  
  66.         $i = 0;
  67.         foreach ( $terms as $term ) {
  68.             $i ++;
  69.             $insert = wp_insert_term( $term, $taxonomy );
  70.             echo is_wp_error( $insert ) ? sprintf( '%s was not inserted : %s', $term, $insert->get_error_message() ) . PHP_EOL : sprintf( '%s inserted successfully :)', $term ) . PHP_EOL;
  71.         }
  72.  
  73.         echo sprintf( '%d terms inserted in %s', $i, $taxonomy ) . PHP_EOL;
  74.         exit( 0 );
  75.     }
  76.  
  77. }
  78.  
  79. BEA_Insert_Terms::insert_terms( $taxonomy, $terms );
RAW Paste Data