skins.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. <?php
  2. namespace Elementor;
  3. if ( ! defined( 'ABSPATH' ) ) {
  4. exit; // Exit if accessed directly.
  5. }
  6. /**
  7. * Elementor skins manager.
  8. *
  9. * Elementor skins manager handler class is responsible for registering and
  10. * initializing all the supported skins.
  11. *
  12. * @since 1.0.0
  13. */
  14. class Skins_Manager {
  15. /**
  16. * Registered Skins.
  17. *
  18. * Holds the list of all the registered skins for all the widgets.
  19. *
  20. * @since 1.0.0
  21. * @access private
  22. *
  23. * @var array Registered skins.
  24. */
  25. private $_skins = [];
  26. /**
  27. * Add new skin.
  28. *
  29. * Register a single new skin for a widget.
  30. *
  31. * @since 1.0.0
  32. * @access public
  33. *
  34. * @param Widget_Base $widget Elementor widget.
  35. * @param Skin_Base $skin Elementor skin.
  36. *
  37. * @return true True if skin added.
  38. */
  39. public function add_skin( Widget_Base $widget, Skin_Base $skin ) {
  40. $widget_name = $widget->get_name();
  41. if ( ! isset( $this->_skins[ $widget_name ] ) ) {
  42. $this->_skins[ $widget_name ] = [];
  43. }
  44. $this->_skins[ $widget_name ][ $skin->get_id() ] = $skin;
  45. return true;
  46. }
  47. /**
  48. * Remove a skin.
  49. *
  50. * Unregister an existing skin from a widget.
  51. *
  52. * @since 1.0.0
  53. * @access public
  54. *
  55. * @param Widget_Base $widget Elementor widget.
  56. * @param string $skin_id Elementor skin ID.
  57. *
  58. * @return true|\WP_Error True if skin removed, `WP_Error` otherwise.
  59. */
  60. public function remove_skin( Widget_Base $widget, $skin_id ) {
  61. $widget_name = $widget->get_name();
  62. if ( ! isset( $this->_skins[ $widget_name ][ $skin_id ] ) ) {
  63. return new \WP_Error( 'Cannot remove not-exists skin.' );
  64. }
  65. unset( $this->_skins[ $widget_name ][ $skin_id ] );
  66. return true;
  67. }
  68. /**
  69. * Get skins.
  70. *
  71. * Retrieve all the skins assigned for a specific widget.
  72. *
  73. * @since 1.0.0
  74. * @access public
  75. *
  76. * @param Widget_Base $widget Elementor widget.
  77. *
  78. * @return false|array Skins if the widget has skins, False otherwise.
  79. */
  80. public function get_skins( Widget_Base $widget ) {
  81. $widget_name = $widget->get_name();
  82. if ( ! isset( $this->_skins[ $widget_name ] ) ) {
  83. return false;
  84. }
  85. return $this->_skins[ $widget_name ];
  86. }
  87. /**
  88. * Skins manager constructor.
  89. *
  90. * Initializing Elementor skins manager by requiring the skin base class.
  91. *
  92. * @since 1.0.0
  93. * @access public
  94. */
  95. public function __construct() {
  96. require ELEMENTOR_PATH . 'includes/base/skin-base.php';
  97. }
  98. }