class-vc-settings-presets.php 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. die( '-1' );
  4. }
  5. /**
  6. * Collection of static methods for work with settings presets
  7. *
  8. * @since 4.8
  9. */
  10. class Vc_Settings_Preset {
  11. /**
  12. * Get default preset id for specific shortcode
  13. *
  14. * @since 4.7
  15. *
  16. * @param string $shortcode_name
  17. *
  18. * @return mixed int|null
  19. */
  20. public static function getDefaultSettingsPresetId( $shortcode_name = null ) {
  21. if ( ! $shortcode_name ) {
  22. return null;
  23. }
  24. $args = array(
  25. 'post_type' => 'vc_settings_preset',
  26. 'post_mime_type' => self::constructShortcodeMimeType( $shortcode_name ),
  27. 'posts_per_page' => - 1,
  28. 'meta_key' => '_vc_default',
  29. 'meta_value' => true,
  30. );
  31. $posts = get_posts( $args );
  32. if ( $posts ) {
  33. $default_id = $posts[0]->ID;
  34. } else {
  35. // check for vendor presets
  36. $default_id = vc_vendor_preset()->getDefaultId( $shortcode_name );
  37. }
  38. return $default_id;
  39. }
  40. /**
  41. * Set existing preset as default
  42. *
  43. * If this is vendor preset, clone it and set new one as default
  44. *
  45. * @param int $id If falsy, no default will be set
  46. * @param string $shortcode_name
  47. *
  48. * @return boolean
  49. *
  50. * @since 4.7
  51. */
  52. public static function setAsDefaultSettingsPreset( $id, $shortcode_name ) {
  53. $post_id = self::getDefaultSettingsPresetId( $shortcode_name );
  54. if ( $post_id ) {
  55. delete_post_meta( $post_id, '_vc_default' );
  56. }
  57. if ( $id ) {
  58. if ( is_numeric( $id ) ) {
  59. // user preset
  60. update_post_meta( $id, '_vc_default', true );
  61. } else {
  62. // vendor preset
  63. $preset = vc_vendor_preset()->get( $id );
  64. if ( ! $preset || $shortcode_name !== $preset['shortcode'] ) {
  65. return false;
  66. }
  67. self::saveSettingsPreset( $preset['shortcode'], $preset['title'], wp_json_encode( $preset['params'] ), true );
  68. }
  69. }
  70. return true;
  71. }
  72. /**
  73. * Get mime type for specific shortcode
  74. *
  75. * @since 4.7
  76. *
  77. * @param $shortcode_name
  78. *
  79. * @return string
  80. */
  81. public static function constructShortcodeMimeType( $shortcode_name ) {
  82. return 'vc-settings-preset/' . str_replace( '_', '-', $shortcode_name );
  83. }
  84. /**
  85. * Get shortcode name from post's mime type
  86. *
  87. * @since 4.7
  88. *
  89. * @param string $post_mime_type
  90. *
  91. * @return string
  92. */
  93. public static function extractShortcodeMimeType( $post_mime_type ) {
  94. $chunks = explode( '/', $post_mime_type );
  95. if ( 2 !== count( $chunks ) ) {
  96. return '';
  97. }
  98. return str_replace( '-', '_', $chunks[1] );
  99. }
  100. /**
  101. * Get all presets
  102. *
  103. * @since 5.2
  104. *
  105. * @return array E.g. array(preset_id => value, preset_id => value, ...)
  106. */
  107. public static function listAllPresets() {
  108. $list = array();
  109. $args = array(
  110. 'post_type' => 'vc_settings_preset',
  111. 'posts_per_page' => - 1,
  112. );
  113. // user presets
  114. $posts = get_posts( $args );
  115. foreach ( $posts as $post ) {
  116. $shortcode_name = self::extractShortcodeMimeType( $post->post_mime_type );
  117. $list[ $post->ID ] = (array) json_decode( $post->post_content );
  118. }
  119. // vendor presets
  120. $presets = self::listDefaultVendorSettingsPresets();
  121. foreach ( $presets as $shortcode => $params ) {
  122. if ( ! isset( $list[ $shortcode ] ) ) {
  123. $list[ $shortcode ] = $params;
  124. }
  125. }
  126. return $list;
  127. }
  128. /**
  129. * Get all default presets
  130. *
  131. * @since 4.7
  132. *
  133. * @return array E.g. array(shortcode_name => value, shortcode_name => value, ...)
  134. */
  135. public static function listDefaultSettingsPresets() {
  136. $list = array();
  137. $args = array(
  138. 'post_type' => 'vc_settings_preset',
  139. 'posts_per_page' => - 1,
  140. 'meta_key' => '_vc_default',
  141. 'meta_value' => true,
  142. );
  143. // user presets
  144. $posts = get_posts( $args );
  145. foreach ( $posts as $post ) {
  146. $shortcode_name = self::extractShortcodeMimeType( $post->post_mime_type );
  147. $list[ $shortcode_name ] = (array) json_decode( $post->post_content );
  148. }
  149. // vendor presets
  150. $presets = self::listDefaultVendorSettingsPresets();
  151. foreach ( $presets as $shortcode => $params ) {
  152. if ( ! isset( $list[ $shortcode ] ) ) {
  153. $list[ $shortcode ] = $params;
  154. }
  155. }
  156. return $list;
  157. }
  158. /**
  159. * Get all default vendor presets
  160. *
  161. * @since 4.8
  162. *
  163. * @return array E.g. array(shortcode_name => value, shortcode_name => value, ...)
  164. */
  165. public static function listDefaultVendorSettingsPresets() {
  166. $list = array();
  167. $presets = vc_vendor_preset()->getDefaults();
  168. foreach ( $presets as $id => $preset ) {
  169. $list[ $preset['shortcode'] ] = $preset['params'];
  170. }
  171. return $list;
  172. }
  173. /**
  174. * Save shortcode preset
  175. *
  176. * @since 4.7
  177. *
  178. * @param string $shortcode_name
  179. * @param string $title
  180. * @param string $content
  181. * @param boolean $is_default
  182. *
  183. * @return mixed int|false Post ID
  184. */
  185. public static function saveSettingsPreset( $shortcode_name, $title, $content, $is_default = false ) {
  186. $post_id = wp_insert_post( array(
  187. 'post_title' => $title,
  188. 'post_content' => $content,
  189. 'post_status' => 'publish',
  190. 'post_type' => 'vc_settings_preset',
  191. 'post_mime_type' => self::constructShortcodeMimeType( $shortcode_name ),
  192. ), false );
  193. if ( $post_id && $is_default ) {
  194. self::setAsDefaultSettingsPreset( $post_id, $shortcode_name );
  195. }
  196. return $post_id;
  197. }
  198. /**
  199. * Get list of all presets for specific shortcode
  200. *
  201. * @since 4.7
  202. *
  203. * @param string $shortcode_name
  204. *
  205. * @return array E.g. array(id1 => title1, id2 => title2, ...)
  206. */
  207. public static function listSettingsPresets( $shortcode_name = null ) {
  208. $list = array();
  209. if ( ! $shortcode_name ) {
  210. return $list;
  211. }
  212. $args = array(
  213. 'post_type' => 'vc_settings_preset',
  214. 'orderby' => array( 'post_date' => 'DESC' ),
  215. 'posts_per_page' => - 1,
  216. 'post_mime_type' => self::constructShortcodeMimeType( $shortcode_name ),
  217. );
  218. $posts = get_posts( $args );
  219. foreach ( $posts as $post ) {
  220. $list[ $post->ID ] = $post->post_title;
  221. }
  222. return $list;
  223. }
  224. /**
  225. * Get list of all vendor presets for specific shortcode
  226. *
  227. * @since 4.8
  228. *
  229. * @param string $shortcode_name
  230. *
  231. * @return array E.g. array(id1 => title1, id2 => title2, ...)
  232. */
  233. public static function listVendorSettingsPresets( $shortcode_name = null ) {
  234. $list = array();
  235. if ( ! $shortcode_name ) {
  236. return $list;
  237. }
  238. $presets = vc_vendor_preset()->getAll( $shortcode_name );
  239. foreach ( $presets as $id => $preset ) {
  240. $list[ $id ] = $preset['title'];
  241. }
  242. return $list;
  243. }
  244. /**
  245. * Get specific shortcode preset
  246. *
  247. * @since 4.7
  248. *
  249. * @param mixed $id Can be int (user preset) or string (vendor preset)
  250. * @param bool $array If true, return array instead of string
  251. *
  252. * @return mixed string?array Post content
  253. */
  254. public static function getSettingsPreset( $id, $array = false ) {
  255. if ( is_numeric( $id ) ) {
  256. // user preset
  257. $post = get_post( $id );
  258. if ( ! $post ) {
  259. return false;
  260. }
  261. $params = $array ? (array) json_decode( $post->post_content ) : $post->post_content;
  262. } else {
  263. // vendor preset
  264. $preset = vc_vendor_preset()->get( $id );
  265. if ( ! $preset ) {
  266. return false;
  267. }
  268. $params = $preset['params'];
  269. }
  270. return $params;
  271. }
  272. /**
  273. * Delete shortcode preset
  274. *
  275. * @since 4.7
  276. *
  277. * @param int $post_id Post must be of type 'vc_settings_preset'
  278. *
  279. * @return bool
  280. */
  281. public static function deleteSettingsPreset( $post_id ) {
  282. $post = get_post( $post_id );
  283. if ( ! $post || 'vc_settings_preset' !== $post->post_type ) {
  284. return false;
  285. }
  286. return (bool) wp_delete_post( $post_id, true );
  287. }
  288. /**
  289. * Return rendered popup menu
  290. *
  291. * @since 4.7
  292. *
  293. * @param string $shortcode_name
  294. *
  295. * @return string
  296. */
  297. public static function getRenderedSettingsPresetPopup( $shortcode_name ) {
  298. $list_vendor_presets = self::listVendorSettingsPresets( $shortcode_name );
  299. $list_presets = self::listSettingsPresets( $shortcode_name );
  300. $default_id = self::getDefaultSettingsPresetId( $shortcode_name );
  301. if ( ! $default_id ) {
  302. $default_id = vc_vendor_preset()->getDefaultId( $shortcode_name );
  303. }
  304. ob_start();
  305. vc_include_template( apply_filters( 'vc_render_settings_preset_popup', 'editors/partials/settings_presets_popup.tpl.php' ), array(
  306. 'list_presets' => array(
  307. $list_presets,
  308. $list_vendor_presets,
  309. ),
  310. 'default_id' => $default_id,
  311. 'shortcode_name' => $shortcode_name,
  312. ) );
  313. $html = ob_get_clean();
  314. return $html;
  315. }
  316. /**
  317. * @param $shortcodes
  318. *
  319. * @return array
  320. * @throws \Exception
  321. */
  322. public static function addVcPresetsToShortcodes( $shortcodes ) {
  323. if ( vc_user_access()->part( 'presets' )->can()->get() ) {
  324. $shortcodesAndPresets = array();
  325. foreach ( $shortcodes as $shortcode ) {
  326. $presets = self::listSettingsPresets( $shortcode['base'] );
  327. $shortcodesAndPresets[ $shortcode['base'] ] = $shortcode;
  328. if ( ! empty( $presets ) ) {
  329. foreach ( $presets as $presetId => $preset ) {
  330. $shortcodesAndPresets[ $presetId ] = array(
  331. 'name' => $preset,
  332. 'base' => $shortcode['base'],
  333. 'description' => $shortcode['description'],
  334. 'presetId' => $presetId,
  335. '_category_ids' => array( '_my_elements_' ),
  336. );
  337. if ( isset( $shortcode['icon'] ) ) {
  338. $shortcodesAndPresets[ $presetId ]['icon'] = $shortcode['icon'];
  339. }
  340. }
  341. }
  342. }
  343. return $shortcodesAndPresets;
  344. }
  345. return $shortcodes;
  346. }
  347. /**
  348. * @param $category
  349. *
  350. * @return array
  351. */
  352. public static function addPresetCategory( $category ) {
  353. $presetCategory = (array) '_my_elements_';
  354. $category = array_merge( $presetCategory, $category );
  355. return $category;
  356. }
  357. }