PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/option-tree/functions/functions.php

https://bitbucket.org/samsanders/love-courage-hope
PHP | 384 lines | 197 code | 36 blank | 151 comment | 46 complexity | 1cb5f128c11deaccd3ef419afaed3d11 MD5 | raw file
  1. <?php if (!defined('OT_VERSION')) exit('No direct script access allowed');
  2. /**
  3. * General Functions
  4. *
  5. * @package WordPress
  6. * @subpackage OptionTree
  7. * @since 1.1.8
  8. * @author Derek Herman
  9. */
  10. /**
  11. * Recognized font styles
  12. *
  13. * Returns an array of all recognized font styles.
  14. *
  15. * @uses apply_filters()
  16. *
  17. * @access public
  18. * @since 1.1.8
  19. *
  20. * @return array
  21. */
  22. function recognized_font_styles() {
  23. return apply_filters( 'recognized_font_styles', array(
  24. 'normal' => 'Normal',
  25. 'italic' => 'Italic',
  26. 'oblique' => 'Oblique',
  27. 'inherit' => 'Inherit'
  28. ) );
  29. }
  30. /**
  31. * Recognized font weights
  32. *
  33. * Returns an array of all recognized font weights.
  34. *
  35. * @uses apply_filters()
  36. *
  37. * @access public
  38. * @since 1.1.8
  39. *
  40. * @return array
  41. */
  42. function recognized_font_weights() {
  43. return apply_filters( 'recognized_font_weights', array(
  44. 'normal' => 'Normal',
  45. 'bold' => 'Bold',
  46. 'bolder' => 'Bolder',
  47. 'lighter' => 'Lighter',
  48. '100' => '100',
  49. '200' => '200',
  50. '300' => '300',
  51. '400' => '400',
  52. '500' => '500',
  53. '600' => '600',
  54. '700' => '700',
  55. '800' => '800',
  56. '900' => '900',
  57. 'inherit' => 'Inherit'
  58. ) );
  59. }
  60. /**
  61. * Recognized font variants
  62. *
  63. * Returns an array of all recognized font variants.
  64. *
  65. * @uses apply_filters()
  66. *
  67. * @access public
  68. * @since 1.1.8
  69. *
  70. * @return array
  71. */
  72. function recognized_font_variants() {
  73. return apply_filters( 'recognized_font_variants', array(
  74. 'normal' => 'Normal',
  75. 'small-caps' => 'Small Caps',
  76. 'inherit' => 'Inherit'
  77. ) );
  78. }
  79. /**
  80. * Recognized font families
  81. *
  82. * Returns an array of all recognized font families.
  83. * Keys are intended to be stored in the database
  84. * while values are ready for display in html.
  85. *
  86. * @uses apply_filters()
  87. *
  88. * @access public
  89. * @since 1.1.8
  90. *
  91. * @return array
  92. */
  93. function recognized_font_families() {
  94. return apply_filters( 'recognized_font_families', array(
  95. 'arial' => 'Arial',
  96. 'georgia' => 'Georgia',
  97. 'helvetica' => 'Helvetica',
  98. 'palatino' => 'Palatino',
  99. 'tahoma' => 'Tahoma',
  100. 'times' => '"Times New Roman", sans-serif',
  101. 'trebuchet' => 'Trebuchet',
  102. 'verdana' => 'Verdana'
  103. ) );
  104. }
  105. /**
  106. * Recognized background repeat
  107. *
  108. * Returns an array of all recognized background repeat values.
  109. *
  110. * @uses apply_filters()
  111. *
  112. * @access public
  113. * @since 1.1.8
  114. *
  115. * @return array
  116. */
  117. function recognized_background_repeat() {
  118. return apply_filters( 'recognized_background_repeat', array(
  119. 'no-repeat' => 'No Repeat',
  120. 'repeat' => 'Repeat All',
  121. 'repeat-x' => 'Repeat Horizontally',
  122. 'repeat-y' => 'Repeat Vertically',
  123. 'inherit' => 'Inherit'
  124. ) );
  125. }
  126. /**
  127. * Recognized background attachment
  128. *
  129. * Returns an array of all recognized background attachment values.
  130. *
  131. * @uses apply_filters()
  132. *
  133. * @access public
  134. * @since 1.1.8
  135. *
  136. * @return array
  137. */
  138. function recognized_background_attachment() {
  139. return apply_filters( 'recognized_background_attachment', array(
  140. "fixed" => "Fixed",
  141. "scroll" => "Scroll",
  142. "inherit" => "Inherit"
  143. ) );
  144. }
  145. /**
  146. * Recognized background position
  147. *
  148. * Returns an array of all recognized background position values.
  149. *
  150. * @uses apply_filters()
  151. *
  152. * @access public
  153. * @since 1.1.8
  154. *
  155. * @return array
  156. */
  157. function recognized_background_position() {
  158. return apply_filters( 'recognized_background_position', array(
  159. "left top" => "Left Top",
  160. "left center" => "Left Center",
  161. "left bottom" => "Left Bottom",
  162. "center top" => "Center Top",
  163. "center center" => "Center Center",
  164. "center bottom" => "Center Bottom",
  165. "right top" => "Right Top",
  166. "right center" => "Right Center",
  167. "right bottom" => "Right Bottom"
  168. ) );
  169. }
  170. /**
  171. * Measurement Units
  172. *
  173. * Returns an array of all available unit types.
  174. *
  175. * @uses apply_filters()
  176. *
  177. * @access public
  178. * @since 1.1.8
  179. *
  180. * @return array
  181. */
  182. function measurement_unit_types() {
  183. return apply_filters( 'measurement_unit_types', array(
  184. 'px' => 'px',
  185. '%' => '%',
  186. 'em' => 'em',
  187. 'pt' => 'pt'
  188. ) );
  189. }
  190. /**
  191. * Find CSS option type and add to style.css
  192. *
  193. * @since 1.1.8
  194. *
  195. * @return bool True on write success, false on failure.
  196. */
  197. function option_tree_css_save() {
  198. global $wpdb;
  199. $options = $wpdb->get_results( "SELECT item_id FROM " . OT_TABLE_NAME . " WHERE `item_type` = 'css' ORDER BY `item_sort` ASC" );
  200. foreach ( $options as $option )
  201. option_tree_insert_css_with_markers( $option->item_id );
  202. return false;
  203. }
  204. /**
  205. * Inserts CSS with Markers
  206. *
  207. * Inserts CSS into a dynamic.css file, placing it between
  208. * BEGIN and END markers. Replaces existing marked info. Retains surrounding
  209. * data.
  210. *
  211. * @since 1.1.8
  212. *
  213. * @return bool True on write success, false on failure.
  214. */
  215. function option_tree_insert_css_with_markers( $option = '' ) {
  216. /* No option defined */
  217. if ( !$option )
  218. return;
  219. /* path to the dynamic.css file */
  220. $filepath = get_stylesheet_directory().'/dynamic.css';
  221. /* allow filter on path */
  222. $filepath = apply_filters( 'css_option_file_path', $filepath, $option );
  223. /* Insert CSS into file */
  224. if ( ! file_exists( $filepath ) || is_writeable( $filepath ) ) {
  225. /* Get options & set CSS value */
  226. $options = get_option('option_tree');
  227. $insertion = option_tree_normalize_css( stripslashes( $options[$option] ) );
  228. $regex = "/{{([a-zA-Z0-9\_\-\#\|\=]+)}}/";
  229. $marker = $option;
  230. /* Match custom CSS */
  231. preg_match_all( $regex, $insertion, $matches );
  232. /* Loop through CSS */
  233. foreach( $matches[0] as $option ) {
  234. $the_option = str_replace( array('{{', '}}'), '', $option );
  235. $option_array = explode("|", $the_option );
  236. /* get array by key from key|value explode */
  237. if ( is_array( $option_array ) ) {
  238. $value = $options[$option_array[0]];
  239. /* get the whole array from $option param */
  240. } else {
  241. $value = $options[$option];
  242. }
  243. if ( is_array( $value ) ) {
  244. /* key|value explode didn't return a second value */
  245. if ( !isset($option_array[1]) ) {
  246. /* Measurement */
  247. if ( isset( $value[0] ) && isset( $value[1] ) ) {
  248. $value = $value[0].$value[1];
  249. /* typography */
  250. } else if ( isset( $value['font-color'] ) || isset( $value['font-style'] ) || isset( $value['font-variant'] ) || isset( $value['font-weight'] ) || isset( $value['font-size'] ) || isset( $value['font-family'] ) ) {
  251. $font = array();
  252. if ( ! empty( $value['font-color'] ) )
  253. $font[] = "font-color: " . $value['font-color'] . ";";
  254. foreach ( recognized_font_families() as $key => $v ) {
  255. if ( ! empty( $value['font-family'] ) && $key == $value['font-family'] )
  256. $font[] = "font-family: " . $v . ";";
  257. }
  258. if ( ! empty( $value['font-size'] ) )
  259. $font[] = "font-size: " . $value['font-size'] . ";";
  260. if ( ! empty( $value['font-style'] ) )
  261. $font[] = "font-style: " . $value['font-style'] . ";";
  262. if ( ! empty( $value['font-variant'] ) )
  263. $font[] = "font-variant: " . $value['font-variant'] . ";";
  264. if ( ! empty( $value['font-weight'] ) )
  265. $font[] = "font-weight: " . $value['font-weight'] . ";";
  266. if ( ! empty( $font ) )
  267. $value = implode( "\n", $font );
  268. /* background */
  269. } else if ( isset( $value['background-color'] ) || isset( $value['background-image'] ) ) {
  270. $bg = array();
  271. if ( ! empty( $value['background-color'] ) )
  272. $bg[] = $value['background-color'];
  273. if ( ! empty( $value['background-image'] ) )
  274. $bg[] = 'url("' . $value['background-image'] . '")';
  275. if ( ! empty( $value['background-repeat'] ) )
  276. $bg[] = $value['background-repeat'];
  277. if ( ! empty( $value['background-attachment'] ) )
  278. $bg[] = $value['background-attachment'];
  279. if ( ! empty( $value['background-position'] ) )
  280. $bg[] = $value['background-position'];
  281. if ( ! empty( $bg ) )
  282. $value = 'background: ' . implode( " ", $bg ) . ';';
  283. }
  284. /* key|value explode return a second value */
  285. } else {
  286. $value = $value[$option_array[1]];
  287. }
  288. }
  289. $insertion = stripslashes( str_replace( $option, $value, $insertion ) );
  290. }
  291. /* file doesn't exist */
  292. if ( ! file_exists( $filepath ) ) {
  293. $markerdata = '';
  294. /* file exist, create array from the lines of code */
  295. } else {
  296. $markerdata = explode( "\n", implode( '', file( $filepath ) ) );
  297. }
  298. /* can't write to the file return false */
  299. if ( !$f = @fopen( $filepath, 'w' ) )
  300. return false;
  301. $foundit = false;
  302. /* has array of lines */
  303. if ( $markerdata ) {
  304. $state = true;
  305. /* foreach line of code */
  306. foreach ( $markerdata as $n => $markerline ) {
  307. /* found begining of marker, set state to false */
  308. if ( strpos( $markerline, '/* BEGIN ' . $marker . ' */' ) !== false )
  309. $state = false;
  310. /* state is true, rebuild css */
  311. if ( $state ) {
  312. if ( $n + 1 < count( $markerdata ) )
  313. fwrite( $f, "{$markerline}\n" );
  314. else
  315. fwrite( $f, "{$markerline}" );
  316. }
  317. /* found end marker write code */
  318. if ( strpos( $markerline, '/* END ' . $marker . ' */' ) !== false ) {
  319. fwrite( $f, "/* BEGIN {$marker} */\n" );
  320. fwrite( $f, "{$insertion}\n" );
  321. fwrite( $f, "/* END {$marker} */\n" );
  322. $state = true;
  323. $foundit = true;
  324. }
  325. }
  326. }
  327. /* nothing inserted, write code. DO IT, DO IT! */
  328. if ( ! $foundit ) {
  329. fwrite( $f, "\n\n/* BEGIN {$marker} */\n" );
  330. fwrite( $f, "{$insertion}\n" );
  331. fwrite( $f, "/* END {$marker} */\n" );
  332. }
  333. /* close file */
  334. fclose( $f );
  335. return true;
  336. } else {
  337. return false;
  338. }
  339. }
  340. function option_tree_normalize_css( $s ) {
  341. // Normalize line endings
  342. // Convert all line-endings to UNIX format
  343. $s = str_replace( "\r\n", "\n", $s );
  344. $s = str_replace( "\r", "\n", $s );
  345. // Don't allow out-of-control blank lines
  346. $s = preg_replace( "/\n{2,}/", "\n\n", $s );
  347. return $s;
  348. }