3

I am building a PHP function to enqueue JavaScript files into a PHP array and then have another PHP function that will load all the JS files into a page and load them in the order based on a sort number that can be passed into the enqueue function. Similar to how WordPress loads JS and CSS files.

So my PHP function enqueue_js_script() might look like this below which takes in a key name for the JS file, a file path to the JS file, and a sort order number which is optional. It then would add the JS file to a PHP class property $this->_js_files[$script_key]...

public function enqueue_js_script($script_key, $file_source, $load_order = 0){
    $this->_js_scripts[$script_key] = $file_source;
}

Then I will also have a PHP function load_js_scripts() which will print each script file path into the header of a webpages HTML.

This is where I want to take into consideration the $load_order passed into enqueue_js_script() to print the scripts into the HTML in the order based on these numbers.

How can I use this sort order number to sort my array of JS scripts?


UPDATE

It looks like I should store the sort number in an array like this instead...

    $this->_js_files[$script_key] = array(
      'file_source' => $file_source,
      'sort' => $load_order
    );
2
  • 2
    To simplify your question, you are passing in a key, a value, and a sort order. You'll need to save your value and sort order in an object and write a custom sort function using usort php.net/manual/en/function.usort.php Commented Feb 5, 2016 at 22:24
  • What's the purpose of $script_key? Can more than one script file be assigned to the same $script_key? Commented Feb 5, 2016 at 22:33

2 Answers 2

2

Using usort and a custom sorting function:

<?php 
public function enqueue_js_script($script_key, $file_source, $load_order = 0){
    $jsScript = new \stdClass;
    $jsScript->load_order = $load_order;
    $jsScript->script_key = $script_key;
    $this->_js_scripts[$script_key] = $jsScript;
}

function sortJSFiles($a, $b)
{
    if ($a->load_order == $b->load_order) {
        return 0;
    }
    return ($a->load_order < $b->load_order) ? -1 : 1;
}


usort($this->_js_scripts, "sortJSFiles");
Sign up to request clarification or add additional context in comments.

Comments

1

Having to pass your array key is not really good practice. The $array[] = $foo construction adds $foo as the new last item of $array.

Using usort.

<?php
class OrderStack {
    private $contents = array();
    public function add($order, $load) {
        if (!is_int($order) || $order < 0) {
            throw new InvalidArgumentException("$order must be a non-negative integer");
        }
        $this->contents[] = array($order, $load);
    }
    public function get_array() {
        usort(
            $this->contents,
            'OrderStack::compare'
        );
        return array_map(
            'OrderStack::get_load',
            $this->contents
        );
    }
    private static function get_load($stack_item) {
        return $stack_item[1];
    }
    private static function compare($a, $b) {
        return $a[0] - $b[0];
    }
}

class YourClass {
    private $_js_scripts;
    public function __construct() {
        $this->_js_scripts = new OrderStack();
    }
    public function enqueue_js_script($file_source, $load_order = 0) {
        $this->_js_scripts->add($load_order, $file_source);
    }
    public function get_js_scripts() {
        return $this->_js_scripts->get_array();
    }
}
?>

The OrderStack class is reusable.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.