| 1 |
<?php |
| 2 |
namespace Depicter\Document\Helper; |
| 3 |
|
| 4 |
|
| 5 |
use Averta\WordPress\Utility\JSON; |
| 6 |
use Depicter\Document\CSS\Selector; |
| 7 |
|
| 8 |
class Helper |
| 9 |
{ |
| 10 |
/** |
| 11 |
* Generate data-actions value for DOM |
| 12 |
* |
| 13 |
* @param array $actions |
| 14 |
* |
| 15 |
* @return string |
| 16 |
*/ |
| 17 |
public static function getActions( $actions ) { |
| 18 |
if ( !is_array( $actions ) || empty( $actions ) ) { |
| 19 |
return ''; |
| 20 |
} |
| 21 |
|
| 22 |
$output = []; |
| 23 |
foreach ( $actions as $key => $action ) { |
| 24 |
$action = (array) $action; |
| 25 |
|
| 26 |
$action_params = []; |
| 27 |
$action_params[] = $action['type']; |
| 28 |
$action_params[] = $action['trigger']; |
| 29 |
$action_params[] = (int) ! empty( $action['delay'] ) ? $action['delay'] : 0; |
| 30 |
|
| 31 |
if( !empty( $action['options'] ) ){ |
| 32 |
$action_params[] = $action['options']; |
| 33 |
} |
| 34 |
|
| 35 |
$output[] = $action_params; |
| 36 |
} |
| 37 |
|
| 38 |
return JSON::encode( $output ); |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
/** |
| 43 |
* Extract section ID from slug |
| 44 |
* |
| 45 |
* @param string $sectionSlug |
| 46 |
* |
| 47 |
* @return string |
| 48 |
*/ |
| 49 |
public static function getSectionIdFromSlug( $sectionSlug = '' ){ |
| 50 |
return ltrim( $sectionSlug, Selector::SECTION_PREFIX . '-' ); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Extract element ID from slug |
| 55 |
* |
| 56 |
* @param string $elementSlug |
| 57 |
* |
| 58 |
* @return string |
| 59 |
*/ |
| 60 |
public static function getElementIdFromSlug( $elementSlug = '' ){ |
| 61 |
return ltrim( $elementSlug, Selector::ELEMENT_PREFIX . '-' ); |
| 62 |
} |
| 63 |
|
| 64 |
public static function getSectionCssId( $documentId, $sectionId ){ |
| 65 |
return Selector::getFullSelectorPath( $documentId, $sectionId ); |
| 66 |
} |
| 67 |
|
| 68 |
public static function sectionsSlugsListToCssIDsList( $sectionsSlugList, $documentId ){ |
| 69 |
|
| 70 |
if( empty( $sectionsSlugList ) || ! is_array( $sectionsSlugList ) ){ |
| 71 |
return []; |
| 72 |
} |
| 73 |
|
| 74 |
$sectionsCssIDsList = []; |
| 75 |
|
| 76 |
foreach( $sectionsSlugList as $sectionSlug ){ |
| 77 |
$sectionIdNumber = self::getSectionIdFromSlug( $sectionSlug ); |
| 78 |
$sectionsCssIDsList[] = self::getSectionCssId( $documentId, $sectionIdNumber ); |
| 79 |
} |
| 80 |
|
| 81 |
return $sectionsCssIDsList; |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* extract third party IDs from document content |
| 86 |
* @param $content |
| 87 |
* |
| 88 |
* @return array|void |
| 89 |
*/ |
| 90 |
public static function extractAssetIds( $content) { |
| 91 |
|
| 92 |
if ( empty( $content ) ) { |
| 93 |
return; |
| 94 |
} |
| 95 |
|
| 96 |
$assetIDs = []; |
| 97 |
|
| 98 |
$patterns = [ |
| 99 |
'"source":"([^"]*)"', |
| 100 |
'"src":"([^"]*)"' |
| 101 |
]; |
| 102 |
|
| 103 |
foreach ( $patterns as $pattern ) { |
| 104 |
preg_match_all( '/' . $pattern . '/', $content, $sources , PREG_SET_ORDER ); |
| 105 |
if ( !empty( $sources ) ) { |
| 106 |
foreach ( $sources as $source ) { |
| 107 |
// check if assetID if for third party libraries or not |
| 108 |
if ( !empty( $source[1] ) && strpos( $source[1], '@') === 0 ) { |
| 109 |
$assetIDs[] = $source[1]; |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
return $assetIDs; |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
/** |
| 120 |
* Get list of class which has specific elementId |
| 121 |
* |
| 122 |
* @param $hideOnSections |
| 123 |
* @param int $documentId |
| 124 |
* @param array $allSections |
| 125 |
* |
| 126 |
* @return string |
| 127 |
*/ |
| 128 |
public static function getVisibleSectionsCssIdList( $hideOnSections, $documentId, $allSections = [] ) { |
| 129 |
|
| 130 |
if ( ! empty( $hideOnSections ) ) { |
| 131 |
foreach ( $allSections as $key => $section ) { |
| 132 |
if ( in_array( $section, $hideOnSections ) ) { |
| 133 |
unset( $allSections[ $key ] ); |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
return implode( ',', Helper::sectionsSlugsListToCssIDsList( $allSections, $documentId ) ); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Get list of class which has specific elementId |
| 143 |
* |
| 144 |
* @param $hideOnSections |
| 145 |
* @param $documentId |
| 146 |
* |
| 147 |
* @return string |
| 148 |
*/ |
| 149 |
public static function getInvisibleSectionsCssIdList( $hideOnSections, $documentId ) { |
| 150 |
if( !empty( $hideOnSections ) ){ |
| 151 |
return implode( ',', Helper::sectionsSlugsListToCssIDsList( $hideOnSections, $documentId ) ); |
| 152 |
} |
| 153 |
return ''; |
| 154 |
} |
| 155 |
} |
| 156 |
|