Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

38279 Third party libraries and scripts #612

Merged
merged 3 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
use onOffice\WPlugin\Utility\__String;
use onOffice\WPlugin\Utility\Redirector;
use onOffice\WPlugin\WP\WPQueryWrapper;
use onOffice\WPlugin\ScriptLoader\IncludeFileModel;

define('ONOFFICE_DI_CONFIG_PATH', implode(DIRECTORY_SEPARATOR, [ONOFFICE_PLUGIN_DIR, 'config', 'di-config.php']));

Expand Down Expand Up @@ -399,4 +400,16 @@ function update_status_close_action_button_option()
};
}, 500);

add_filter('script_loader_tag', 'filter_script_loader_tag', 10, 2);
function filter_script_loader_tag($tag, $handle) {
$attributes = [IncludeFileModel::LOAD_DEFER, IncludeFileModel::LOAD_ASYNC];
foreach ($attributes as $attr) {
if (!wp_scripts()->get_data($handle, $attr)) continue;
if (!preg_match(":\s$attr(=|>|\s):", $tag))
$tag = preg_replace(':(?=></script>):', " $attr", $tag, 1);
break;
}
return $tag;
}

return $pDI;
25 changes: 25 additions & 0 deletions plugin/ScriptLoader/IncludeFileModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ class IncludeFileModel

const TYPE_STYLE = 'style';

const LOAD_DEFER = 'defer';

const LOAD_ASYNC = 'async';

/** @var string */
private $_identifier = '';

Expand All @@ -48,6 +52,9 @@ class IncludeFileModel
/** @var string */
private $_type = '';

/** @var string */
private $_loadAsynchronous = '';


/**
*
Expand Down Expand Up @@ -150,4 +157,22 @@ public function setLoadInFooter(bool $loadInFooter): self
$this->_loadInFooter = $loadInFooter;
return $this;
}

/**
* @return string
*/
public function getLoadAsynchronous(): string
{
return $this->_loadAsynchronous;
}

/**
* @param string $loadAsynchronous
* @return $this
*/
public function setLoadAsynchronous(string $loadAsynchronous): self
{
$this->_loadAsynchronous = $loadAsynchronous;
return $this;
}
}
1 change: 1 addition & 0 deletions plugin/ScriptLoader/ScriptLoaderGeneric.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public function enqueue()
/* @var $pIncludeModel IncludeFileModel */
foreach ($this->getModelByType(IncludeFileModel::TYPE_SCRIPT) as $pIncludeModel) {
wp_enqueue_script($pIncludeModel->getIdentifier());
wp_script_add_data($pIncludeModel->getIdentifier(), $pIncludeModel->getLoadAsynchronous(), true);
}
foreach ($this->getModelByType(IncludeFileModel::TYPE_STYLE) as $pIncludeModel) {
wp_enqueue_style($pIncludeModel->getIdentifier());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,12 @@ public function getScriptLoaderGenericConfiguration(): array
$pluginPath = ONOFFICE_PLUGIN_DIR.'/index.php';
$script = IncludeFileModel::TYPE_SCRIPT;
$style = IncludeFileModel::TYPE_STYLE;
$defer = IncludeFileModel::LOAD_DEFER;

$values = [
(new IncludeFileModel($script, 'select2', plugins_url('/vendor/select2/select2/dist/js/select2.min.js', $pluginPath)))
->setLoadInFooter(true),
->setLoadInFooter(true)
->setLoadAsynchronous($defer),
(new IncludeFileModel($script, 'onoffice-custom-select', plugins_url('/js/onoffice-custom-select.js', $pluginPath)))
->setLoadInFooter(true),
(new IncludeFileModel($script, 'onoffice-multiselect', plugins_url('/js/onoffice-multiselect.js', $pluginPath)))
Expand Down
2 changes: 2 additions & 0 deletions tests/TestClassIncludeFileModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,5 +63,7 @@ public function testSetter(IncludeFileModel $pIncludeFileModel)
$this->assertEquals(['test1', 'test2'], $pIncludeFileModel->getDependencies());
$this->assertSame($pIncludeFileModel, $pIncludeFileModel->setLoadInFooter(true));
$this->assertTrue($pIncludeFileModel->getLoadInFooter());
$this->assertSame($pIncludeFileModel, $pIncludeFileModel->setLoadAsynchronous('defer'));
$this->assertEquals('defer', $pIncludeFileModel->getLoadAsynchronous());
}
}
2 changes: 2 additions & 0 deletions tests/TestClassScriptLoaderGeneric.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public function prepare()
$pModel1 = new IncludeFileModel(IncludeFileModel::TYPE_SCRIPT, 'script1', '/test/file.js');
$pModel1->setDependencies(['testdep']);
$pModel1->setLoadInFooter(true);
$pModel1->setLoadAsynchronous('defer');
$pModel2 = new IncludeFileModel(IncludeFileModel::TYPE_STYLE, 'style2', '/test/file.css');
return [$pModel1, $pModel2];
}));
Expand Down Expand Up @@ -91,5 +92,6 @@ public function testEnqueue()
$this->_pSubject->enqueue();
$this->assertEquals(['style2'], wp_styles()->queue);
$this->assertEquals(['script1'], wp_scripts()->queue);
$this->assertTrue(wp_scripts()->get_data('script1', 'defer'));
}
}
Loading