/v4/acf-generous-slider.php

  1. <?php 
  2.  
  3. class acf_field_generous_slider extends acf_field { 
  4.  
  5. /** 
  6. * The id of the field. 
  7. * 
  8. * @since 3.6 
  9. * 
  10. * @var array $name Single word, no spaces. 
  11. */ 
  12. var $name; 
  13.  
  14. /** 
  15. * The label for the field. 
  16. * 
  17. * @since 3.6 
  18. * 
  19. * @var string $label Visible when selecting a field type. 
  20. */ 
  21. var $label; 
  22.  
  23. /** 
  24. * The type of ACF field. 
  25. * 
  26. * @since 3.6 
  27. * 
  28. * @var string $category Basic|Content|Choice|Relational|Layout 
  29. */ 
  30. var $category; 
  31.  
  32. /** 
  33. * The settings for the field. 
  34. * 
  35. * @since 3.6 
  36. * 
  37. * @var array $settings The settings for the field. 
  38. */ 
  39. var $settings; 
  40.  
  41. /** 
  42. * Default settings which are merged into the field object. 
  43. * 
  44. * @since 3.6 
  45. * 
  46. * @var array $defaults The defaults for the field. 
  47. */ 
  48. var $defaults; 
  49.  
  50. /** 
  51. * Initialize the class. 
  52. * 
  53. * Set name, label needed for actions, filters. 
  54. * 
  55. * @since 3.6 
  56. */ 
  57. function __construct() { 
  58. $this->name = 'generous_slider'; 
  59. $this->label = __( 'Generous Slider'); 
  60. $this->category = __( 'Basic', 'acf' ); 
  61. $this->defaults = array(); 
  62. $this->settings = array( 
  63. 'path' => apply_filters( 'acf/helpers/get_path', __FILE__ ),  
  64. 'dir' => apply_filters( 'acf/helpers/get_dir', __FILE__ ),  
  65. 'version' => '0.1.0' 
  66. ); 
  67.  
  68. parent::__construct(); 
  69.  
  70. /** 
  71. * Create extra options for your field. This is rendered when editing a field. 
  72. * The value of $field['name'] can be used (like below) to save extra data to the $field 
  73. * 
  74. * @since 3.6 
  75. * 
  76. * @param array $field The field array holding all the field options. 
  77. */ 
  78. function create_options( $field ) { 
  79. $key = $field['name']; 
  80.  
  81. /** 
  82. * Creates the HTML interface for the field 
  83. * 
  84. * @since 3.6 
  85. * 
  86. * @param array $field The field array holding all the field options. 
  87. */ 
  88. function create_field( $field ) { 
  89.  
  90. global $wp_plugin_generous; 
  91.  
  92. $options = $wp_plugin_generous->get_options(); 
  93.  
  94. if( isset( $options['username'] ) && $options['username'] !== '') { 
  95.  
  96. $field_id = ''; 
  97. $field_title = ''; 
  98.  
  99. if ( isset( $field['value'], $field['value']['id'] ) ) { 
  100. $field_id = $field['value']['id']; 
  101.  
  102. if ( isset( $field['value']['title'] ) ) { 
  103. $field_title = $field['value']['title']; 
  104.  
  105. $prefix = 'acf-generous-slider--search'; 
  106.  
  107. echo "<input type=\"text\" name=\"{$field['name']}[title]\" class=\"{$prefix}-input-title\" value=\"" . htmlspecialchars( $field_title ) . "\" />"; 
  108. echo "<input type=\"hidden\" name=\"{$field['name']}[id]\" class=\"{$prefix}-input-id\" value=\"{$field_id}\" />"; 
  109. echo "<input type=\"hidden\" class=\"{$prefix}-account\" value=\"{$options['username']}\" />"; 
  110.  
  111. echo "<div class=\"{$prefix}-results\"></div>"; 
  112.  
  113. } else { 
  114.  
  115. echo "<span>Generous username not found. Check plugin settings.</span>"; 
  116.  
  117.  
  118.  
  119. /** 
  120. * This action is called in the admin_enqueue_scripts action on the edit 
  121. * screen where your field is created. Use this action to add CSS + JavaScript 
  122. * to assist your create_field() action. 
  123. * 
  124. * Info: http://codex.wordpress.org/Plugin_API/Action_Reference/admin_enqueue_scripts 
  125. * 
  126. * @since 3.6 
  127. */ 
  128. function input_admin_enqueue_scripts() { 
  129.  
  130. wp_register_script( 'acf-input-generous_slider', plugin_dir_url( dirname( __FILE__ ) ) . 'assets/js/input.js', array('acf-input'), $this->settings['version'] ); 
  131. wp_register_style( 'acf-input-generous_slider', plugin_dir_url( dirname( __FILE__ ) ) . 'assets/css/input.css', array('acf-input'), $this->settings['version'] );  
  132.  
  133. wp_enqueue_script(array( 
  134. 'acf-input-generous_slider',  
  135. )); 
  136.  
  137. wp_enqueue_style(array( 
  138. 'acf-input-generous_slider',  
  139. )); 
  140.  
  141.  
  142. /** 
  143. * This action is called in the admin_head action on the edit screen where 
  144. * your field is created. Use this action to add CSS and JavaScript to assist 
  145. * your create_field() action. 
  146. * 
  147. * Info: http://codex.wordpress.org/Plugin_API/Action_Reference/admin_head 
  148. * 
  149. * @since 3.6 
  150. */ 
  151. function input_admin_head() { 
  152. // Note: This function can be removed if not used 
  153.  
  154. /** 
  155. * This action is called in the admin_enqueue_scripts action on the edit 
  156. * screen where your field is edited. Use this action to add CSS + JavaScript 
  157. * to assist your create_field_options() action. 
  158. * 
  159. * Info: http://codex.wordpress.org/Plugin_API/Action_Reference/admin_enqueue_scripts 
  160. * 
  161. * @since 3.6 
  162. */ 
  163. function field_group_admin_enqueue_scripts() { 
  164. // Note: This function can be removed if not used 
  165.  
  166. /** 
  167. * This action is called in the admin_head action on the edit screen where 
  168. * your field is edited. Use this action to add CSS and JavaScript to assist 
  169. * your create_field_options() action. 
  170. * 
  171. * Info: http://codex.wordpress.org/Plugin_API/Action_Reference/admin_head 
  172. * 
  173. * @since 3.6 
  174. */ 
  175. function field_group_admin_head() { 
  176. // Note: This function can be removed if not used 
  177.  
  178. /** 
  179. * Loads the value after the database. 
  180. * 
  181. * This filter is applied to the $value after it is loaded from the db. 
  182. * 
  183. * @since 3.6 
  184. * 
  185. * @param array $value The value found in the database. 
  186. * @param int $post_id The $post_id from which the value was loaded. 
  187. * @param array $field The field array holding all the field options. 
  188. * 
  189. * @return array|false $value The new value with slider data, or false. 
  190. */ 
  191. function load_value( $value, $post_id, $field ) { 
  192. return $value; 
  193.  
  194. /** 
  195. * Updates the value for the database. 
  196. * 
  197. * This filter is applied to the $value before it is updated in the db. 
  198. * 
  199. * @since 3.6 
  200. * 
  201. * @param array $value The value which will be saved in the database. 
  202. * @param int $post_id The $post_id of which the value will be saved. 
  203. * @param array $field The field array holding all the field options. 
  204. * 
  205. * @return array $value The modified value. 
  206. */ 
  207. function update_value( $value, $post_id, $field ) { 
  208. if ( '' === $value['id'] ) { 
  209. $value = false; 
  210.  
  211. return $value; 
  212.  
  213. /** 
  214. * Formats the value before the field is created in the admin. 
  215. * 
  216. * This filter is applied to the $value after it is loaded from the db and 
  217. * before it is passed to the create_field action. 
  218. * 
  219. * @since 3.6 
  220. * 
  221. * @param array $value The value which will be saved in the database. 
  222. * @param int $post_id The $post_id of which the value will be saved. 
  223. * @param array $field The field array holding all the field options. 
  224. * 
  225. * @return array $value The modified value. 
  226. */ 
  227. function format_value( $value, $post_id, $field ) { 
  228. return $value; 
  229.  
  230. /** 
  231. * Formats the value for the frontend. 
  232. * 
  233. * This filter is applied to the $value after it is loaded from the db and 
  234. * before it is passed back to the API functions such as the_field. 
  235. * 
  236. * @since 3.6 
  237. * 
  238. * @param array $value The value which will be saved in the database. 
  239. * @param int $post_id The $post_id of which the value will be saved. 
  240. * @param array $field The field array holding all the field options. 
  241. * 
  242. * @return array $value The modified value. 
  243. */ 
  244. function format_value_for_api( $value, $post_id, $field ) { 
  245. if ( isset( $value['id'] ) ) { 
  246. $api = new WP_Generous_Api(); 
  247. return $api->get_slider( $value['id'] ); 
  248. } else { 
  249. return false; 
  250.  
  251. /** 
  252. * Updates the field after it is loaded from the database. 
  253. * 
  254. * This filter is applied to the $field after it is loaded from the database. 
  255. * 
  256. * @since 3.6 
  257. * 
  258. * @param array $field The field array holding all the field options. 
  259. * 
  260. * @return array $field The modified field. 
  261. */ 
  262. function load_field( $field ) { 
  263. return $field; 
  264.  
  265. /** 
  266. * Updates the field before it's saved to the database. 
  267. * 
  268. * This filter is applied to the $field before it is saved to the database. 
  269. * 
  270. * @since 3.6 
  271. * 
  272. * @param array $field The field array holding all the field options. 
  273. * @param int $post_id The field group ID (post_type = acf). 
  274. * 
  275. * @return array $field The modified field. 
  276. */ 
  277. function update_field( $field, $post_id ) { 
  278. return $field; 
  279.  
  280.  
  281. new acf_field_generous_slider(); 
.