/kirby/extensions/methods.php

https://gitlab.com/gricelya/Blog-Merry · PHP · 269 lines · 101 code · 33 blank · 135 comment · 3 complexity · 2c3faf5cf16f79e38c2498dae8b2d6f1 MD5 · raw file

  1. <?php
  2. /**
  3. * Converts the field value to valid html
  4. * @param Field $field The calling Kirby Field instance
  5. * @param boolean $keepTags Don't touch valid html tags
  6. * @return Field
  7. */
  8. field::$methods['html'] = field::$methods['h'] = function($field, $keepTags = true) {
  9. $field->value = html($field->value, $keepTags);
  10. return $field;
  11. };
  12. /**
  13. * Escapes unwanted characters in the field value
  14. * to protect from possible xss attacks or other
  15. * unwanted side effects in your html code
  16. * @param Field $field The calling Kirby Field instance
  17. * @param string $context html|attr|css|js|url
  18. * @return Field
  19. */
  20. field::$methods['escape'] = field::$methods['esc'] = function($field, $context = 'html') {
  21. $field->value = esc($field->value, $context);
  22. return $field;
  23. };
  24. /**
  25. * Converts html entities and specialchars in the field
  26. * value to valid xml entities
  27. * @param Field $field The calling Kirby Field instance
  28. * @return Field
  29. */
  30. field::$methods['xml'] = field::$methods['x'] = function($field) {
  31. $field->value = xml($field->value);
  32. return $field;
  33. };
  34. /**
  35. * Parses the field value as kirbytext
  36. * @param Field $field The calling Kirby Field instance
  37. * @return Field
  38. */
  39. field::$methods['kirbytext'] = field::$methods['kt'] = function($field) {
  40. $field->value = kirbytext($field);
  41. return $field;
  42. };
  43. /**
  44. * Parses the field value as markdown
  45. * @param Field $field The calling Kirby Field instance
  46. * @return Field
  47. */
  48. field::$methods['markdown'] = field::$methods['md'] = function($field) {
  49. $field->value = markdown($field->value);
  50. return $field;
  51. };
  52. /**
  53. * Converts the field value to lower case
  54. * @param Field $field The calling Kirby Field instance
  55. * @return Field
  56. */
  57. field::$methods['lower'] = function($field) {
  58. $field->value = str::lower($field->value);
  59. return $field;
  60. };
  61. /**
  62. * Converts the field value to upper case
  63. * @param Field $field The calling Kirby Field instance
  64. * @return Field
  65. */
  66. field::$methods['upper'] = function($field) {
  67. $field->value = str::upper($field->value);
  68. return $field;
  69. };
  70. /**
  71. * Applies the widont rule to avoid single
  72. * words on the last line
  73. * @param Field $field The calling Kirby Field instance
  74. * @return Field
  75. */
  76. field::$methods['widont'] = function($field) {
  77. $field->value = widont($field->value);
  78. return $field;
  79. };
  80. /**
  81. * Creates a simple text excerpt without formats
  82. * @param Field $field The calling Kirby Field instance
  83. * @param integer $chars The desired excerpt length
  84. * @return string
  85. */
  86. field::$methods['excerpt'] = function($field, $chars = 140, $mode = 'chars') {
  87. return excerpt($field, $chars, $mode);
  88. };
  89. /**
  90. * Shortens the field value by the given length
  91. * @param Field $field The calling Kirby Field instance
  92. * @param integer $length The desired string length
  93. * @param string $rep The attached ellipsis character if the string is longer
  94. * @return string
  95. */
  96. field::$methods['short'] = function($field, $length, $rep = '…') {
  97. return str::short($field->value, $length, $rep);
  98. };
  99. /**
  100. * Returns the string length of the field value
  101. * @param Field $field The calling Kirby Field instance
  102. * @return integer
  103. */
  104. field::$methods['length'] = function($field) {
  105. return str::length($field->value);
  106. };
  107. /**
  108. * Returns the word count for the field value
  109. * @param Field $field The calling Kirby Field instance
  110. * @return integer
  111. */
  112. field::$methods['words'] = function($field) {
  113. return str_word_count(strip_tags($field->value));
  114. };
  115. /**
  116. * Splits the field value by the given separator
  117. * @param Field $field The calling Kirby Field instance
  118. * @param string $separator The string to split the field value by
  119. * @return array
  120. */
  121. field::$methods['split'] = function($field, $separator = ',') {
  122. return str::split($field->value, $separator);
  123. };
  124. /**
  125. * Parses the field value as yaml and returns an array
  126. * @param Field $field The calling Kirby Field instance
  127. * @return array
  128. */
  129. field::$methods['yaml'] = function($field) {
  130. return yaml($field->value);
  131. };
  132. /**
  133. * Checks if the field value is empty
  134. * @param Field $field The calling Kirby Field instance
  135. * @return boolean
  136. */
  137. field::$methods['empty'] = field::$methods['isEmpty'] = function($field) {
  138. return empty($field->value);
  139. };
  140. /**
  141. * Checks if the field value is not empty
  142. * @param Field $field The calling Kirby Field instance
  143. * @return boolean
  144. */
  145. field::$methods['isNotEmpty'] = function($field) {
  146. return !$field->isEmpty();
  147. };
  148. /**
  149. * Returns a page object from a uri in a field
  150. * @param Field $field The calling Kirby Field instance
  151. * @return Collection
  152. */
  153. field::$methods['toPage'] = function($field) {
  154. return page($field->value);
  155. };
  156. /**
  157. * Returns all page objects from a yaml list in a field
  158. * @param Field $field The calling Kirby Field instance
  159. * @return Collection
  160. */
  161. field::$methods['pages'] = field::$methods['toPages'] = function($field) {
  162. $related = array();
  163. foreach($field->yaml() as $r) {
  164. // make sure to only add found related pages
  165. if($rel = page($r)) $related[$rel->id()] = $rel;
  166. }
  167. return new Collection($related);
  168. };
  169. /**
  170. * Returns a file object from a filename in a field
  171. * @param Field $field The calling Kirby Field instance
  172. * @return Collection
  173. */
  174. field::$methods['toFile'] = function($field) {
  175. return $field->page()->file($field->value);
  176. };
  177. /**
  178. * Adds 'or' method to Field objects, which allows getting a field
  179. * value or getting back a default value if the field is empty.
  180. * @author fvsch <florent@fvsch.com>
  181. * @param Field $field The calling Kirby Field instance
  182. * @param mixed $fallback Fallback value returned if field is empty
  183. * @return mixed
  184. */
  185. field::$methods['or'] = function($field, $fallback = null) {
  186. return $field->empty() ? $fallback : $field;
  187. };
  188. /**
  189. * Filter the Field value, or a fallback value if the Field is empty,
  190. * to get a boolean value. '1', 'on', 'true' or 'yes' will be true,
  191. * and everything else will be false.
  192. * @author fvsch <florent@fvsch.com>
  193. * @param Field $field The calling Kirby Field instance
  194. * @param boolean $default Default value returned if field is empty
  195. * @return boolean
  196. */
  197. field::$methods['bool'] = field::$methods['isTrue'] = function($field, $default = false) {
  198. $val = $field->empty() ? $default : $field->value;
  199. return filter_var($val, FILTER_VALIDATE_BOOLEAN);
  200. };
  201. /**
  202. * Checks if the field content is false
  203. * @param Field $field The calling Kirby Field instance
  204. * @return boolean
  205. */
  206. field::$methods['isFalse'] = function($field) {
  207. return !$field->bool();
  208. };
  209. /**
  210. * Get an integer value for the Field.
  211. * @author fvsch <florent@fvsch.com>
  212. * @param Object(Field) [$field] The calling Kirby Field instance
  213. * @param integer [$default] Default value returned if field is empty
  214. * @return integer
  215. */
  216. field::$methods['int'] = function($field, $default = 0) {
  217. $val = $field->empty() ? $default : $field->value;
  218. return intval($val);
  219. };
  220. field::$methods['toStructure'] = field::$methods['structure'] = function($field) {
  221. return structure($field->yaml(), $field->page());
  222. };
  223. field::$methods['link'] = function($field, $attr1 = array(), $attr2 = array()) {
  224. $a = new Brick('a', $field->value());
  225. if(is_string($attr1)) {
  226. $a->attr('href', url($attr1));
  227. $a->attr($attr2);
  228. } else {
  229. $a->attr('href', $field->page()->url());
  230. $a->attr($attr1);
  231. }
  232. return $a;
  233. };
  234. field::$methods['toUrl'] = field::$methods['url'] = function($field) {
  235. return url($field->value());
  236. };