First, download PHPUnit and create the autoloader file (assuming composer is already installed):
$ composer install
Then, run the unit tests:
$ ./vendor/bin/phpunit
PHPUnit 3.7.26 by Sebastian Bergmann.
Configuration read from /Users/coreymcmahon/Sites/CostingResource/phpunit.xml
.....................................................
Time: 248 ms, Memory: 6.00Mb
OK (53 tests, 184 assertions)
To install the plugin, save the folder contents to the following path under your Wordpress installation:
(wordpress folder)/wp-content/plugins/CostingResource
Note: the following files and folders should be omitted when performing the upload: .git/
, tests/
, vendor/
, .gitignore
, README.md
, composer.lock
, composer.json
, phpunit.xml
. These files are only required during development
You should then navigate to the Plugins menu in the Wordpress administration dashboard, and click Activate next to the cutting calculator in the list.
Once the plugin is activated, you can use it on a page or post by using the shortcode:
[costing_resource_calculators]
Make sure the template has enough horizontal space to accommodate the calculator.
/ : Root level of the project.
front.php : The "front controller". All requests for the plugin are passed
through here.
index.php : The Wordpress Plugin metadata file. Creates the shortcode.
bootstrap.php : The bootstrap file. Responsible for including all dependencies.
assets/ : The HTML, JavaScript, images and CSS for generating the user
interface for the calculators.
data/ : CSV files that are used for providing the backend data for the
calculations.
src/ : The source-code for the calculators and HTTP routing code to
service AJAX requests.
There are a number of steps that need to be completed in order to create a new calculator.
Create a new folder (and PHP namespace) in src/CostingResource/
and name it after your new calculator (eg: for ExampleCalculator, use the folder / namespace Example
). You'll then need to create two files in here:
Calculator.php
: implements the calculation process. Seesrc/SpotWelding/Calculator.php
andsrc/Cutting/Calculator.php
and follow the format used there.CsvData.php
: implements the data retrieval process. Seesrc/SpotWelding/CsvData.php
andsrc/Cutting/CsvData.php
and follow the format used there.
Any PHP files that you create need to be added to bootstrap.php
, otherwise they won't be loaded at runtime.
Open the file src/CostingResource/Settings.php
and add the name and namespace for the new calculator to the list of static variables. Make sure you also add the new calculator to the function getCalculatorInstanceFor(...)
so that an instance of the new calculator class is returned.
Create the templates in a new folder in assets/templates/[calculator-name]
as demonstrated in assets/templates/cutting
and assets/templates/spot_welding
. Typically you'll create index.html.php
which is resposible for rendering the outer elements of the tabs, and then three sub-templates that correspond with each of the tabs.
Calculator specific JavaScript should go in assets/js/calculators/[calculator-name].js
(this will be automatically included by the plugin), while any event handlers that need to be attached should be done via inline JavaScript in the respective template files. This is so the event handlers are reattached when the form is re-rendered.
Styles go in assets/css/styles.css
(no calculator specific stylesheet, just use calculator-specific ID and class attributes). Images can be placed in assets/images
.