class-fl-builder-module.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <?php
  2. /**
  3. * Base class that gets extended by all module classes.
  4. *
  5. * @since 1.0
  6. */
  7. class FLBuilderModule {
  8. /**
  9. * A unique ID for the module.
  10. *
  11. * @since 1.0
  12. * @var string $node
  13. */
  14. public $node;
  15. /**
  16. * A unique ID for the module's parent.
  17. *
  18. * @since 1.0
  19. * @var int $parent
  20. */
  21. public $parent;
  22. /**
  23. * The sort order for this module.
  24. *
  25. * @since 1.0
  26. * @var int $position
  27. */
  28. public $position;
  29. /**
  30. * A display name for the module.
  31. *
  32. * @since 1.0
  33. * @var string $name
  34. */
  35. public $name;
  36. /**
  37. * A description to display for the module.
  38. *
  39. * @since 1.0
  40. * @var string $description
  41. */
  42. public $description;
  43. /**
  44. * The category this module belongs to.
  45. *
  46. * @since 1.0
  47. * @var string $category
  48. */
  49. public $category;
  50. /**
  51. * Must be the module's folder name.
  52. *
  53. * @since 1.0
  54. * @var string $slug
  55. */
  56. public $slug;
  57. /**
  58. * The module's directory path.
  59. *
  60. * @since 1.0
  61. * @var string $dir
  62. */
  63. public $dir;
  64. /**
  65. * The module's directory url.
  66. *
  67. * @since 1.0
  68. * @var string $url
  69. */
  70. public $url;
  71. /**
  72. * An array of form settings.
  73. *
  74. * @since 1.0
  75. * @var array $form
  76. */
  77. public $form = array();
  78. /**
  79. * Whether this module is enabled on the
  80. * frontend or not.
  81. *
  82. * @since 1.0
  83. * @var boolean $enabled
  84. */
  85. public $enabled = true;
  86. /**
  87. * Whether this module's content should
  88. * be exported to the WP editor or not.
  89. *
  90. * @since 1.0
  91. * @var boolean $editor_export
  92. */
  93. public $editor_export = true;
  94. /**
  95. * Whether partial refresh should be enabled
  96. * for this module or not.
  97. *
  98. * @since 1.7
  99. * @var boolean $partial_refresh
  100. */
  101. public $partial_refresh = false;
  102. /**
  103. * The module settings object.
  104. *
  105. * @since 1.0
  106. * @var object $settings
  107. */
  108. public $settings;
  109. /**
  110. * Additional CSS to enqueue.
  111. *
  112. * @since 1.0
  113. * @var array $css
  114. */
  115. public $css = array();
  116. /**
  117. * Additional JS to enqueue.
  118. *
  119. * @since 1.0
  120. * @var array $js
  121. */
  122. public $js = array();
  123. /**
  124. * The class of the font icon for this module.
  125. *
  126. * @since 2.0
  127. */
  128. public $icon = '';
  129. /**
  130. * Module constructor.
  131. *
  132. * @since 1.0
  133. */
  134. public function __construct( $params ) {
  135. $class_info = new ReflectionClass( $this );
  136. $class_path = $class_info->getFileName();
  137. $dir_path = dirname( $class_path );
  138. $this->slug = basename( $class_path, '.php' );
  139. $this->enabled = isset( $params['enabled'] ) ? $params['enabled'] : true;
  140. $this->editor_export = isset( $params['editor_export'] ) ? $params['editor_export'] : true;
  141. $this->partial_refresh = isset( $params['partial_refresh'] ) ? $params['partial_refresh'] : false;
  142. // We need to normalize the paths here since path comparisons
  143. // break on Windows because they use backslashes.
  144. $abspath = str_replace( '\\', '/', ABSPATH );
  145. $fl_builder_dir = str_replace( '\\', '/', FL_BUILDER_DIR );
  146. $dir_path = str_replace( '\\', '/', $dir_path );
  147. $stylesheet_directory = str_replace( '\\', '/', get_stylesheet_directory() );
  148. $stylesheet_directory_uri = str_replace( '\\', '/', get_stylesheet_directory_uri() );
  149. $template_directory = str_replace( '\\', '/', get_template_directory() );
  150. $template_directory_uri = str_replace( '\\', '/', get_template_directory_uri() );
  151. // Find the right paths.
  152. if ( is_child_theme() && stristr( $dir_path, $stylesheet_directory ) ) {
  153. $this->url = trailingslashit( str_replace( $stylesheet_directory, $stylesheet_directory_uri, $dir_path ) );
  154. $this->dir = trailingslashit( $dir_path );
  155. } elseif ( stristr( $dir_path, $template_directory ) ) {
  156. $this->url = trailingslashit( str_replace( $template_directory, $template_directory_uri, $dir_path ) );
  157. $this->dir = trailingslashit( $dir_path );
  158. } elseif ( isset( $params['url'] ) && isset( $params['dir'] ) ) {
  159. $this->url = trailingslashit( $params['url'] );
  160. $this->dir = trailingslashit( $params['dir'] );
  161. } elseif ( ! stristr( $dir_path, $fl_builder_dir ) ) {
  162. $this->url = trailingslashit( str_replace( trailingslashit( $abspath ), trailingslashit( home_url() ), $dir_path ) );
  163. $this->dir = trailingslashit( $dir_path );
  164. } else {
  165. $this->url = trailingslashit( FL_BUILDER_URL . 'modules/' . $this->slug );
  166. $this->dir = trailingslashit( FL_BUILDER_DIR . 'modules/' . $this->slug );
  167. }
  168. // Icon requires dir be defined before calling get_icon()
  169. $this->icon = isset( $params['icon'] ) ? $this->get_icon( $params['icon'] ) : $this->get_icon();
  170. $details = apply_filters( 'fl_builder_module_details', array(
  171. 'name' => $params['name'],
  172. 'description' => $params['description'],
  173. 'category' => $this->normalize_category_name( $params['category'] ),
  174. 'group' => isset( $params['group'] ) ? $params['group'] : false,
  175. 'icon' => $this->icon,
  176. ), $this->slug );
  177. $this->name = $details['name'];
  178. $this->description = $details['description'];
  179. $this->category = $details['category'];
  180. $this->group = $details['group'];
  181. $this->icon = $details['icon'];
  182. }
  183. /**
  184. * Used to enqueue additional frontend styles. Do not enqueue
  185. * frontend.css or frontend.responsive.css as those will be
  186. * enqueued automatically. Params are the same as those used in
  187. * WordPress' wp_enqueue_style function.
  188. *
  189. * @since 1.0
  190. * @param string $handle
  191. * @param string $src
  192. * @param array $deps
  193. * @param string $ver
  194. * @param string $media
  195. * @return void
  196. */
  197. public function add_css( $handle, $src = null, $deps = null, $ver = null, $media = null ) {
  198. $this->css[ $handle ] = array( $src, $deps, $ver, $media );
  199. }
  200. /**
  201. * Used to enqueue additional frontend scripts. Do not enqueue
  202. * frontend.js as that will be enqueued automatically. Params
  203. * are the same as those used in WordPress' wp_enqueue_script function.
  204. *
  205. * @since 1.0
  206. * @param string $handle
  207. * @param string $src
  208. * @param array $deps
  209. * @param string $ver
  210. * @param bool $in_footer
  211. * @return void
  212. */
  213. public function add_js( $handle, $src = null, $deps = null, $ver = null, $in_footer = null ) {
  214. $this->js[ $handle ] = array( $src, $deps, $ver, $in_footer );
  215. }
  216. /**
  217. * Enqueues the needed styles for any icon fields
  218. * in this module.
  219. *
  220. * @since 1.4.6
  221. * @return void
  222. */
  223. public function enqueue_icon_styles() {
  224. FLBuilderIcons::enqueue_styles_for_module( $this );
  225. }
  226. /**
  227. * Enqueues the needed styles for any font fields
  228. * in this module.
  229. *
  230. * @since 1.6.3
  231. * @return void
  232. */
  233. public function enqueue_font_styles() {
  234. FLBuilderFonts::add_fonts_for_module( $this );
  235. }
  236. /**
  237. * Should be overridden by subclasses to enqueue
  238. * additional css/js using the add_css and add_js methods.
  239. *
  240. * @since 1.0
  241. * @return void
  242. */
  243. public function enqueue_scripts() {
  244. }
  245. /**
  246. * Should be overridden by subclasses to
  247. * work with settings data before it is saved.
  248. *
  249. * @since 1.0
  250. * @param object A settings object that is going to be saved.
  251. * @return object
  252. */
  253. public function update( $settings ) {
  254. return $settings;
  255. }
  256. /**
  257. * Should be overridden by subclasses to work with a module before
  258. * it is deleted. Please note, this method is called when a module
  259. * is updated and when it's actually removed from the page and should
  260. * be used for things like clearing photo cache from the builder's
  261. * cache directory. If only need to run logic when a module is
  262. * actually removed from the page, use the remove method instead.
  263. *
  264. * @since 1.0
  265. * @return void
  266. */
  267. public function delete() {
  268. }
  269. /**
  270. * Should be overridden by subclasses to work with a module when
  271. * it is actually removed from the page.
  272. *
  273. * @since 1.0
  274. * @return void
  275. */
  276. public function remove() {
  277. }
  278. /**
  279. * Get svg icon string
  280. *
  281. * @since 2.0
  282. * @return String
  283. */
  284. public function get_icon( $icon = '' ) {
  285. // check if $icon is referencing an included icon.
  286. if ( '' != $icon && file_exists( FL_BUILDER_DIR . 'img/svg/' . $icon ) ) {
  287. $path = FL_BUILDER_DIR . 'img/svg/' . $icon;
  288. // check if module directory includes an icon.svg file
  289. } elseif ( file_exists( $this->dir . 'icon.svg' ) ) {
  290. $path = $this->dir . 'icon.svg';
  291. // default to included icon
  292. } else {
  293. $path = FL_BUILDER_DIR . 'img/svg/insert.svg';
  294. }
  295. if ( file_exists( $path ) ) {
  296. return file_get_contents( $path );
  297. } else {
  298. return '';
  299. }
  300. }
  301. /**
  302. * Normalizes category names to support 2.0 since the default
  303. * category names changed.
  304. *
  305. * @since 2.0
  306. * @access private
  307. * @param string $cat
  308. * @return string
  309. */
  310. private function normalize_category_name( $cat ) {
  311. if ( __( 'Basic Modules', 'fl-builder' ) === $cat ) {
  312. $cat = __( 'Basic', 'fl-builder' );
  313. } elseif ( __( 'Advanced Modules', 'fl-builder' ) === $cat ) {
  314. $cat = __( 'Advanced', 'fl-builder' );
  315. }
  316. return $cat;
  317. }
  318. /**
  319. * Get the default svg icon
  320. *
  321. * @since 2.0
  322. * @return String
  323. */
  324. static public function get_default_icon() {
  325. $path = FL_BUILDER_DIR . 'img/svg/insert.svg';
  326. return file_get_contents( $path );
  327. }
  328. /**
  329. * Get the widget icon
  330. *
  331. * @since 2.0
  332. * @return String
  333. */
  334. static public function get_widget_icon() {
  335. $path = FL_BUILDER_DIR . 'img/svg/wordpress-alt.svg';
  336. return file_get_contents( $path );
  337. }
  338. }