Advertisement
longnguyenwp

Test transaction - MB Custom Table

Aug 15th, 2021
1,444
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.30 KB | None | 0 0
  1. <?php
  2. // Step 1: Register a model.
  3. add_action( 'init', 'prefix_register_transaction_model' );
  4. function prefix_register_transaction_model() {
  5.     mb_register_model( 'transaction', [
  6.         'table'  => 'transactions',
  7.         'labels' => [
  8.             'name'          => 'Transactions',
  9.             'singular_name' => 'Transaction',
  10.         ],
  11.         'menu_icon' => 'dashicons-money-alt',
  12.     ] );
  13. }
  14.  
  15. // Step 2: Create a custom table for the model.
  16. add_action( 'init', 'prefix_register_transaction_table' );
  17. function prefix_register_transaction_table() {
  18.     MetaBox\CustomTable\API::create(
  19.         'transactions',                    // Table name.
  20.         [                                  // Table columns (without ID).
  21.             'created_at' => 'DATETIME',
  22.             'amount'     => 'BIGINT',
  23.             'email'      => 'VARCHAR(20)',
  24.             'gateway'    => 'TEXT',
  25.             'status'     => 'VARCHAR(20)',
  26.             'screenshot' => 'TEXT',
  27.         ],
  28.         ['email', 'status'],               // List of index keys.
  29.         true                               // THIS: Must be true for models.
  30.     );
  31. }
  32.  
  33. // Step 3: Register fields for model, corresponding to the custom table structure.
  34. add_filter( 'rwmb_meta_boxes', 'prefix_register_transaction_fields' );
  35. function prefix_register_transaction_fields( $meta_boxes ) {
  36.     $meta_boxes[] = [
  37.         'title'        => 'Transaction Details',
  38.         'models'       => ['transaction'], // Model name
  39.         'storage_type' => 'custom_table',  // Must be 'custom_table'
  40.         'table'        => 'transactions',  // Custom table name
  41.         'fields'       => [
  42.             [
  43.                 'id'          => 'created_at',
  44.                 'type'        => 'datetime',
  45.                 'name'        => 'Created at',
  46.                 'js_options'  => [
  47.                     'timeFormat' => 'HH:mm:ss',
  48.                 ],
  49.                 'admin_columns' => true,
  50.             ],
  51.             [
  52.                 'id'     => 'amount',
  53.                 'type'   => 'number',
  54.                 'name'   => 'Amount',
  55.                 'append' => 'USD',
  56.                 'admin_columns' => [
  57.                     'position' => 'after id',
  58.                     'sort'     => true,
  59.                 ],
  60.             ],
  61.             [
  62.                 'id'   => 'gateway',
  63.                 'name' => 'Gateway',
  64.                 'admin_columns' => true,
  65.             ],
  66.             [
  67.                 'id'   => 'status',
  68.                 'type' => 'select',
  69.                 'name' => 'Status',
  70.                 'options' => [
  71.                     'pending'   => 'Pending',
  72.                     'completed' => 'Completed',
  73.                     'refunded'  => 'Refunded',
  74.                 ],
  75.                 'admin_columns' => true,
  76.             ],
  77.         ],
  78.     ];
  79.  
  80.     $meta_boxes[] = [
  81.         'title'        => 'Additional Transaction Details',
  82.         'models'       => ['transaction'], // Model name
  83.         'storage_type' => 'custom_table',  // Must be 'custom_table'
  84.         'table'        => 'transactions',  // Custom table name
  85.         'fields'       => [
  86.             [
  87.                 'id'   => 'email',
  88.                 'type' => 'email',
  89.                 'name' => 'Email',
  90.                 'admin_columns' => [
  91.                     'position'   => 'after amount',
  92.                     'searchable' => true,
  93.                 ],
  94.             ],
  95.             [
  96.                 'id'            => 'screenshot',
  97.                 'type'          => 'image_advanced',
  98.                 'name'          => 'Screenshots',
  99.                 'admin_columns' => true,
  100.             ],
  101.         ],
  102.     ];
  103.  
  104.     return $meta_boxes;
  105. }
  106.  
  107. add_action( 'init', function() {
  108.     // Get raw value via API.
  109.     $value = \MetaBox\CustomTable\API::get_value( 'screenshot', 17, 'transactions' );
  110.     // ray( $value );
  111.  
  112.     // Get meaningful value with helper function.
  113.     $value2 = rwmb_meta( 'screenshot', [
  114.         'object_type' => 'model',
  115.         'type'        => 'transaction',
  116.     ], 17 );
  117.     // ray( $value2 );
  118. }, 30 ); // Priority 30 to ensure meta boxes are registered (at init with priority = 20)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement