PageRenderTime 40ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/elementor/core/kits/manager.php

https://gitlab.com/campus-academy/krowkaramel
PHP | 435 lines | 273 code | 82 blank | 80 comment | 29 complexity | 98acc90188de1611c4c1ae8853a7ce70 MD5 | raw file
  1. <?php
  2. namespace Elementor\Core\Kits;
  3. use Elementor\Core\Base\Document;
  4. use Elementor\Core\Kits\Controls\Repeater;
  5. use Elementor\Core\Kits\Documents\Tabs\Global_Colors;
  6. use Elementor\Core\Kits\Documents\Tabs\Global_Typography;
  7. use Elementor\Plugin;
  8. use Elementor\Core\Files\CSS\Post as Post_CSS;
  9. use Elementor\Core\Files\CSS\Post_Preview as Post_Preview;
  10. use Elementor\Core\Documents_Manager;
  11. use Elementor\Core\Kits\Documents\Kit;
  12. use Elementor\TemplateLibrary\Source_Local;
  13. use Elementor\Utils;
  14. if ( ! defined( 'ABSPATH' ) ) {
  15. exit; // Exit if accessed directly.
  16. }
  17. class Manager {
  18. const OPTION_ACTIVE = 'elementor_active_kit';
  19. const OPTION_PREVIOUS = 'elementor_previous_kit';
  20. const E_HASH_COMMAND_OPEN_SITE_SETTINGS = 'e:run:panel/global/open';
  21. public function get_active_id() {
  22. return get_option( self::OPTION_ACTIVE );
  23. }
  24. public function get_previous_id() {
  25. return get_option( self::OPTION_PREVIOUS );
  26. }
  27. public function get_active_kit() {
  28. $kit = Plugin::$instance->documents->get( $this->get_active_id() );
  29. if ( ! $this->is_valid_kit( $kit ) ) {
  30. return $this->get_empty_kit_instance();
  31. }
  32. return $kit;
  33. }
  34. public function get_active_kit_for_frontend() {
  35. $kit = Plugin::$instance->documents->get_doc_for_frontend( $this->get_active_id() );
  36. if ( ! $this->is_valid_kit( $kit ) ) {
  37. return $this->get_empty_kit_instance();
  38. }
  39. return $kit;
  40. }
  41. /**
  42. * @param $kit
  43. *
  44. * @return bool
  45. */
  46. private function is_valid_kit( $kit ) {
  47. return $kit && $kit instanceof Kit && 'trash' !== $kit->get_main_post()->post_status;
  48. }
  49. /**
  50. * Returns an empty kit for situation when there is no kit in the site.
  51. *
  52. * @return Kit
  53. * @throws \Exception
  54. */
  55. private function get_empty_kit_instance() {
  56. return new Kit( [
  57. 'settings' => [],
  58. 'post_id' => 0,
  59. ] );
  60. }
  61. /**
  62. * Checks if specific post is a kit.
  63. *
  64. * @param $post_id
  65. *
  66. * @return bool
  67. */
  68. public function is_kit( $post_id ) {
  69. $document = Plugin::$instance->documents->get( $post_id );
  70. return $document && $document instanceof Kit && ! $document->is_revision();
  71. }
  72. /**
  73. * Init kit controls.
  74. *
  75. * A temp solution in order to avoid init kit group control from within another group control.
  76. *
  77. * After moving the `default_font` to the kit, the Typography group control cause initialize the kit controls at: https://github.com/elementor/elementor/blob/e6e1db9eddef7e3c1a5b2ba0c2338e2af2a3bfe3/includes/controls/groups/typography.php#L91
  78. * and because the group control is a singleton, its args are changed to the last kit group control.
  79. */
  80. public function init_kit_controls() {
  81. $this->get_active_kit_for_frontend()->get_settings();
  82. }
  83. public function get_current_settings( $setting = null ) {
  84. $kit = $this->get_active_kit_for_frontend();
  85. if ( ! $kit ) {
  86. return '';
  87. }
  88. return $kit->get_settings( $setting );
  89. }
  90. public function create( array $kit_data = [], array $kit_meta_data = [] ) {
  91. $default_kit_data = [
  92. 'post_status' => 'publish',
  93. ];
  94. $kit_data = array_merge( $default_kit_data, $kit_data );
  95. $kit_data['post_type'] = Source_Local::CPT;
  96. $kit = Plugin::$instance->documents->create( 'kit', $kit_data, $kit_meta_data );
  97. if ( isset( $kit_data['settings'] ) ) {
  98. $kit->save( [ 'settings' => $kit_data['settings'] ] );
  99. }
  100. return $kit->get_id();
  101. }
  102. public function create_new_kit( $kit_name = '', $settings = [], $active = true ) {
  103. $kit_name = $kit_name ? $kit_name : esc_html__( 'Custom', 'elementor' );
  104. $id = $this->create( [
  105. 'post_title' => $kit_name,
  106. 'settings' => $settings,
  107. ] );
  108. if ( $active ) {
  109. update_option( self::OPTION_PREVIOUS, $this->get_active_id() );
  110. update_option( self::OPTION_ACTIVE, $id );
  111. }
  112. return $id;
  113. }
  114. public function create_default() {
  115. return $this->create( [
  116. 'post_title' => esc_html__( 'Default Kit', 'elementor' ),
  117. ] );
  118. }
  119. /**
  120. * Create a default kit if needed.
  121. *
  122. * This action runs on activation hook, all the Plugin components do not exists and
  123. * the Document manager and Kits manager instances cannot be used.
  124. *
  125. * @return int|void|\WP_Error
  126. */
  127. public static function create_default_kit() {
  128. if ( get_option( self::OPTION_ACTIVE ) ) {
  129. return;
  130. }
  131. $id = wp_insert_post( [
  132. 'post_title' => __( 'Default Kit', 'elementor' ),
  133. 'post_type' => Source_Local::CPT,
  134. 'post_status' => 'publish',
  135. 'meta_input' => [
  136. '_elementor_edit_mode' => 'builder',
  137. Document::TYPE_META_KEY => 'kit',
  138. ],
  139. ] );
  140. update_option( self::OPTION_ACTIVE, $id );
  141. return $id;
  142. }
  143. /**
  144. * @param Documents_Manager $documents_manager
  145. */
  146. public function register_document( $documents_manager ) {
  147. $documents_manager->register_document_type( 'kit', Kit::get_class_full_name() );
  148. }
  149. public function localize_settings( $settings ) {
  150. $kit = $this->get_active_kit();
  151. $kit_controls = $kit->get_controls();
  152. $design_system_controls = [
  153. 'colors' => $kit_controls['system_colors']['fields'],
  154. 'typography' => $kit_controls['system_typography']['fields'],
  155. ];
  156. $settings = array_replace_recursive( $settings, [
  157. 'kit_id' => $kit->get_main_id(),
  158. 'kit_config' => [
  159. 'typography_prefix' => Global_Typography::TYPOGRAPHY_GROUP_PREFIX,
  160. 'design_system_controls' => $design_system_controls,
  161. ],
  162. 'user' => [
  163. 'can_edit_kit' => $kit->is_editable_by_current_user(),
  164. ],
  165. ] );
  166. return $settings;
  167. }
  168. public function preview_enqueue_styles() {
  169. $kit = $this->get_kit_for_frontend();
  170. if ( $kit ) {
  171. // On preview, the global style is not enqueued.
  172. $this->frontend_before_enqueue_styles();
  173. Plugin::$instance->frontend->print_fonts_links();
  174. }
  175. }
  176. public function frontend_before_enqueue_styles() {
  177. $kit = $this->get_kit_for_frontend();
  178. if ( $kit ) {
  179. if ( $kit->is_autosave() ) {
  180. $css_file = Post_Preview::create( $kit->get_id() );
  181. } else {
  182. $css_file = Post_CSS::create( $kit->get_id() );
  183. }
  184. $css_file->enqueue();
  185. }
  186. }
  187. public function render_panel_html() {
  188. require __DIR__ . '/views/panel.php';
  189. }
  190. public function get_kit_for_frontend() {
  191. $kit = false;
  192. $active_kit = $this->get_active_kit();
  193. $is_kit_preview = is_preview() && isset( $_GET['preview_id'] ) && $active_kit->get_main_id() === (int) $_GET['preview_id'];
  194. if ( $is_kit_preview ) {
  195. $kit = Plugin::$instance->documents->get_doc_or_auto_save( $active_kit->get_main_id(), get_current_user_id() );
  196. } elseif ( 'publish' === $active_kit->get_main_post()->post_status ) {
  197. $kit = $active_kit;
  198. }
  199. return $kit;
  200. }
  201. public function update_kit_settings_based_on_option( $key, $value ) {
  202. /** @var Kit $active_kit */
  203. $active_kit = $this->get_active_kit();
  204. if ( $active_kit->is_saving() ) {
  205. return;
  206. }
  207. $active_kit->update_settings( [ $key => $value ] );
  208. }
  209. /**
  210. * Map Scheme To Global
  211. *
  212. * Convert a given scheme value to its corresponding default global value
  213. *
  214. * @param string $type 'color'/'typography'
  215. * @param $value
  216. * @return mixed
  217. */
  218. private function map_scheme_to_global( $type, $value ) {
  219. $schemes_to_globals_map = [
  220. 'color' => [
  221. '1' => Global_Colors::COLOR_PRIMARY,
  222. '2' => Global_Colors::COLOR_SECONDARY,
  223. '3' => Global_Colors::COLOR_TEXT,
  224. '4' => Global_Colors::COLOR_ACCENT,
  225. ],
  226. 'typography' => [
  227. '1' => Global_Typography::TYPOGRAPHY_PRIMARY,
  228. '2' => Global_Typography::TYPOGRAPHY_SECONDARY,
  229. '3' => Global_Typography::TYPOGRAPHY_TEXT,
  230. '4' => Global_Typography::TYPOGRAPHY_ACCENT,
  231. ],
  232. ];
  233. return $schemes_to_globals_map[ $type ][ $value ];
  234. }
  235. /**
  236. * Convert Scheme to Default Global
  237. *
  238. * If a control has a scheme property, convert it to a default Global.
  239. *
  240. * @param $scheme - Control scheme property
  241. * @return array - Control/group control args
  242. * @since 3.0.0
  243. * @access public
  244. */
  245. public function convert_scheme_to_global( $scheme ) {
  246. if ( isset( $scheme['type'] ) && isset( $scheme['value'] ) ) {
  247. //_deprecated_argument( $args['scheme'], '3.0.0', 'Schemes are now deprecated - use $args[\'global\'] instead.' );
  248. return $this->map_scheme_to_global( $scheme['type'], $scheme['value'] );
  249. }
  250. // Typography control 'scheme' properties usually only include the string with the typography value ('1'-'4').
  251. return $this->map_scheme_to_global( 'typography', $scheme );
  252. }
  253. public function register_controls() {
  254. $controls_manager = Plugin::$instance->controls_manager;
  255. $controls_manager->register( new Repeater() );
  256. }
  257. public function is_custom_colors_enabled() {
  258. return ! get_option( 'elementor_disable_color_schemes' );
  259. }
  260. public function is_custom_typography_enabled() {
  261. return ! get_option( 'elementor_disable_typography_schemes' );
  262. }
  263. /**
  264. * Add kit wrapper body class.
  265. *
  266. * It should be added even for non Elementor pages,
  267. * in order to support embedded templates.
  268. */
  269. private function add_body_class() {
  270. $kit = $this->get_kit_for_frontend();
  271. if ( $kit ) {
  272. Plugin::$instance->frontend->add_body_class( 'elementor-kit-' . $kit->get_main_id() );
  273. }
  274. }
  275. /**
  276. * Send a confirm message before move a kit to trash, or if delete permanently not for trash.
  277. *
  278. * @param $post_id
  279. * @param false $is_permanently_delete
  280. */
  281. private function before_delete_kit( $post_id, $is_permanently_delete = false ) {
  282. $document = Plugin::$instance->documents->get( $post_id );
  283. if (
  284. ! $document ||
  285. ! $this->is_kit( $post_id ) ||
  286. isset( $_GET['force_delete_kit'] ) || // phpcs:ignore -- nonce validation is not require here.
  287. ( $is_permanently_delete && $document->is_trash() )
  288. ) {
  289. return;
  290. }
  291. ob_start();
  292. require __DIR__ . '/views/trash-kit-confirmation.php';
  293. $confirmation_content = ob_get_clean();
  294. // PHPCS - the content does not contain user input value.
  295. wp_die( new \WP_Error( 'cant_delete_kit', $confirmation_content ) ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
  296. }
  297. /**
  298. * Add 'Edit with elementor -> Site Settings' in admin bar.
  299. *
  300. * @param [] $admin_bar_config
  301. *
  302. * @return array $admin_bar_config
  303. */
  304. private function add_menu_in_admin_bar( $admin_bar_config ) {
  305. $document = Plugin::$instance->documents->get( get_the_ID() );
  306. if ( ! $document || ! $document->is_built_with_elementor() ) {
  307. $recent_edited_post = Utils::get_recently_edited_posts_query( [
  308. 'posts_per_page' => 1,
  309. ] );
  310. if ( $recent_edited_post->post_count ) {
  311. $posts = $recent_edited_post->get_posts();
  312. $document = Plugin::$instance->documents->get( reset( $posts )->ID );
  313. }
  314. }
  315. if ( $document ) {
  316. $admin_bar_config['elementor_edit_page']['children'][] = [
  317. 'id' => 'elementor_site_settings',
  318. 'title' => esc_html__( 'Site Settings', 'elementor' ),
  319. 'sub_title' => esc_html__( 'Site', 'elementor' ),
  320. 'href' => $document->get_edit_url() . '#' . self::E_HASH_COMMAND_OPEN_SITE_SETTINGS,
  321. 'class' => 'elementor-site-settings',
  322. 'parent_class' => 'elementor-second-section',
  323. ];
  324. }
  325. return $admin_bar_config;
  326. }
  327. public function __construct() {
  328. add_action( 'elementor/documents/register', [ $this, 'register_document' ] );
  329. add_filter( 'elementor/editor/localize_settings', [ $this, 'localize_settings' ] );
  330. add_filter( 'elementor/editor/footer', [ $this, 'render_panel_html' ] );
  331. add_action( 'elementor/frontend/after_enqueue_styles', [ $this, 'frontend_before_enqueue_styles' ], 0 );
  332. add_action( 'elementor/preview/enqueue_styles', [ $this, 'preview_enqueue_styles' ], 0 );
  333. add_action( 'elementor/controls/register', [ $this, 'register_controls' ] );
  334. add_action( 'wp_trash_post', function ( $post_id ) {
  335. $this->before_delete_kit( $post_id );
  336. } );
  337. add_action( 'before_delete_post', function ( $post_id ) {
  338. $this->before_delete_kit( $post_id, true );
  339. } );
  340. add_action( 'update_option_blogname', function ( $old_value, $value ) {
  341. $this->update_kit_settings_based_on_option( 'site_name', $value );
  342. }, 10, 2 );
  343. add_action( 'update_option_blogdescription', function ( $old_value, $value ) {
  344. $this->update_kit_settings_based_on_option( 'site_description', $value );
  345. }, 10, 2 );
  346. add_action( 'wp_head', function() {
  347. $this->add_body_class();
  348. } );
  349. add_filter( 'elementor/frontend/admin_bar/settings', function ( $admin_bar_config ) {
  350. return $this->add_menu_in_admin_bar( $admin_bar_config );
  351. }, 9 /* Before site-editor (theme-builder) */ );
  352. }
  353. }