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

/wp-content/plugins/types/embedded/types.php

https://bitbucket.org/cisash/fananeen
PHP | 489 lines | 331 code | 36 blank | 122 comment | 53 complexity | d8d5b51a9abef6446b510a84ca5b3267 MD5 | raw file
  1. <?php
  2. if (!defined('WPCF_VERSION')) {
  3. define('WPCF_RUNNING_EMBEDDED', true);
  4. add_action('init', 'wpcf_embedded_init');
  5. }
  6. define('WPCF_EMBEDDED_ABSPATH', dirname(__FILE__));
  7. define('WPCF_EMBEDDED_INC_ABSPATH', WPCF_EMBEDDED_ABSPATH . '/includes');
  8. define('WPCF_EMBEDDED_RES_ABSPATH', WPCF_EMBEDDED_ABSPATH . '/resources');
  9. if (!defined('WPCF_DEBUG')) {
  10. define('WPCF_DEBUG', false);
  11. }
  12. if (!defined('ICL_COMMON_FUNCTIONS')) {
  13. require_once WPCF_EMBEDDED_ABSPATH . '/common/functions.php';
  14. }
  15. // TODO Remove
  16. //require_once WPCF_EMBEDDED_INC_ABSPATH . '/footer-credit.php';
  17. wpcf_embedded_after_setup_theme_hook();
  18. /**
  19. * after_setup_theme hook.
  20. */
  21. function wpcf_embedded_after_setup_theme_hook() {
  22. $custom_types = get_option('wpcf-custom-types', array());
  23. if (!empty($custom_types)) {
  24. foreach ($custom_types as $post_type => $data) {
  25. if (!empty($data['supports']['thumbnail'])) {
  26. if (!current_theme_supports('post-thumbnails')) {
  27. add_theme_support('post-thumbnails');
  28. remove_post_type_support('post', 'thumbnail');
  29. remove_post_type_support('page', 'thumbnail');
  30. } else {
  31. add_post_type_support($post_type, 'thumbnail');
  32. }
  33. }
  34. }
  35. }
  36. }
  37. /**
  38. * Main init hook.
  39. */
  40. function wpcf_embedded_init() {
  41. $locale = get_locale();
  42. load_textdomain('wpcf',
  43. WPCF_EMBEDDED_ABSPATH . '/locale/types-' . $locale . '.mo');
  44. if (!defined('WPV_VERSION')) {
  45. load_textdomain('wpv-views',
  46. WPCF_EMBEDDED_ABSPATH . '/locale/locale-views/views-' . $locale . '.mo');
  47. }
  48. // Define necessary constants if plugin is not present
  49. if (!defined('WPCF_VERSION')) {
  50. define('WPCF_VERSION', '1.1.3.1');
  51. define('WPCF_META_PREFIX', 'wpcf-');
  52. define('WPCF_EMBEDDED_RELPATH', icl_get_file_relpath(__FILE__));
  53. } else {
  54. define('WPCF_EMBEDDED_RELPATH', WPCF_RELPATH . '/embedded');
  55. }
  56. define('WPCF_EMBEDDED_INC_RELPATH', WPCF_EMBEDDED_RELPATH . '/includes');
  57. define('WPCF_EMBEDDED_RES_RELPATH', WPCF_EMBEDDED_RELPATH . '/resources');
  58. if (is_admin()) {
  59. require_once WPCF_EMBEDDED_ABSPATH . '/admin.php';
  60. wpcf_embedded_admin_init_hook();
  61. } else {
  62. require_once WPCF_EMBEDDED_ABSPATH . '/frontend.php';
  63. }
  64. wpcf_init_custom_types_taxonomies();
  65. if (defined('DOING_AJAX')) {
  66. require_once WPCF_EMBEDDED_ABSPATH . '/frontend.php';
  67. }
  68. wpcf_embedded_check_import();
  69. }
  70. /**
  71. * Inits custom types and taxonomies.
  72. */
  73. function wpcf_init_custom_types_taxonomies() {
  74. $custom_taxonomies = get_option('wpcf-custom-taxonomies', array());
  75. if (!empty($custom_taxonomies)) {
  76. require_once WPCF_EMBEDDED_INC_ABSPATH . '/custom-taxonomies.php';
  77. wpcf_custom_taxonomies_init();
  78. }
  79. $custom_types = get_option('wpcf-custom-types', array());
  80. if (!empty($custom_types)) {
  81. require_once WPCF_EMBEDDED_INC_ABSPATH . '/custom-types.php';
  82. wpcf_custom_types_init();
  83. }
  84. }
  85. /**
  86. * WPML translate call.
  87. *
  88. * @param type $name
  89. * @param type $string
  90. * @return type
  91. */
  92. function wpcf_translate($name, $string, $context = 'plugin Types') {
  93. if (!function_exists('icl_t')) {
  94. return $string;
  95. }
  96. return icl_t($context, $name, stripslashes($string));
  97. }
  98. /**
  99. * Registers WPML translation string.
  100. *
  101. * @param type $context
  102. * @param type $name
  103. * @param type $value
  104. */
  105. function wpcf_translate_register_string($context, $name, $value,
  106. $allow_empty_value = false) {
  107. if (function_exists('icl_register_string')) {
  108. icl_register_string($context, $name, stripslashes($value),
  109. $allow_empty_value);
  110. }
  111. }
  112. /**
  113. * Returns meta_key type for specific field type.
  114. *
  115. * @param type $type
  116. * @return type
  117. */
  118. function types_get_field_type($type) {
  119. require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
  120. $data = wpcf_fields_type_action($type);
  121. if (!empty($data['meta_key_type'])) {
  122. return $data['meta_key_type'];
  123. }
  124. return 'CHAR';
  125. }
  126. /**
  127. * Imports settings.
  128. */
  129. function wpcf_embedded_check_import() {
  130. if (file_exists(WPCF_EMBEDDED_ABSPATH . '/settings.php')) {
  131. require_once WPCF_EMBEDDED_ABSPATH . '/admin.php';
  132. require_once WPCF_EMBEDDED_ABSPATH . '/settings.php';
  133. $dismissed = get_option('wpcf_dismissed_messages', array());
  134. if (in_array($timestamp, $dismissed)) {
  135. return false;
  136. }
  137. if ($timestamp > get_option('wpcf-types-embedded-import', 0)) {
  138. if (!$auto_import) {
  139. wp_enqueue_script('wpcf-js',
  140. WPCF_EMBEDDED_RES_RELPATH . '/js/basic.js',
  141. array('jquery', 'jquery-ui-sortable', 'jquery-ui-draggable'),
  142. WPCF_VERSION);
  143. $link = "<a href=\"" . admin_url('?types-embedded-import=1&amp;_wpnonce=' . wp_create_nonce('embedded-import')) . "\">";
  144. $text = sprintf(__('You have Types import pending. %sClick here to import.%s %sDismiss message.%s',
  145. 'wpcf'), $link, '</a>',
  146. "<a onclick=\"jQuery(this).parent().parent().fadeOut();\" class=\"wpcf-ajax-link\" href=\""
  147. . admin_url('admin-ajax.php?action=wpcf_ajax&amp;wpcf_action=dismiss_message&amp;id='
  148. . $timestamp . '&amp;_wpnonce=' . wp_create_nonce('dismiss_message')) . "\">",
  149. '</a>');
  150. wpcf_admin_message($text);
  151. }
  152. if ($auto_import || (isset($_GET['types-embedded-import']) && isset($_GET['_wpnonce'])
  153. && wp_verify_nonce($_GET['_wpnonce'], 'embedded-import'))) {
  154. if (file_exists(WPCF_EMBEDDED_ABSPATH . '/settings.xml')) {
  155. $_POST['overwrite-groups'] = 1;
  156. $_POST['overwrite-fields'] = 1;
  157. $_POST['overwrite-types'] = 1;
  158. $_POST['overwrite-tax'] = 1;
  159. // $_POST['delete-groups'] = 0;
  160. // $_POST['delete-fields'] = 0;
  161. // $_POST['delete-types'] = 0;
  162. // $_POST['delete-tax'] = 0;
  163. $_POST['post_relationship'] = 1;
  164. require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
  165. require_once WPCF_EMBEDDED_INC_ABSPATH . '/import-export.php';
  166. $data = @file_get_contents(WPCF_EMBEDDED_ABSPATH . '/settings.xml');
  167. wpcf_admin_import_data($data, false);
  168. update_option('wpcf-types-embedded-import', $timestamp);
  169. wp_redirect(admin_url());
  170. } else {
  171. $code = __('settings.xml file missing', 'wpcf');
  172. wpcf_admin_message($code, 'error');
  173. }
  174. }
  175. }
  176. }
  177. }
  178. /**
  179. * Display information about upgrading to the plugin version of types.
  180. *
  181. */
  182. function wpcf_promote_types_admin() {
  183. $custom_types = get_option('wpcf-custom-types', array());
  184. ?>
  185. <?php
  186. if (sizeof($custom_types) > 0) {
  187. echo '<p>' . __('Types creates Custom Post Types. These are user-defined WordPress content types. On your theme the following Types are defined:',
  188. 'wpcf') . "</p>\n";
  189. echo "<ul style='margin-left:20px;'>\n";
  190. foreach ($custom_types as $type) {
  191. echo "<li>" . $type['labels']['name'] . "</li>\n";
  192. }
  193. echo "</ul>\n";
  194. }
  195. ?>
  196. <p><?php
  197. echo sprintf(__('If you want to edit these or create your own you can download the full version of <strong>Types</strong> from <a href="%s">%s</a>',
  198. 'wpcf'), 'http://wordpress.org/extend/plugins/types/',
  199. 'http://wordpress.org/extend/plugins/types/');
  200. ?></p>
  201. <?php
  202. }
  203. /**
  204. * Actions for outside fields control.
  205. *
  206. * @param type $action
  207. */
  208. function wpcf_types_cf_under_control($action = 'add', $args = array()) {
  209. global $wpcf_types_under_control;
  210. $wpcf_types_under_control['errors'] = array();
  211. switch ($action) {
  212. case 'add':
  213. $fields = wpcf_admin_fields_get_fields();
  214. foreach ($args['fields'] as $field_id) {
  215. $field_type = !empty($args['type']) ? $args['type'] : 'textfield';
  216. if (strpos($field_id, md5('wpcf_not_controlled')) !== false) {
  217. $field_id_add = str_replace('_' . md5('wpcf_not_controlled'),
  218. '', $field_id);
  219. // Activating field that previously existed in Types
  220. if (array_key_exists($field_id_add, $fields)) {
  221. $fields[$field_id_add]['data']['disabled'] = 0;
  222. } else { // Adding from outside
  223. $fields[$field_id_add]['id'] = $field_id_add;
  224. $fields[$field_id_add]['type'] = $field_type;
  225. $fields[$field_id_add]['name'] = $field_id_add;
  226. $fields[$field_id_add]['slug'] = $field_id_add;
  227. $fields[$field_id_add]['description'] = '';
  228. $fields[$field_id_add]['data'] = array();
  229. // @TODO WATCH THIS! MUST NOT BE DROPPED IN ANY CASE
  230. $fields[$field_id_add]['data']['controlled'] = 1;
  231. }
  232. $unset_key = array_search($field_id, $args['fields']);
  233. if ($unset_key !== false) {
  234. unset($args['fields'][$unset_key]);
  235. $args['fields'][$unset_key] = $field_id_add;
  236. }
  237. }
  238. }
  239. wpcf_admin_fields_save_fields($fields, true);
  240. return $args['fields'];
  241. break;
  242. case 'check_exists':
  243. $fields = wpcf_admin_fields_get_fields();
  244. $field = $args;
  245. if (array_key_exists($field, $fields) && empty($fields[$field]['data']['disabled'])) {
  246. return true;
  247. }
  248. return false;
  249. break;
  250. case 'check_outsider':
  251. $fields = wpcf_admin_fields_get_fields();
  252. $field = $args;
  253. if (array_key_exists($field, $fields) && !empty($fields[$field]['data']['controlled'])) {
  254. return true;
  255. }
  256. return false;
  257. break;
  258. default:
  259. break;
  260. }
  261. }
  262. /**
  263. * Controlls meta prefix.
  264. *
  265. * @param array $field
  266. */
  267. function wpcf_types_get_meta_prefix($field = array()) {
  268. if (empty($field)) {
  269. return WPCF_META_PREFIX;
  270. }
  271. if (!empty($field['data']['controlled'])) {
  272. return '';
  273. }
  274. return WPCF_META_PREFIX;
  275. }
  276. /**
  277. * Compares WP versions
  278. * @global type $wp_version
  279. * @param type $version
  280. * @param type $operator
  281. * @return type
  282. */
  283. function wpcf_compare_wp_version($version = '3.2.1', $operator = '>') {
  284. global $wp_version;
  285. return version_compare($wp_version, $version, $operator);
  286. }
  287. /**
  288. * Gets post type with data to which belongs.
  289. *
  290. * @param type $post_type
  291. * @return type
  292. */
  293. function wpcf_pr_get_belongs($post_type) {
  294. require_once WPCF_EMBEDDED_ABSPATH . '/includes/post-relationship.php';
  295. return wpcf_pr_admin_get_belongs($post_type);
  296. }
  297. /**
  298. * Gets all post types and data that owns.
  299. *
  300. * @param type $post_type
  301. * @return type
  302. */
  303. function wpcf_pr_get_has($post_type) {
  304. require_once WPCF_EMBEDDED_ABSPATH . '/includes/post-relationship.php';
  305. return wpcf_pr_admin_get_has($post_type);
  306. }
  307. /**
  308. * Gets individual post ID to which queried post belongs.
  309. *
  310. * @param type $post_id
  311. * @param type $post_type Post type of owner
  312. * @return type
  313. */
  314. function wpcf_pr_post_get_belongs($post_id, $post_type) {
  315. return get_post_meta($post_id, '_wpcf_belongs_' . $post_type . '_id', true);
  316. }
  317. /**
  318. * Gets all posts that belong to queried post, grouped by post type.
  319. *
  320. * @param type $post_id
  321. * @param type $post_type
  322. * @return type
  323. */
  324. function wpcf_pr_post_get_has($post_id, $post_type_q = null) {
  325. $post_type = get_post_type($post_id);
  326. $has = array_keys(wpcf_pr_get_has($post_type));
  327. $add = is_null($post_type_q) ? '&post_type=any' : '&post_type=' . $post_type_q;
  328. $posts = get_posts('numberposts=-1&post_status=null&meta_key=_wpcf_belongs_'
  329. . $post_type . '_id&meta_value=' . $post_id . $add);
  330. $results = array();
  331. foreach ($posts as $post) {
  332. if (!in_array($post->post_type, $has)) {
  333. continue;
  334. }
  335. $results[$post->post_type][] = $post;
  336. }
  337. return is_null($post_type_q) ? $results : array_shift($results);
  338. }
  339. /**
  340. * Gets posts that belongs to current post.
  341. *
  342. * @global type $post
  343. * @param type $post_type
  344. * @param type $args
  345. * @return type
  346. */
  347. function types_child_posts($post_type, $args = array()) {
  348. require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields.php';
  349. require_once WPCF_EMBEDDED_INC_ABSPATH . '/fields-post.php';
  350. global $post, $wp_post_types;
  351. // WP allows querying inactive post types
  352. if (!isset($wp_post_types[$post_type])
  353. || !$wp_post_types[$post_type]->publicly_queryable) {
  354. return array();
  355. }
  356. $defaults = array(
  357. 'post_type' => $post_type,
  358. 'numberposts' => -1,
  359. 'post_status' => null,
  360. 'meta_key' => '_wpcf_belongs_' . $post->post_type . '_id',
  361. 'meta_value' => $post->ID,
  362. 'suppress_filters' => false,
  363. );
  364. $args = wp_parse_args($args, $defaults);
  365. $args = apply_filters('types_child_posts_args', $args);
  366. $child_posts = get_posts($args);
  367. foreach ($child_posts as $child_post_key => $child_post) {
  368. if ($child_posts[$child_post_key]->post_status=='trash')
  369. {
  370. unset($child_posts[$child_post_key]);
  371. continue;
  372. }
  373. $child_posts[$child_post_key]->fields = array();
  374. $groups = wpcf_admin_post_get_post_groups_fields($child_post);
  375. foreach ($groups as $group) {
  376. if (!empty($group['fields'])) {
  377. // Process fields
  378. foreach ($group['fields'] as $k => $field) {
  379. $child_posts[$child_post_key]->fields[$k] = get_post_meta($child_post->ID,
  380. wpcf_types_get_meta_prefix($field) . $field['slug'],
  381. false /*true*/); // get all field instances
  382. // handle checkboxes which are one value serialized
  383. if ($field['type']=='checkboxes' && isset($child_posts[$child_post_key]->fields[$k][0]))
  384. $child_posts[$child_post_key]->fields[$k]=maybe_unserialize($child_posts[$child_post_key]->fields[$k][0]);
  385. }
  386. }
  387. }
  388. }
  389. return $child_posts;
  390. }
  391. /**
  392. * Gets settings.
  393. */
  394. function wpcf_get_settings($specific = false) {
  395. $defaults = array(
  396. 'add_resized_images_to_library' => 0,
  397. 'register_translations_on_import' => 1,
  398. 'images_remote' => 0,
  399. 'images_remote_cache_time' => '36',
  400. 'help_box' => 'by_types',
  401. );
  402. $settings = wp_parse_args(get_option('wpcf_settings', array()), $defaults);
  403. $settings = apply_filters('types_settings', $settings);
  404. if ($specific) {
  405. return isset($settings[$specific]) ? $settings[$specific] : false;
  406. }
  407. return $settings;
  408. }
  409. /**
  410. * Saves settings.
  411. */
  412. function wpcf_save_settings($settings) {
  413. update_option('wpcf_settings', $settings);
  414. }
  415. /**
  416. * Check if it can be repetitive
  417. * @param type $field
  418. * @return type
  419. */
  420. function wpcf_admin_can_be_repetitive($type) {
  421. return !in_array($type,
  422. array('checkbox', 'checkboxes', 'wysiwyg', 'radio', 'select'));
  423. }
  424. /**
  425. * Check if field is repetitive
  426. * @param type $type
  427. * @return type
  428. */
  429. function wpcf_admin_is_repetitive($field) {
  430. if (!isset($field['data']['repetitive']) || !isset($field['type'])) {
  431. return false;
  432. }
  433. $check = intval($field['data']['repetitive']);
  434. return !empty($check)
  435. && wpcf_admin_can_be_repetitive($field['type']);
  436. }
  437. /**
  438. * Returns unique ID.
  439. *
  440. * @staticvar array $cache
  441. * @param type $cache_key
  442. * @return type
  443. */
  444. function wpcf_unique_id($cache_key) {
  445. $cache_key = md5(strval($cache_key) . strval(time()));
  446. static $cache = array();
  447. if (!isset($cache[$cache_key])) {
  448. $cache[$cache_key] = 1;
  449. } else {
  450. $cache[$cache_key] += 1;
  451. }
  452. return $cache_key . '-' . $cache[$cache_key];
  453. }