class-vc-vendor-presets.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. die( '-1' );
  4. }
  5. /**
  6. * Singleton to hold all vendor presets
  7. *
  8. * @since 4.8
  9. */
  10. class Vc_Vendor_Preset {
  11. private static $instance;
  12. private static $presets = array();
  13. /**
  14. * @return \Vc_Vendor_Preset
  15. */
  16. public static function getInstance() {
  17. if ( ! self::$instance ) {
  18. self::$instance = new self();
  19. }
  20. return self::$instance;
  21. }
  22. protected function __construct() {
  23. }
  24. /**
  25. * Add vendor preset to collection
  26. *
  27. * @param string $title
  28. * @param string $shortcode
  29. * @param array $params
  30. * @param bool $default
  31. *
  32. * @return bool
  33. * @since 4.8
  34. *
  35. */
  36. public function add( $title, $shortcode, $params, $default = false ) {
  37. if ( ! $title || ! is_string( $title ) || ! $shortcode || ! is_string( $shortcode ) || ! $params || ! is_array( $params ) ) {
  38. return false;
  39. }
  40. $preset = array(
  41. 'shortcode' => $shortcode,
  42. 'default' => $default,
  43. 'params' => $params,
  44. 'title' => $title,
  45. );
  46. // @codingStandardsIgnoreLine
  47. $id = md5( serialize( $preset ) );
  48. self::$presets[ $id ] = $preset;
  49. return true;
  50. }
  51. /**
  52. * Get specific vendor preset
  53. *
  54. * @param string $id
  55. *
  56. * @return mixed array|false
  57. * @since 4.8
  58. *
  59. */
  60. public function get( $id ) {
  61. if ( isset( self::$presets[ $id ] ) ) {
  62. return self::$presets[ $id ];
  63. }
  64. return false;
  65. }
  66. /**
  67. * Get all vendor presets for specific shortcode
  68. *
  69. * @param string $shortcode
  70. *
  71. * @return array
  72. * @since 4.8
  73. *
  74. */
  75. public function getAll( $shortcode ) {
  76. $list = array();
  77. foreach ( self::$presets as $id => $preset ) {
  78. if ( $shortcode === $preset['shortcode'] ) {
  79. $list[ $id ] = $preset;
  80. }
  81. }
  82. return $list;
  83. }
  84. /**
  85. * Get all default vendor presets
  86. *
  87. * Include only one default preset per shortcode
  88. *
  89. * @return array
  90. * @since 4.8
  91. *
  92. */
  93. public function getDefaults() {
  94. $list = array();
  95. $added = array();
  96. foreach ( self::$presets as $id => $preset ) {
  97. if ( $preset['default'] && ! in_array( $preset['shortcode'], $added, true ) ) {
  98. $added[] = $preset['shortcode'];
  99. $list[ $id ] = $preset;
  100. }
  101. }
  102. return $list;
  103. }
  104. /**
  105. * Get ID of default preset for specific shortcode
  106. *
  107. * If multiple presets are default, return first
  108. *
  109. * @param string $shortcode
  110. *
  111. * @return string|null
  112. * @since 4.8
  113. *
  114. */
  115. public function getDefaultId( $shortcode ) {
  116. foreach ( self::$presets as $id => $preset ) {
  117. if ( $shortcode === $preset['shortcode'] && $preset['default'] ) {
  118. return $id;
  119. }
  120. }
  121. return null;
  122. }
  123. }