| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: Services Section |
| 4 |
* Description: Use Services Section Block to provide services of your business to clients with customizable settings. |
| 5 |
* Version: 1.1.1 |
| 6 |
* Author: bPlugins LLC |
| 7 |
* Author URI: http://bplugins.com |
| 8 |
* License: GPLv3 |
| 9 |
* License URI: https://www.gnu.org/licenses/gpl-2.0.txt |
| 10 |
* Text Domain: services-section |
| 11 |
*/ |
| 12 |
|
| 13 |
// ABS PATH |
| 14 |
if ( !defined( 'ABSPATH' ) ) { exit; } |
| 15 |
|
| 16 |
// Constant |
| 17 |
define( 'SSB_PLUGIN_VERSION', '1.1.1' ); |
| 18 |
define( 'SSB_ASSETS_DIR', plugin_dir_url( __FILE__ ) . 'assets/' ); |
| 19 |
|
| 20 |
// Register Block Category |
| 21 |
function ssb_block_categories( $categories ) { |
| 22 |
return array_merge( array( array( |
| 23 |
'slug' => 'ssb', |
| 24 |
'title' => __( 'Services Section', 'services-section' ), |
| 25 |
) ), $categories ); |
| 26 |
} |
| 27 |
if ( version_compare( $GLOBALS['wp_version'], '5.8-alpha-1', '<' ) ) { |
| 28 |
add_filter( 'block_categories', 'ssb_block_categories', 10, 2 ); |
| 29 |
} else { |
| 30 |
add_filter( 'block_categories_all', 'ssb_block_categories', 10, 2 ); |
| 31 |
} |
| 32 |
|
| 33 |
// Register Blocks |
| 34 |
function ssb_wp_register_script( $block, $options = array() ) { |
| 35 |
register_block_type( 'services-section/' . $block, |
| 36 |
array_merge( array( |
| 37 |
'editor_script' => 'ssb_editor_script', |
| 38 |
'editor_style' => 'ssb_editor_style', |
| 39 |
'script' => 'ssb_script', |
| 40 |
'style' => 'ssb_style', |
| 41 |
), $options ) |
| 42 |
); |
| 43 |
} |
| 44 |
|
| 45 |
// Enqueue assets |
| 46 |
function ssb_enqueue_block_assets() { |
| 47 |
wp_enqueue_script( 'font-awesome-kit', SSB_ASSETS_DIR . 'js/font-awesome-kit.js', array(), SSB_PLUGIN_VERSION, true ); |
| 48 |
} |
| 49 |
add_action( 'enqueue_block_assets', 'ssb_enqueue_block_assets' ); |
| 50 |
|
| 51 |
function ssb_register() { |
| 52 |
// Resister script in editor |
| 53 |
wp_register_script( 'ssb_editor_script', plugins_url( 'dist/editor.js', __FILE__ ), array( 'wp-blocks', 'wp-element', 'wp-data', 'wp-i18n', 'wp-editor', 'wp-components', 'wp-blob', 'wp-html-entities', 'wp-compose', 'wp-rich-text' ), null, false ); |
| 54 |
// Register style in editor |
| 55 |
wp_register_style( 'ssb_editor_style', plugins_url( 'dist/editor.css', __FILE__ ), array( 'wp-edit-blocks'), null ); |
| 56 |
|
| 57 |
// Register script in frontend |
| 58 |
wp_register_script( 'ssb_script', plugins_url( 'dist/script.js', __FILE__ ), array( 'jquery'), null, true ); |
| 59 |
// Register style in frontend |
| 60 |
wp_register_style( 'ssb_style', plugins_url( 'dist/style.css', __FILE__ ), array( 'wp-editor' ), null ); |
| 61 |
|
| 62 |
// Register Blocks |
| 63 |
ssb_wp_register_script( 'services', array( 'render_callback' => 'render_ssb_services' ) ); |
| 64 |
ssb_wp_register_script( 'service', array( 'render_callback' => 'render_ssb_service' ) ); |
| 65 |
|
| 66 |
wp_set_script_translations( 'ssb_editor_script', 'services-section', plugin_dir_path( __FILE__ ) . 'languages' ); |
| 67 |
} |
| 68 |
add_action( 'init', 'ssb_register' ); |
| 69 |
|
| 70 |
// Generate Styles |
| 71 |
class SSBStyleGenerator { |
| 72 |
public static $styles = []; |
| 73 |
public static function addStyle($selector, $styles){ |
| 74 |
if(array_key_exists($selector, self::$styles)){ |
| 75 |
self::$styles[$selector] = wp_parse_args(self::$styles[$selector], $styles); |
| 76 |
}else { |
| 77 |
self::$styles[$selector] = $styles; |
| 78 |
} |
| 79 |
} |
| 80 |
public static function renderStyle(){ |
| 81 |
$output = ''; |
| 82 |
foreach(self::$styles as $selector => $style){ |
| 83 |
$new = ''; |
| 84 |
foreach($style as $property => $value){ |
| 85 |
if($value == ''){ $new .= $property; }else { $new .= " $property: $value;"; } |
| 86 |
} |
| 87 |
$output .= "$selector { $new }"; |
| 88 |
} |
| 89 |
return $output; |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
// Render Services |
| 94 |
function render_ssb_services($attributes, $content){ |
| 95 |
extract( $attributes ); |
| 96 |
|
| 97 |
$cId = $cId ?? ''; |
| 98 |
$columns = $columns ?? array( 'desktop' => 3, 'tablet' => 2, 'mobile' => 1 ); |
| 99 |
$columnGap = $columnGap ?? '30px'; |
| 100 |
$rowGap = $rowGap ?? '30px'; |
| 101 |
$textAlign = $textAlign ?? 'left'; |
| 102 |
$itemHeight = $itemHeight ?? '375px'; |
| 103 |
$itemPadding = $itemPadding ?? array('top' => '50px', 'right' => '30px', 'bottom' => '50px', 'left' => '50px'); |
| 104 |
$itemShadow = $itemShadow ?? array('blur' => '20px', 'color' => '#0000001a'); |
| 105 |
$itemBorder = $itemBorder ?? array('radius' => '15px'); |
| 106 |
$iconMargin = $iconMargin ?? array('bottom' => '30px'); |
| 107 |
$titleTypo = $titleTypo ?? array('fontSize' => 23); |
| 108 |
$titleMargin = $titleMargin ?? array('bottom' => '30px'); |
| 109 |
$descTypo = $descTypo ?? array('fontSize' => 16); |
| 110 |
|
| 111 |
// Generate Styles |
| 112 |
$servicesStyle = new SSBStyleGenerator(); |
| 113 |
$servicesStyle::addStyle("#ssbServices-$cId .ssbServices", array( |
| 114 |
'grid-gap' => "$rowGap $columnGap" |
| 115 |
)); |
| 116 |
$servicesStyle::addStyle("#ssbServices-$cId .ssbServices .ssbService", array( |
| 117 |
'text-align' => $textAlign, |
| 118 |
'min-height' => $itemHeight, |
| 119 |
'padding' => $itemPadding['styles'] ?? '50px 30px 50px 50px', |
| 120 |
'box-shadow' => $itemShadow['styles'] ?? '0 0 20px 0 #0000001a', |
| 121 |
$itemBorder['styles'] ?? 'border-radius: 15px;' => '' |
| 122 |
)); |
| 123 |
$servicesStyle::addStyle("#ssbServices-$cId .ssbServices .ssbService .bgLayer", array( |
| 124 |
'border-radius' => $itemBorder['radius'] ?? '15px' |
| 125 |
)); |
| 126 |
$servicesStyle::addStyle("#ssbServices-$cId .ssbServices .ssbService .icon", array( |
| 127 |
'margin' => $iconMargin['styles'] ?? '0 0 30px 0' |
| 128 |
)); |
| 129 |
$servicesStyle::addStyle("#ssbServices-$cId .ssbServices .ssbService .title", array( |
| 130 |
$titleTypo['styles'] ?? 'font-size: 23px' => '', |
| 131 |
'margin' => $titleMargin['styles'] ?? '0 0 30px 0' |
| 132 |
)); |
| 133 |
$servicesStyle::addStyle("#ssbServices-$cId .ssbServices .ssbService .description", array( |
| 134 |
$descTypo['styles'] ?? 'font-size: 16px' => '' |
| 135 |
)); |
| 136 |
|
| 137 |
$titleFontLink = $titleTypo['googleFontLink'] ?? ''; |
| 138 |
$descFontLink = $descTypo['googleFontLink'] ?? ''; |
| 139 |
|
| 140 |
$alignClass = isset($align) ? 'align' . $align : ''; |
| 141 |
|
| 142 |
ob_start(); // Echo the content |
| 143 |
echo "<div class='wp-block-services-section-services $alignClass' id='ssbServices-$cId'> |
| 144 |
<style>@import url($titleFontLink); @import url($descFontLink);". $servicesStyle::renderStyle() ."</style> |
| 145 |
|
| 146 |
<div class='ssbServices columns-".$columns['desktop']." columns-tablet-".$columns['tablet']." columns-mobile-".$columns['mobile']."'>". $content ."</div> |
| 147 |
</div>"; |
| 148 |
|
| 149 |
$servicesStyle::$styles = array(); // Empty before blocks styles in after blocks |
| 150 |
return ob_get_clean(); |
| 151 |
} |
| 152 |
|
| 153 |
// Render Service |
| 154 |
function render_ssb_service( $attributes ) { |
| 155 |
extract( $attributes ); |
| 156 |
|
| 157 |
$cId = $cId ?? ''; |
| 158 |
$background = $background ?? array('type' => 'image', 'image' => array('url' => plugins_url( 'dist/src/img/service-pattern.png', __FILE__ )), 'overlayColor' => '#0000', 'position' => 'bottom left', 'size' => 'auto'); |
| 159 |
$isIcon = $isIcon ?? true; |
| 160 |
$isUpIcon = $isUpIcon ?? false; |
| 161 |
$icon = $icon ?? array('class' => 'fa fa-wordpress', 'fontSize' => 70); |
| 162 |
$upIcon = $upIcon ?? array('id' => NULL, 'url' => '', 'alt' => '', 'title' => ''); |
| 163 |
$iconWidth = $iconWidth ?? '70px'; |
| 164 |
$isTitle = $isTitle ?? true; |
| 165 |
$title = $title ?? 'Service title'; |
| 166 |
$titleColor = $titleColor ?? '#222'; |
| 167 |
$isDesc = $isDesc ?? true; |
| 168 |
$desc = $desc ?? 'Lorem ipsum dolor sit, amet consectetur adipisicing elit. Accusamus, fuga.'; |
| 169 |
$descColor = $descColor ?? '#333'; |
| 170 |
$isLink = $isLink ?? true; |
| 171 |
$link = $link ?? '#'; |
| 172 |
$linkBGColor = $linkBGColor ?? '#d2d2d2'; |
| 173 |
$linkBGHovColor = $linkBGHovColor ?? '#fe6601'; |
| 174 |
$linkIconColor = $linkIconColor ?? '#fff'; |
| 175 |
|
| 176 |
// Generate Styles |
| 177 |
$serviceStyle = new SSBStyleGenerator(); |
| 178 |
$serviceStyle::addStyle("#ssbService-$cId .ssbService .bgLayer", array( |
| 179 |
$background['styles'] ?? 'background-image: url('. plugins_url( 'dist/src/img/service-pattern.png', __FILE__ ) .'); background-position: bottom left; background-size: auto; background-repeat: no-repeat; background-color: #0000;' => '' |
| 180 |
)); |
| 181 |
$serviceStyle::addStyle("#ssbService-$cId .ssbService .icon i", array( |
| 182 |
$icon['styles'] ?? 'font-size: 70px;' => '' |
| 183 |
)); |
| 184 |
$serviceStyle::addStyle("#ssbService-$cId .ssbService .icon img", array( |
| 185 |
'width' => $iconWidth |
| 186 |
)); |
| 187 |
$serviceStyle::addStyle("#ssbService-$cId .ssbService .title", array( |
| 188 |
'color' => $titleColor |
| 189 |
)); |
| 190 |
$serviceStyle::addStyle("#ssbService-$cId .ssbService .description", array( |
| 191 |
'color' => $descColor |
| 192 |
)); |
| 193 |
$serviceStyle::addStyle("#ssbService-$cId .ssbService .link a", array( |
| 194 |
'background-color' => $linkBGColor |
| 195 |
)); |
| 196 |
$serviceStyle::addStyle("#ssbService-$cId:hover .ssbService .link a", array( |
| 197 |
'background-color' => $linkBGHovColor |
| 198 |
)); |
| 199 |
$serviceStyle::addStyle("#ssbService-$cId .ssbService .link a svg", array( |
| 200 |
'fill' => $linkIconColor |
| 201 |
)); |
| 202 |
|
| 203 |
// Components |
| 204 |
$iconLibEl = isset($icon['class']) ? "<i class='".$icon['class']."'></i>" : ''; |
| 205 |
$iconUpEl = isset($upIcon['url']) ? "<img src='".$upIcon['url']."' alt='".$upIcon['alt']."' />" : ''; |
| 206 |
$iconEl = $isUpIcon ? $iconUpEl : $iconLibEl; |
| 207 |
$iconWrapEl = $isIcon ? "<div class='icon'>$iconEl</div>" : ''; |
| 208 |
$titleEl = $isTitle ? "<h3 class='title'>$title</h3>" : ''; |
| 209 |
$descEl = $isDesc ? "<p class='description'>$desc</p>" : ''; |
| 210 |
$linkEl = $isLink ? "<div class='link'><a href='$link'> |
| 211 |
<svg xmlns='http://www.w3.org/2000/svg' width='24' height='24' viewBox='0 0 493.356 493.356'> |
| 212 |
<path d='M490.498,239.278l-109.632-99.929c-3.046-2.474-6.376-2.95-9.993-1.427c-3.613,1.525-5.427,4.283-5.427,8.282v63.954H9.136 c-2.666,0-4.856,0.855-6.567,2.568C0.859,214.438,0,216.628,0,219.292v54.816c0,2.663,0.855,4.853,2.568,6.563 c1.715,1.712,3.905,2.567,6.567,2.567h356.313v63.953c0,3.812,1.817,6.57,5.428,8.278c3.62,1.529,6.95,0.951,9.996-1.708 l109.632-101.077c1.903-1.902,2.852-4.182,2.852-6.849C493.356,243.367,492.401,241.181,490.498,239.278z' /> |
| 213 |
</svg> |
| 214 |
</a></div>" : ''; |
| 215 |
|
| 216 |
ob_start(); // Echo the content |
| 217 |
echo "<div class='wp-block-services-section-service' id='ssbService-$cId'> |
| 218 |
<style>". $serviceStyle::renderStyle() ."</style> |
| 219 |
|
| 220 |
<div class='ssbService wow flipInY animated animated' data-wow-delay='300ms' data-wow-duration='1500ms'> |
| 221 |
<div class='bgLayer'></div> |
| 222 |
$iconWrapEl $titleEl $descEl $linkEl |
| 223 |
</div> |
| 224 |
</div>"; |
| 225 |
|
| 226 |
$serviceStyle::$styles = array(); // Empty before blocks styles in after blocks |
| 227 |
return ob_get_clean(); |
| 228 |
} |