Permalink
Browse files

refactored app folder for api and assets compilation complexity

  • Loading branch information...
1 parent bab60da commit 5c64b1a8f39b6ff6def27caf86f6b512bcf4650f @nadimtuhin nadimtuhin committed Aug 18, 2015
View
@@ -0,0 +1,83 @@
+<?php namespace App;
+
+use App\Assets\BlocksScripts;
+use App\Assets\BuildModeScripts;
+use App\Assets\OnepageScripts;
+use App\Assets\WpConflictResolver;
+use App\Content\OnepageContent;
+use App\Content\OnepageInternalScripts;
+use App\Content\OnepageToolbar;
+use App\Loaders\BlocksLoader;
+use App\Loaders\PresetsLoader;
+use App\Loaders\TemplateLoader;
+use ThemeXpert\WordPress\PageTemplater;
+
+class Application {
+ protected $onepager;
+
+ public function __construct($onepager){
+ $this->onepager = $onepager;
+
+ $this->enqueue_assets();
+ $this->inject_page_contents();
+ $this->init_loaders();
+ }
+
+ protected function enqueue_assets() {
+ new WpConflictResolver();
+ new OnepageScripts();
+ new BlocksScripts();
+ new BuildModeScripts();
+
+ add_action('wp_enqueue_scripts', function(){
+ $is_build_mode = $this->isBuildMode();
+ $page_id = $this->getCurrentPageId();
+
+ $this->onepager->asset()->enqueue( ! $is_build_mode, $page_id );
+ });
+ }
+
+ protected function inject_page_contents(){
+ new OnepageToolbar();
+ new OnepageContent();
+ new OnepageInternalScripts();
+ }
+
+ protected function init_loaders() {
+ $blockManager = $this->getBlockManager();
+ $presetManager = $this->getPresetManager();
+ $pageTemplateManager = new PageTemplater();
+
+ new BlocksLoader($blockManager);
+ new PresetsLoader($presetManager);
+ new TemplateLoader($pageTemplateManager);
+ }
+
+ /**
+ * @return mixed
+ */
+ protected function isBuildMode() {
+ return $this->onepager->content()->isBuildMode();
+ }
+
+ /**
+ * @return mixed
+ */
+ protected function getCurrentPageId() {
+ return $this->onepager->content()->getCurrentPageId();
+ }
+
+ /**
+ * @return mixed
+ */
+ protected function getBlockManager() {
+ return $this->onepager->blockManager();
+ }
+
+ /**
+ * @return mixed
+ */
+ protected function getPresetManager() {
+ return $this->onepager->presetManager();
+ }
+}
@@ -0,0 +1,83 @@
+<?php namespace App\Assets;
+
+class BlocksScripts {
+ public function __construct( $enqueue = true ) {
+ if ( $enqueue ) {
+ add_action( 'wp_enqueue_scripts', [ $this, 'enqueue' ] );
+ }
+ }
+
+ public function enqueue() {
+ if ( $this->isBuildMode() ) {
+ $this->enqueueAllBlocksScripts();
+ } else {
+ $pageId = $this->getCurrentPageId();
+ $sections = $this->getAllValidSections( $pageId );
+
+ $this->enqueueSectionBlocks( $sections );
+ }
+ }
+
+ public function enqueueSectionBlocks( $sections ) {
+ $blocks = $this->getBlocksFromUsedSections( $sections );
+
+ $this->enqueueBlocksScripts( $blocks );
+ }
+
+ protected function enqueueAllBlocksScripts() {
+ $blocks = (array) onepager()->blockManager()->all();
+
+ $this->enqueueBlocksScripts( $blocks );
+ }
+
+ protected function getBlocksFromUsedSections( $sections ) {
+ $blockSlugs = array_unique( array_map( function ( $section ) {
+ return $section['slug'];
+ }, $sections ) );
+
+ return array_filter( array_map( function ( $blockSlug ) {
+ $block = onepager()->blockManager()->get( $blockSlug );
+
+ return $block ?: false;
+ }, $blockSlugs ) );
+ }
+
+ protected function enqueueBlocksScripts( $blocks ) {
+ array_walk( $blocks, [ $this, 'enqueueBlockScripts' ] );
+ }
+
+ protected function enqueueBlockScripts( $block ) {
+ $enqueueCb = $block['enqueue'];
+
+ if ( ! $enqueueCb ) {
+ return;
+ }
+
+ $blockUrl = $block['url'];
+ $enqueueCb( $blockUrl );
+ }
+
+ /**
+ * @return mixed
+ */
+ protected function isBuildMode() {
+ return onepager()->content()->isBuildMode();
+ }
+
+ /**
+ * @return mixed
+ */
+ protected function getCurrentPageId() {
+ return onepager()->content()->getCurrentPageId();
+ }
+
+ /**
+ * @param $pageId
+ *
+ * @return mixed
+ */
+ protected function getAllValidSections( $pageId ) {
+ return onepager()->section()->getAllValid( $pageId );
+ }
+
+}
@@ -0,0 +1,84 @@
+<?php namespace App\Assets;
+
+use App\Assets\Traits\FormEngineScripts;
+
+class BuildModeScripts {
+ use FormEngineScripts;
+
+ public function __construct( ) {
+ add_action( 'wp_enqueue_scripts', [$this, 'enqueueScripts']);
+ }
+
+ public function enqueueScripts() {
+ if ( ! $this->isBuildMode() ) {
+ return false;
+ }
+
+ $this->enqueueFormEngineScripts();
+ $this->enqueueInitializerScript();
+ }
+
+ protected function enqueueInitializerScript() {
+ $pageId = $this->getCurrentPageId();
+ $asset = onepager()->asset();
+ $data = $this->localizeScriptData( $pageId );
+
+
+ $asset->script( 'onepager', asset( 'assets/app.bundle.js' ), [ 'jquery' ] );
+ $asset->localizeScript( 'onepager', $data, 'onepager' );
+ }
+
+ function localizeScriptData( $pageId ) {
+ $onepager = onepager();
+
+ $ajaxUrl = $onepager->api()->getAjaxUrl();
+ $nav_arr = $onepager->content()->getMenus();
+ $cat_arr = $onepager->content()->getCategories();
+ $pages_arr = $onepager->content()->getPages();
+ $blocks = array_values( (array) $onepager->blockManager()->all() );
+ $groupOrder = $onepager->blockManager()->getGroupOrder();
+
+ $sections = array_map( function ( $section ) {
+ $section = onepager()->render()->sectionBlockDataMerge( $section );
+ $section['content'] = onepager()->render()->section( $section );
+ $section['style'] = onepager()->render()->style( $section );
+
+ return $section;
+ }, onepager()->section()->getAllValid( $pageId ) );
+
+ $footer_markup = get_editor_section_list_footer();
+ $disableUrl = getOpBuildModeUrl( getCurrentPageURL(), false );
+
+ return array(
+ 'ajaxUrl' => $ajaxUrl,
+ 'optionPanel' => onepager()->optionsPanel( "onepager" )->getOptions(),
+ 'options' => get_option( 'onepager' ),
+ 'page' => 'onepager',
+ 'blocks' => $blocks,
+ 'pageId' => $pageId,
+ 'sections' => $sections,
+ 'menus' => $nav_arr,
+ 'pages' => $pages_arr,
+ 'categories' => $cat_arr,
+ 'groupOrder' => $groupOrder,
+ 'footer' => $footer_markup,
+ 'disable' => $disableUrl,
+ 'presets' => \Onepager::getPresets(),
+ 'basePreset' => \Onepager::getBasePreset()
+ );
+ }
+
+ /**
+ * @return mixed
+ */
+ protected function isBuildMode() {
+ return onepager()->content()->isBuildMode();
+ }
+
+ /**
+ * @return mixed
+ */
+ protected function getCurrentPageId() {
+ return onepager()->content()->getCurrentPageId();
+ }
+}
@@ -0,0 +1,29 @@
+<?php namespace App\Assets;
+
+use App\Assets\Traits\CommonAssets;
+
+class OnepageScripts {
+ use CommonAssets;
+
+ public function __construct() {
+ add_action( 'wp_enqueue_scripts', [$this, 'enqueueScripts']);
+ }
+
+ public function enqueueScripts() {
+ if ( ! onepager()->content()->isOnepage() ) {
+ return;
+ }
+
+ $this->enqueueCommonScripts();
+ $this->enqueuePageScripts();
+ }
+
+ protected function enqueuePageScripts(){
+ $asset = onepager()->asset();
+
+ $asset->script( 'wow', asset( 'assets/js/wow.js' ), [ 'jquery' ] );
+ $asset->script( 'nicescroll', asset( 'assets/js/jquery.nicescroll.js' ), [ 'jquery' ] );
+ $asset->script( 'lithium', asset( 'assets/lithium.js' ), [ 'jquery' ] );
+ $asset->style( 'lithium', asset( 'assets/css/lithium.css' ) );
+ }
+}
@@ -0,0 +1,19 @@
+<?php namespace App\Assets;
+
+use App\Assets\Traits\CommonAssets;
+use App\Assets\Traits\FormEngineScripts;
+
+class OptionsPanelScripts {
+ use CommonAssets;
+ use FormEngineScripts;
+
+ public function enqueue( ) {
+ $this->enqueueCommonScripts();
+ $this->enqueueFormEngineScripts();
+
+ $asset = onepager()->asset();
+ $asset->style( 'lithium-ui', asset( 'assets/css/lithium-builder.css' ) );
+ $asset->script( 'admin-bundle', asset( 'assets/optionspanel.bundle.js' ), [ 'jquery' ] );
+ $asset->enqueue();
+ }
+}
@@ -0,0 +1,16 @@
+<?php namespace App\Assets\Traits;
+
+trait CommonAssets {
+ public function enqueueCommonScripts( ) {
+ $asset = onepager()->asset();
+
+ $asset->style( 'bootstrap', asset( 'assets/css/bootstrap.css' ) );
+ $asset->script( 'bootstrap', asset( 'assets/js/bootstrap.js' ), [ 'jquery' ] );
+ $asset->style( 'animatecss', asset( 'assets/css/animate.css' ) );
+ $asset->style( 'fontawesome', asset( 'assets/css/font-awesome.css' ) );
+
+ if ( is_super_admin() ) {
+ $asset->style( 'lithium-ui', asset( 'assets/css/lithium-builder.css' ) );
+ }
+ }
+}
@@ -0,0 +1,50 @@
+<?php namespace App\Assets\Traits;
+
+use _WP_Editors;
+
+trait FormEngineScripts {
+ public function enqueueFormEngineScripts() {
+ $this->jsWpEditor();
+ $this->formEngineScripts();
+
+ wp_enqueue_media();
+ }
+
+ protected function formEngineScripts() {
+ $asset = onepager()->asset();
+
+ // tx namespaced assets to avoid multiple assets loading from other ThemeXpert product
+ $asset->script( 'tx-bootstrap-switch', asset( 'assets/js/bootstrap-switch.js' ), [ 'jquery' ] );
+ $asset->script( 'tx-bootstrap-select', asset( 'assets/js/bootstrap-select.js' ), [ 'jquery' ] );
+ $asset->script( 'tx-iconselector', asset( 'assets/js/icon-selector-bootstrap.js' ), [ 'jquery' ] );
+
+ $asset->script( 'tx-colorpicker', asset( 'assets/js/bootstrap-colorpicker.js' ), [ 'jquery' ] );
+ $asset->style( 'tx-colorpicker', asset( "assets/css/bootstrap-colorpicker.css" ) );
+
+ $asset->script( 'tx-toastr', asset( 'assets/js/toastr.js' ), [ 'jquery' ] );
+ }
+
+ protected function jsWpEditor( $settings = array() ) {
+ if ( ! class_exists( '_WP_Editors' ) ) {
+ require( ABSPATH . WPINC . '/class-wp-editor.php' );
+ }
+
+ $set = _WP_Editors::parse_settings( 'apid', $settings );
+ if ( ! current_user_can( 'upload_files' ) ) {
+ $set['media_buttons'] = false;
+ }
+ if ( $set['media_buttons'] ) {
+ wp_enqueue_script( 'thickbox' );
+ wp_enqueue_style( 'thickbox' );
+ wp_enqueue_script( 'media-upload' );
+ $post = get_post();
+ if ( ! $post && ! empty( $GLOBALS['post_ID'] ) ) {
+ $post = $GLOBALS['post_ID'];
+ }
+ wp_enqueue_media( array(
+ 'post' => $post,
+ ) );
+ }
+ _WP_Editors::editor_settings( 'apid', $set );
+ }
+}
Oops, something went wrong.

0 comments on commit 5c64b1a

Please sign in to comment.