PageRenderTime 65ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-content/plugins/so-widgets-bundle/base/inc/fields/repeater.class.php

https://gitlab.com/mostafame/team_website
PHP | 128 lines | 81 code | 7 blank | 40 comment | 9 complexity | a246bd9aa4baa6f810c53d8a65f707c4 MD5 | raw file
  1. <?php
  2. /**
  3. * Class SiteOrigin_Widget_Field_Repeater
  4. */
  5. class SiteOrigin_Widget_Field_Repeater extends SiteOrigin_Widget_Field_Container_Base {
  6. /**
  7. * A default label for each repeated item.
  8. *
  9. * @access protected
  10. * @var string
  11. */
  12. protected $item_name;
  13. /**
  14. * This associative array describes how the repeater may retrieve the item labels from HTML elements as they are
  15. * updated. The options are:
  16. * - selector string A JQuery selector which is used to find an element from which to retrieve the item label.
  17. * - update_event string The JavaScript event on which to bind and update the item label.
  18. * - value_method string The JavaScript function which should be used to retrieve the item label from an element.
  19. *
  20. * @access protected
  21. * @var array
  22. */
  23. protected $item_label;
  24. /**
  25. * The maximum number of repeated items to display before adding a scrollbar to the repeater.
  26. *
  27. * @access protected
  28. * @var int
  29. */
  30. protected $scroll_count;
  31. /**
  32. * Whether or not items may be added to or removed from this repeater by user interaction.
  33. *
  34. * @access protected
  35. * @var bool
  36. */
  37. protected $readonly;
  38. protected function render_field( $value, $instance ) {
  39. if( !isset( $this->fields ) || empty( $this->fields ) ) return;
  40. $container = array( 'name' => $this->base_name, 'type' => 'repeater' );
  41. $item_label = isset( $this->item_label ) ? $this->item_label : null;
  42. if ( ! empty( $item_label ) ) {
  43. // convert underscore naming convention to camelCase for javascript and encode as json string
  44. $item_label = wp_parse_args( $item_label, array(
  45. 'update_event' => 'change',
  46. 'value_method' => 'val'
  47. ) );
  48. $item_label = siteorigin_widgets_underscores_to_camel_case( $item_label );
  49. $item_label = json_encode( $item_label );
  50. }
  51. if( empty( $this->item_name ) ) $this->item_name = __( 'Item', 'so-widgets-bundle' );
  52. ?>
  53. <div class="siteorigin-widget-field-repeater"
  54. data-item-name="<?php echo esc_attr( $this->item_name ) ?>"
  55. data-repeater-name="<?php echo esc_attr( $this->base_name ) ?>"
  56. data-element-name="<?php echo esc_attr( $this->element_name ) ?>"
  57. <?php echo ! empty( $item_label ) ? 'data-item-label="' . esc_attr( $item_label ) . '"' : '' ?>
  58. <?php echo ! empty( $this->scroll_count ) ? 'data-scroll-count="' . esc_attr( $this->scroll_count ) . '"' : '' ?>
  59. <?php if( ! empty( $this->readonly ) ) echo 'readonly' ?>>
  60. <div class="siteorigin-widget-field-repeater-top">
  61. <div class="siteorigin-widget-field-repeater-expand"></div>
  62. <h3><?php echo esc_html( $this->label ) ?></h3>
  63. </div>
  64. <div class="siteorigin-widget-field-repeater-items">
  65. <?php
  66. if( !empty( $value ) ) {
  67. foreach( $value as $v ) {
  68. ?>
  69. <div class="siteorigin-widget-field-repeater-item ui-draggable">
  70. <div class="siteorigin-widget-field-repeater-item-top">
  71. <div class="siteorigin-widget-field-expand"></div>
  72. <?php if( empty( $this->readonly ) ) : ?>
  73. <div class="siteorigin-widget-field-copy"></div>
  74. <div class="siteorigin-widget-field-remove"></div>
  75. <?php endif; ?>
  76. <h4><?php echo esc_html( $this->item_name ) ?></h4>
  77. </div>
  78. <div class="siteorigin-widget-field-repeater-item-form">
  79. <?php
  80. $this->create_and_render_sub_fields( $v, $container );
  81. ?>
  82. </div>
  83. </div>
  84. <?php
  85. }
  86. }
  87. ?>
  88. </div>
  89. <?php if( empty( $this->readonly ) ) : ?>
  90. <div class="siteorigin-widget-field-repeater-add"><?php esc_html_e( 'Add', 'so-widgets-bundle' ) ?></div>
  91. <?php endif; ?>
  92. <?php
  93. ob_start();
  94. $this->create_and_render_sub_fields( null, $container, true );
  95. $rpt_fields = ob_get_clean();
  96. $rpt_fields = preg_replace( '/\s+name\s*=\s*/', ' data-name=', $rpt_fields );
  97. ?>
  98. <div class="siteorigin-widget-field-repeater-item-html" style="display: none;">
  99. <?php echo $rpt_fields; ?>
  100. </div>
  101. </div>
  102. <?php
  103. }
  104. protected function render_field_label( $value, $instance ) {
  105. // Empty override. This field renders it's own label in the render_field() function.
  106. }
  107. /**
  108. * Go over the items in the repeater and sanitize each one using the container sanitization function.
  109. *
  110. * @param mixed $value
  111. *
  112. * @return array|mixed
  113. */
  114. function sanitize_field_input( $value, $instance ){
  115. if( empty($value) ) return array();
  116. foreach( $value as &$el ) {
  117. $el = parent::sanitize_field_input( $el, $instance );
  118. }
  119. return $value;
  120. }
  121. }