PHP code example of snicco / better-wp-api

1. Go to this page and download the library: Download snicco/better-wp-api library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

snicco / better-wp-api example snippets


use Snicco\Component\BetterWPAPI\BetterWPAPI;

class CSVImporter {

    public function __construct(BetterWPAPI $wp = null) {
        $this->wp = $wp ?: new BetterWPAPI();
    }
    
    public function import(string $file){
    
        if(!$this->wp->currentUserCan('import-csv')) {
            throw new Exception('Not authorized');
        }
        // import csv.
    }
    
}

$importer = new CSVImporter();

$importer->import(__DIR__.'/orders.csv');
 
class CSVImporterTest extends TestCase {

   /**
    * @test
    */
    public function that_missing_permissions_throw_an_exception() {
        
        $this->expectExceptionMessage('Not authorized');
        
        $wp = new class extends BetterWPAPI {
            public function currentUserCan():bool {
                return false;
            }
        }
        
        $importer = new CSVImporter($wp);
        $importer->import(__DIR__.'/test-users.csv');
    }

}