functions-wordpress.class.php 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279
  1. <?php
  2. /**
  3. * @author ThemePunch <info@themepunch.com>
  4. * @link http://www.themepunch.com/
  5. * @copyright 2015 ThemePunch
  6. */
  7. if( !defined( 'ABSPATH') ) exit();
  8. class RevSliderFunctionsWP {
  9. public static $urlSite;
  10. public static $urlAdmin;
  11. const SORTBY_NONE = "none";
  12. const SORTBY_ID = "ID";
  13. const SORTBY_AUTHOR = "author";
  14. const SORTBY_TITLE = "title";
  15. const SORTBY_SLUG = "name";
  16. const SORTBY_DATE = "date";
  17. const SORTBY_LAST_MODIFIED = "modified";
  18. const SORTBY_RAND = "rand";
  19. const SORTBY_COMMENT_COUNT = "comment_count";
  20. const SORTBY_MENU_ORDER = "menu_order";
  21. const ORDER_DIRECTION_ASC = "ASC";
  22. const ORDER_DIRECTION_DESC = "DESC";
  23. const THUMB_SMALL = "thumbnail";
  24. const THUMB_MEDIUM = "medium";
  25. const THUMB_LARGE = "large";
  26. const THUMB_FULL = "full";
  27. const STATE_PUBLISHED = "publish";
  28. const STATE_DRAFT = "draft";
  29. /**
  30. *
  31. * init the static variables
  32. */
  33. public static function initStaticVars(){
  34. self::$urlAdmin = admin_url();
  35. if(substr(self::$urlAdmin, -1) != "/")
  36. self::$urlAdmin .= "/";
  37. }
  38. /**
  39. *
  40. * get sort by with the names
  41. */
  42. public static function getArrSortBy(){
  43. $arr = array();
  44. $arr[self::SORTBY_ID] = "Post ID";
  45. $arr[self::SORTBY_DATE] = "Date";
  46. $arr[self::SORTBY_TITLE] = "Title";
  47. $arr[self::SORTBY_SLUG] = "Slug";
  48. $arr[self::SORTBY_AUTHOR] = "Author";
  49. $arr[self::SORTBY_LAST_MODIFIED] = "Last Modified";
  50. $arr[self::SORTBY_COMMENT_COUNT] = "Number Of Comments";
  51. $arr[self::SORTBY_RAND] = "Random";
  52. $arr[self::SORTBY_NONE] = "Unsorted";
  53. $arr[self::SORTBY_MENU_ORDER] = "Custom Order";
  54. return($arr);
  55. }
  56. /**
  57. *
  58. * get array of sort direction
  59. */
  60. public static function getArrSortDirection(){
  61. $arr = array();
  62. $arr[self::ORDER_DIRECTION_DESC] = "Descending";
  63. $arr[self::ORDER_DIRECTION_ASC] = "Ascending";
  64. return($arr);
  65. }
  66. /**
  67. * get blog id
  68. */
  69. public static function getBlogID(){
  70. global $blog_id;
  71. return($blog_id);
  72. }
  73. /**
  74. *
  75. * get blog id
  76. */
  77. public static function isMultisite(){
  78. $isMultisite = is_multisite();
  79. return($isMultisite);
  80. }
  81. /**
  82. *
  83. * check if some db table exists
  84. */
  85. public static function isDBTableExists($tableName){
  86. global $wpdb;
  87. if(empty($tableName))
  88. RevSliderFunctions::throwError("Empty table name!!!");
  89. $sql = "show tables like '$tableName'";
  90. $table = $wpdb->get_var($sql);
  91. if($table == $tableName)
  92. return(true);
  93. return(false);
  94. }
  95. /**
  96. *
  97. * get wordpress base path
  98. */
  99. public static function getPathBase(){
  100. return ABSPATH;
  101. }
  102. /**
  103. *
  104. * get wp-content path
  105. */
  106. public static function getPathUploads(){
  107. global $wpdb;
  108. if(self::isMultisite()){
  109. if(!defined("BLOGUPLOADDIR")){
  110. $pathBase = self::getPathBase();
  111. //$pathContent = $pathBase."wp-content/uploads/";
  112. $pathContent = $pathBase."wp-content/uploads/sites/{$wpdb->blogid}/";
  113. }else
  114. $pathContent = BLOGUPLOADDIR;
  115. }else{
  116. $pathContent = WP_CONTENT_DIR;
  117. if(!empty($pathContent)){
  118. $pathContent .= "/";
  119. }
  120. else{
  121. $pathBase = self::getPathBase();
  122. $pathContent = $pathBase."wp-content/uploads/";
  123. }
  124. }
  125. return($pathContent);
  126. }
  127. /**
  128. *
  129. * get content url
  130. */
  131. public static function getUrlUploads(){
  132. if(self::isMultisite() == false){ //without multisite
  133. $baseUrl = content_url()."/";
  134. }
  135. else{ //for multisite
  136. $arrUploadData = wp_upload_dir();
  137. $baseUrl = $arrUploadData["baseurl"]."/";
  138. }
  139. return($baseUrl);
  140. }
  141. /**
  142. * Check if current user is administrator
  143. **/
  144. public static function isAdminUser(){
  145. return current_user_can('administrator');
  146. }
  147. /* Import media from url
  148. *
  149. * @param string $file_url URL of the existing file from the original site
  150. * @param int $folder_name The slidername will be used as folder name in import
  151. *
  152. * @return boolean True on success, false on failure
  153. */
  154. public static function import_media($file_url, $folder_name) {
  155. require_once(ABSPATH . 'wp-admin/includes/image.php');
  156. $ul_dir = wp_upload_dir();
  157. $artDir = 'revslider/';
  158. //if the directory doesn't exist, create it
  159. if(!file_exists($ul_dir['basedir'].'/'.$artDir)) mkdir($ul_dir['basedir'].'/'.$artDir);
  160. if(!file_exists($ul_dir['basedir'].'/'.$artDir.$folder_name)) mkdir($ul_dir['basedir'].'/'.$artDir.$folder_name);
  161. //rename the file... alternatively, you could explode on "/" and keep the original file name
  162. $filename = basename($file_url);
  163. //$siteurl = get_option('siteurl');
  164. if(@fclose(@fopen($file_url, "r"))){ //make sure the file actually exists
  165. $saveDir = $ul_dir['basedir'].'/'.$artDir.$folder_name.$filename;
  166. $atc_id = self::get_image_id_by_url($artDir.$folder_name.$filename);
  167. if($atc_id == false || $atc_id == NULL){
  168. copy($file_url, $saveDir);
  169. $file_info = getimagesize($saveDir);
  170. //create an array of attachment data to insert into wp_posts table
  171. $artdata = array(
  172. 'post_author' => 1,
  173. 'post_date' => current_time('mysql'),
  174. 'post_date_gmt' => current_time('mysql'),
  175. 'post_title' => $filename,
  176. 'post_status' => 'inherit',
  177. 'comment_status' => 'closed',
  178. 'ping_status' => 'closed',
  179. 'post_name' => sanitize_title_with_dashes(str_replace("_", "-", $filename)),
  180. 'post_modified' => current_time('mysql'),
  181. 'post_modified_gmt' => current_time('mysql'),
  182. 'post_parent' => '',
  183. 'post_type' => 'attachment',
  184. 'guid' => $ul_dir['baseurl'].'/'.$artDir.$folder_name.$filename,
  185. 'post_mime_type' => $file_info['mime'],
  186. 'post_excerpt' => '',
  187. 'post_content' => ''
  188. );
  189. //insert the database record
  190. $attach_id = wp_insert_attachment($artdata, $artDir.$folder_name.$filename);
  191. }else{
  192. $attach_id = $atc_id;
  193. }
  194. //generate metadata and thumbnails
  195. if($attach_data = wp_generate_attachment_metadata($attach_id, $saveDir)) wp_update_attachment_metadata($attach_id, $attach_data);
  196. if(!self::isMultisite()) $artDir = 'uploads/'.$artDir;
  197. return array("id" => $attach_id, "path" => $artDir.$folder_name.$filename);
  198. }else{
  199. return false;
  200. }
  201. }
  202. /**
  203. *
  204. * register widget (must be class)
  205. */
  206. public static function registerWidget($widgetName){
  207. add_action('widgets_init', array('RevSliderFunctionsWP', 'revSliderRegisterWidget'));
  208. }
  209. public static function revSliderRegisterWidget(){
  210. register_widget( 'RevSliderWidget' );
  211. }
  212. /**
  213. * get image relative path from image url (from upload)
  214. */
  215. public static function getImagePathFromURL($urlImage){
  216. $baseUrl = self::getUrlUploads();
  217. $pathImage = str_replace($baseUrl, "", $urlImage);
  218. return($pathImage);
  219. }
  220. /**
  221. * get image real path physical on disk from url
  222. */
  223. public static function getImageRealPathFromUrl($urlImage){
  224. $filepath = self::getImagePathFromURL($urlImage);
  225. $realPath = RevSliderFunctionsWP::getPathUploads().$filepath;
  226. return($realPath);
  227. }
  228. /**
  229. *
  230. * get image url from image path.
  231. */
  232. public static function getImageUrlFromPath($pathImage){
  233. //protect from absolute url
  234. $pathLower = strtolower($pathImage);
  235. if(strpos($pathLower, "http://") !== false || strpos($pathLower, "https://") !== false || strpos($pathLower, "www.") === 0)
  236. return($pathImage);
  237. $urlImage = self::getUrlUploads().$pathImage;
  238. return($urlImage);
  239. }
  240. /**
  241. *
  242. * get post categories list assoc - id / title
  243. */
  244. public static function getCategoriesAssoc($taxonomy = "category"){
  245. if(strpos($taxonomy,",") !== false){
  246. $arrTax = explode(",", $taxonomy);
  247. $arrCats = array();
  248. foreach($arrTax as $tax){
  249. $cats = self::getCategoriesAssoc($tax);
  250. $arrCats = array_merge($arrCats,$cats);
  251. }
  252. return($arrCats);
  253. }
  254. //$cats = get_terms("category");
  255. $args = array("taxonomy"=>$taxonomy);
  256. $cats = get_categories($args);
  257. $arrCats = array();
  258. foreach($cats as $cat){
  259. $numItems = $cat->count;
  260. $itemsName = "items";
  261. if($numItems == 1)
  262. $itemsName = "item";
  263. $title = $cat->name . " ($numItems $itemsName)";
  264. $id = $cat->cat_ID;
  265. $arrCats[$id] = $title;
  266. }
  267. return($arrCats);
  268. }
  269. /**
  270. *
  271. * return post type title from the post type
  272. */
  273. public static function getPostTypeTitle($postType){
  274. $objType = get_post_type_object($postType);
  275. if(empty($objType))
  276. return($postType);
  277. $title = $objType->labels->singular_name;
  278. return($title);
  279. }
  280. /**
  281. *
  282. * get post type taxomonies
  283. */
  284. public static function getPostTypeTaxomonies($postType){
  285. $arrTaxonomies = get_object_taxonomies(array( 'post_type' => $postType ), 'objects');
  286. $arrNames = array();
  287. foreach($arrTaxonomies as $key=>$objTax){
  288. $arrNames[$objTax->name] = $objTax->labels->name;
  289. }
  290. return($arrNames);
  291. }
  292. /**
  293. *
  294. * get post types taxonomies as string
  295. */
  296. public static function getPostTypeTaxonomiesString($postType){
  297. $arrTax = self::getPostTypeTaxomonies($postType);
  298. $strTax = "";
  299. foreach($arrTax as $name=>$title){
  300. if(!empty($strTax))
  301. $strTax .= ",";
  302. $strTax .= $name;
  303. }
  304. return($strTax);
  305. }
  306. /**
  307. *
  308. * get all the post types including custom ones
  309. * the put to top items will be always in top (they must be in the list)
  310. */
  311. public static function getPostTypesAssoc($arrPutToTop = array()){
  312. $arrBuiltIn = array(
  313. "post"=>"post",
  314. "page"=>"page",
  315. );
  316. $arrCustomTypes = get_post_types(array('_builtin' => false));
  317. //top items validation - add only items that in the customtypes list
  318. $arrPutToTopUpdated = array();
  319. foreach($arrPutToTop as $topItem){
  320. if(in_array($topItem, $arrCustomTypes) == true){
  321. $arrPutToTopUpdated[$topItem] = $topItem;
  322. unset($arrCustomTypes[$topItem]);
  323. }
  324. }
  325. $arrPostTypes = array_merge($arrPutToTopUpdated,$arrBuiltIn,$arrCustomTypes);
  326. //update label
  327. foreach($arrPostTypes as $key=>$type){
  328. $arrPostTypes[$key] = self::getPostTypeTitle($type);
  329. }
  330. return($arrPostTypes);
  331. }
  332. /**
  333. *
  334. * get the category data
  335. */
  336. public static function getCategoryData($catID){
  337. $catData = get_category($catID);
  338. if(empty($catData))
  339. return($catData);
  340. $catData = (array)$catData;
  341. return($catData);
  342. }
  343. /**
  344. *
  345. * get posts by coma saparated posts
  346. */
  347. public static function getPostsByIDs($strIDs, $slider_id, $is_gal, $additional = array()){
  348. if(is_string($strIDs)){
  349. $arr = explode(",",$strIDs);
  350. }else{
  351. $arr = $strIDs;
  352. }
  353. $query = array(
  354. 'post_type'=>"any",
  355. 'ignore_sticky_posts' => 1,
  356. 'post__in' => $arr
  357. );
  358. if($is_gal){
  359. $query['post_status'] = 'inherit';
  360. $query['orderby'] = 'post__in';
  361. }
  362. $query = array_merge($query, $additional);
  363. $query = apply_filters('revslider_get_posts', $query, $slider_id);
  364. $objQuery = new WP_Query($query);
  365. $arrPosts = $objQuery->posts;
  366. //dmp($query);dmp("num posts: ".count($arrPosts));exit();
  367. foreach($arrPosts as $key=>$post){
  368. if(method_exists($post, "to_array"))
  369. $arrPosts[$key] = $post->to_array();
  370. else
  371. $arrPosts[$key] = (array)$post;
  372. }
  373. return($arrPosts);
  374. }
  375. /**
  376. *
  377. * get posts by some category
  378. * could be multiple
  379. */
  380. public static function getPostsByCategory($slider_id,$catID,$sortBy = self::SORTBY_ID,$direction = self::ORDER_DIRECTION_DESC,$numPosts=-1,$postTypes="any",$taxonomies="category",$arrAddition = array(), $tax_addition = array()){
  381. //get post types
  382. if(strpos($postTypes,",") !== false){
  383. $postTypes = explode(",", $postTypes);
  384. if(array_search("any", $postTypes) !== false)
  385. $postTypes = "any";
  386. }
  387. if(empty($postTypes))
  388. $postTypes = "any";
  389. if(strpos($catID,",") !== false)
  390. $catID = explode(",",$catID);
  391. else
  392. $catID = array($catID);
  393. if(RevSliderWpml::isWpmlExists()){ //translate categories to languages
  394. $newcat = array();
  395. foreach($catID as $id){
  396. //$newcat[] = icl_object_id($id, 'category', true);
  397. $newcat[] = apply_filters( 'wpml_object_id', $id, 'category', true );
  398. }
  399. $catID = $newcat;
  400. }
  401. $query = array(
  402. 'order'=>$direction,
  403. 'ignore_sticky_posts' => 1,
  404. 'posts_per_page'=>$numPosts,
  405. 'showposts'=>$numPosts,
  406. 'post_type'=>$postTypes
  407. );
  408. //add sort by (could be by meta)
  409. if(strpos($sortBy, "meta_num_") === 0){
  410. $metaKey = str_replace("meta_num_", "", $sortBy);
  411. $query["orderby"] = "meta_value_num";
  412. $query["meta_key"] = $metaKey;
  413. }else
  414. if(strpos($sortBy, "meta_") === 0){
  415. $metaKey = str_replace("meta_", "", $sortBy);
  416. $query["orderby"] = "meta_value";
  417. $query["meta_key"] = $metaKey;
  418. }else
  419. $query["orderby"] = $sortBy;
  420. //get taxonomies array
  421. $arrTax = array();
  422. if(!empty($taxonomies)){
  423. $arrTax = explode(",", $taxonomies);
  424. }
  425. if(!empty($taxonomies)){
  426. $taxQuery = array();
  427. //add taxomonies to the query
  428. if(strpos($taxonomies,",") !== false){ //multiple taxomonies
  429. $taxonomies = explode(",",$taxonomies);
  430. foreach($taxonomies as $taxomony){
  431. $taxArray = array(
  432. 'taxonomy' => $taxomony,
  433. 'field' => 'id',
  434. 'terms' => $catID
  435. );
  436. $taxQuery[] = $taxArray;
  437. }
  438. }else{ //single taxomony
  439. $taxArray = array(
  440. 'taxonomy' => $taxonomies,
  441. 'field' => 'id',
  442. 'terms' => $catID
  443. );
  444. $taxQuery[] = $taxArray;
  445. }
  446. $taxQuery['relation'] = 'OR';
  447. $query['tax_query'] = $taxQuery;
  448. } //if exists taxanomies
  449. if(!empty($tax_addition)){
  450. if(!isset($query['tax_query'])) $query['tax_query'] = array();
  451. foreach($tax_addition as $tak => $tav){
  452. $query['tax_query'][] = $tav;
  453. }
  454. $query['tax_query']['relation'] = 'AND';
  455. }
  456. if(!empty($arrAddition))
  457. $query = array_merge($query, $arrAddition);
  458. $query = apply_filters('revslider_get_posts', $query, $slider_id);
  459. $objQuery = new WP_Query($query);
  460. $arrPosts = $objQuery->posts;
  461. foreach($arrPosts as $key=>$post){
  462. if(method_exists($post, "to_array"))
  463. $arrPost = $post->to_array();
  464. else
  465. $arrPost = (array)$post;
  466. $arrPostCats = self::getPostCategories($post, $arrTax);
  467. $arrPost["categories"] = $arrPostCats;
  468. $arrPosts[$key] = $arrPost;
  469. }
  470. return($arrPosts);
  471. }
  472. /**
  473. *
  474. * get post categories by postID and taxonomies
  475. * the postID can be post object or array too
  476. */
  477. public static function getPostCategories($postID,$arrTax){
  478. if(!is_numeric($postID)){
  479. $postID = (array)$postID;
  480. $postID = $postID["ID"];
  481. }
  482. $arrCats = wp_get_post_terms( $postID, $arrTax);
  483. $arrCats = RevSliderFunctions::convertStdClassToArray($arrCats);
  484. return($arrCats);
  485. }
  486. /**
  487. *
  488. * get single post
  489. */
  490. public static function getPost($postID){
  491. $post = get_post($postID);
  492. if(empty($post))
  493. RevSliderFunctions::throwError("Post with id: $postID not found");
  494. $arrPost = $post->to_array();
  495. return($arrPost);
  496. }
  497. /**
  498. *
  499. * update post state
  500. */
  501. public static function updatePostState($postID,$state){
  502. $arrUpdate = array();
  503. $arrUpdate["ID"] = $postID;
  504. $arrUpdate["post_status"] = $state;
  505. wp_update_post($arrUpdate);
  506. }
  507. /**
  508. *
  509. * update post menu order
  510. */
  511. public static function updatePostOrder($postID,$order){
  512. $arrUpdate = array();
  513. $arrUpdate["ID"] = $postID;
  514. $arrUpdate["menu_order"] = $order;
  515. wp_update_post($arrUpdate);
  516. }
  517. /**
  518. *
  519. * get url of post thumbnail
  520. */
  521. public static function getUrlPostImage($postID,$size = self::THUMB_FULL){
  522. $post_thumbnail_id = get_post_thumbnail_id( $postID );
  523. if(empty($post_thumbnail_id))
  524. return("");
  525. $arrImage = wp_get_attachment_image_src($post_thumbnail_id,$size);
  526. if(empty($arrImage))
  527. return("");
  528. $urlImage = $arrImage[0];
  529. return($urlImage);
  530. }
  531. /**
  532. *
  533. * get post thumb id from post id
  534. */
  535. public static function getPostThumbID($postID){
  536. $thumbID = get_post_thumbnail_id( $postID );
  537. return($thumbID);
  538. }
  539. /**
  540. *
  541. * get attachment image array by id and size
  542. */
  543. public static function getAttachmentImage($thumbID,$size = self::THUMB_FULL){
  544. $arrImage = wp_get_attachment_image_src($thumbID,$size);
  545. if(empty($arrImage))
  546. return(false);
  547. $output = array();
  548. $output["url"] = RevSliderFunctions::getVal($arrImage, 0);
  549. $output["width"] = RevSliderFunctions::getVal($arrImage, 1);
  550. $output["height"] = RevSliderFunctions::getVal($arrImage, 2);
  551. return($output);
  552. }
  553. /**
  554. *
  555. * get attachment image url
  556. */
  557. public static function getUrlAttachmentImage($thumbID,$size = self::THUMB_FULL){
  558. $arrImage = wp_get_attachment_image_src($thumbID,$size);
  559. if(empty($arrImage))
  560. return(false);
  561. $url = RevSliderFunctions::getVal($arrImage, 0);
  562. return($url);
  563. }
  564. /**
  565. *
  566. * get link of edit slides by category id
  567. */
  568. public static function getUrlSlidesEditByCatID($catID){
  569. $url = self::$urlAdmin;
  570. $url .= "edit.php?s&post_status=all&post_type=post&action=-1&m=0&cat=".$catID."&paged=1&mode=list&action2=-1";
  571. return($url);
  572. }
  573. /**
  574. *
  575. * get edit post url
  576. */
  577. public static function getUrlEditPost($postID){
  578. $url = self::$urlAdmin;
  579. $url .= "post.php?post=".$postID."&action=edit";
  580. return($url);
  581. }
  582. /**
  583. *
  584. * get new post url
  585. */
  586. public static function getUrlNewPost(){
  587. $url = self::$urlAdmin;
  588. $url .= "post-new.php";
  589. return($url);
  590. }
  591. /**
  592. *
  593. * delete post
  594. */
  595. public static function deletePost($postID){
  596. $success = wp_delete_post($postID,false);
  597. if($success == false)
  598. RevSliderFunctions::throwError("Could not delete post: $postID");
  599. }
  600. /**
  601. *
  602. * update post thumbnail
  603. */
  604. public static function updatePostThumbnail($postID,$thumbID){
  605. set_post_thumbnail($postID, $thumbID);
  606. }
  607. /**
  608. *
  609. * get intro from content
  610. */
  611. public static function getIntroFromContent($text){
  612. $intro = "";
  613. if(!empty($text)){
  614. $arrExtended = get_extended($text);
  615. $intro = RevSliderFunctions::getVal($arrExtended, "main");
  616. /*
  617. if(strlen($text) != strlen($intro))
  618. $intro .= "...";
  619. */
  620. }
  621. return($intro);
  622. }
  623. /**
  624. *
  625. * get excerpt from post id
  626. */
  627. public static function getExcerptById($postID, $limit=55){
  628. $post = get_post($postID);
  629. $excerpt = $post->post_excerpt;
  630. $excerpt = trim($excerpt);
  631. $excerpt = trim($excerpt);
  632. if(empty($excerpt))
  633. $excerpt = $post->post_content;
  634. $excerpt = strip_tags($excerpt,"<b><br><br/><i><strong><small>");
  635. $excerpt = RevSliderFunctions::getTextIntro($excerpt, $limit);
  636. return apply_filters('revslider_getExcerptById', $excerpt, $post, $limit);
  637. }
  638. /**
  639. *
  640. * get user display name from user id
  641. */
  642. public static function getUserDisplayName($userID){
  643. $displayName = get_the_author_meta('display_name', $userID);
  644. return($displayName);
  645. }
  646. /**
  647. *
  648. * get user avatar from user id
  649. */
  650. public static function getUserAvatarUrl($userID){
  651. $avatar = get_avatar_url($userID,array("size"=>"80"));
  652. return($avatar);
  653. }
  654. /**
  655. *
  656. * get user posts page from user id
  657. */
  658. public static function getUserPostsPage($userID){
  659. $link = get_author_posts_url($userID);
  660. return($link);
  661. }
  662. /**
  663. *
  664. * get user page from user id
  665. */
  666. public static function getUserPage($userID){
  667. $curauth = get_user_by('ID', $userID);
  668. $user_url = $curauth->user_url;
  669. return($user_url);
  670. }
  671. /**
  672. *
  673. * get categories by id's
  674. */
  675. public static function getCategoriesByIDs($arrIDs,$strTax = null){
  676. if(empty($arrIDs))
  677. return(array());
  678. if(is_string($arrIDs))
  679. $strIDs = $arrIDs;
  680. else
  681. $strIDs = implode(",", $arrIDs);
  682. $args = array();
  683. $args["include"] = $strIDs;
  684. if(!empty($strTax)){
  685. if(is_string($strTax))
  686. $strTax = explode(",",$strTax);
  687. $args["taxonomy"] = $strTax;
  688. }
  689. $arrCats = get_categories( $args );
  690. if(!empty($arrCats))
  691. $arrCats = RevSliderFunctions::convertStdClassToArray($arrCats);
  692. return($arrCats);
  693. }
  694. /**
  695. *
  696. * get categories short
  697. */
  698. public static function getCategoriesByIDsShort($arrIDs,$strTax = null){
  699. $arrCats = self::getCategoriesByIDs($arrIDs,$strTax);
  700. $arrNew = array();
  701. foreach($arrCats as $cat){
  702. $catID = $cat["term_id"];
  703. $catName = $cat["name"];
  704. $arrNew[$catID] = $catName;
  705. }
  706. return($arrNew);
  707. }
  708. /**
  709. * get categories list, copy the code from default wp functions
  710. */
  711. public static function getCategoriesHtmlList($catIDs,$strTax = null){
  712. global $wp_rewrite;
  713. //$catList = get_the_category_list( ",", "", $postID );
  714. $categories = self::getCategoriesByIDs($catIDs,$strTax);
  715. $arrErrors = RevSliderFunctions::getVal($categories, "errors");
  716. if(!empty($arrErrors)){
  717. foreach($arrErrors as $key=>$arr){
  718. $strErrors = implode($arr,",");
  719. }
  720. RevSliderFunctions::throwError("getCategoriesHtmlList error: ".$strErrors);
  721. }
  722. $rel = ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) ? 'rel="category tag"' : 'rel="category"';
  723. $separator = ',';
  724. $thelist = '';
  725. $i = 0;
  726. foreach ( $categories as $category ) {
  727. if(is_object($category))
  728. $category = (array)$category;
  729. if ( 0 < $i )
  730. $thelist .= $separator;
  731. $catID = $category["term_id"];
  732. $link = get_category_link($catID);
  733. $catName = $category["name"];
  734. if(!empty($link))
  735. $thelist .= '<a href="' . esc_url( $link ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s", 'revslider'), $category["name"] ) ) . '" ' . $rel . '>' . $catName.'</a>';
  736. else
  737. $thelist .= $catName;
  738. ++$i;
  739. }
  740. return $thelist;
  741. }
  742. /**
  743. *
  744. * get post tags html list
  745. */
  746. public static function getTagsHtmlList($postID){
  747. $tagList = get_the_tag_list("",",","",$postID);
  748. return($tagList);
  749. }
  750. /**
  751. *
  752. * convert date to the date format that the user chose.
  753. */
  754. public static function convertPostDate($date, $with_time = false){
  755. if(empty($date))
  756. return($date);
  757. if($with_time){
  758. $date = date_i18n(get_option('date_format').' '.get_option('time_format'), strtotime($date));
  759. }else{
  760. $date = date_i18n(get_option('date_format'), strtotime($date));
  761. }
  762. return($date);
  763. }
  764. /**
  765. *
  766. * get assoc list of the taxonomies
  767. */
  768. public static function getTaxonomiesAssoc(){
  769. $arr = get_taxonomies();
  770. unset($arr["post_tag"]);
  771. unset($arr["nav_menu"]);
  772. unset($arr["link_category"]);
  773. unset($arr["post_format"]);
  774. return($arr);
  775. }
  776. /**
  777. *
  778. * get post types array with taxomonies
  779. */
  780. public static function getPostTypesWithTaxomonies(){
  781. $arrPostTypes = self::getPostTypesAssoc();
  782. foreach($arrPostTypes as $postType=>$title){
  783. $arrTaxomonies = self::getPostTypeTaxomonies($postType);
  784. $arrPostTypes[$postType] = $arrTaxomonies;
  785. }
  786. return($arrPostTypes);
  787. }
  788. /**
  789. *
  790. * get array of post types with categories (the taxonomies is between).
  791. * get only those taxomonies that have some categories in it.
  792. */
  793. public static function getPostTypesWithCats(){
  794. $arrPostTypes = self::getPostTypesWithTaxomonies();
  795. $arrPostTypesOutput = array();
  796. foreach($arrPostTypes as $name=>$arrTax){
  797. $arrTaxOutput = array();
  798. foreach($arrTax as $taxName=>$taxTitle){
  799. $cats = self::getCategoriesAssoc($taxName);
  800. if(!empty($cats))
  801. $arrTaxOutput[] = array(
  802. "name"=>$taxName,
  803. "title"=>$taxTitle,
  804. "cats"=>$cats);
  805. }
  806. $arrPostTypesOutput[$name] = $arrTaxOutput;
  807. }
  808. return($arrPostTypesOutput);
  809. }
  810. /**
  811. *
  812. * get array of all taxonomies with categories.
  813. */
  814. public static function getTaxonomiesWithCats(){
  815. $arrTax = self::getTaxonomiesAssoc();
  816. $arrTaxNew = array();
  817. foreach($arrTax as $key=>$value){
  818. $arrItem = array();
  819. $arrItem["name"] = $key;
  820. $arrItem["title"] = $value;
  821. $arrItem["cats"] = self::getCategoriesAssoc($key);
  822. $arrTaxNew[$key] = $arrItem;
  823. }
  824. return($arrTaxNew);
  825. }
  826. /**
  827. *
  828. * get content url
  829. */
  830. public static function getUrlContent(){
  831. if(self::isMultisite() == false){ //without multisite
  832. $baseUrl = content_url()."/";
  833. }
  834. else{ //for multisite
  835. $arrUploadData = wp_upload_dir();
  836. $baseUrl = $arrUploadData["baseurl"]."/";
  837. }
  838. if(is_ssl()){
  839. $baseUrl = str_replace("http://", "https://", $baseUrl);
  840. }
  841. return($baseUrl);
  842. }
  843. /**
  844. *
  845. * get wp-content path
  846. */
  847. public static function getPathContent(){
  848. if(self::isMultisite()){
  849. if(!defined("BLOGUPLOADDIR")){
  850. $pathBase = self::getPathBase();
  851. $pathContent = $pathBase."wp-content/";
  852. }else
  853. $pathContent = BLOGUPLOADDIR;
  854. }else{
  855. $pathContent = WP_CONTENT_DIR;
  856. if(!empty($pathContent)){
  857. $pathContent .= "/";
  858. }
  859. else{
  860. $pathBase = self::getPathBase();
  861. $pathContent = $pathBase."wp-content/";
  862. }
  863. }
  864. return($pathContent);
  865. }
  866. /**
  867. *
  868. * get cats and taxanomies data from the category id's
  869. */
  870. public static function getCatAndTaxData($catIDs){
  871. if(is_string($catIDs)){
  872. $catIDs = trim($catIDs);
  873. if(empty($catIDs))
  874. return(array("tax"=>"","cats"=>""));
  875. $catIDs = explode(",", $catIDs);
  876. }
  877. $strCats = "";
  878. $arrTax = array();
  879. foreach($catIDs as $cat){
  880. if(strpos($cat,"option_disabled") === 0)
  881. continue;
  882. $pos = strrpos($cat,"_");
  883. if($pos === false)
  884. RevSliderFunctions::throwError("The category is in wrong format");
  885. $taxName = substr($cat,0,$pos);
  886. $catID = substr($cat,$pos+1,strlen($cat)-$pos-1);
  887. $arrTax[$taxName] = $taxName;
  888. if(!empty($strCats))
  889. $strCats .= ",";
  890. $strCats .= $catID;
  891. }
  892. $strTax = "";
  893. foreach($arrTax as $taxName){
  894. if(!empty($strTax))
  895. $strTax .= ",";
  896. $strTax .= $taxName;
  897. }
  898. $output = array("tax"=>$strTax,"cats"=>$strCats);
  899. return($output);
  900. }
  901. /**
  902. *
  903. * get current language code
  904. */
  905. public static function getCurrentLangCode(){
  906. $langTag = ICL_LANGUAGE_CODE;
  907. return($langTag);
  908. }
  909. /**
  910. *
  911. * check the current post for the existence of a short code
  912. */
  913. public static function hasShortcode($shortcode = '') {
  914. if(!is_singular())
  915. return false;
  916. $post = get_post(get_the_ID());
  917. $found = false;
  918. if (empty($shortcode))
  919. return $found;
  920. if (stripos($post->post_content, '[' . $shortcode) !== false )
  921. $found = true;
  922. return $found;
  923. }
  924. /**
  925. * Check if shortcodes exists in the content
  926. * @since: 5.0
  927. */
  928. public static function check_for_shortcodes($mid_content){
  929. if($mid_content !== null){
  930. if(has_shortcode($mid_content, 'gallery')){
  931. preg_match('/\[gallery.*ids=.(.*).\]/', $mid_content, $img_ids);
  932. if(isset($img_ids[1])){
  933. if($img_ids[1] !== '') return explode(',', $img_ids[1]);
  934. }
  935. }
  936. }
  937. return false;
  938. }
  939. /**
  940. * retrieve the image id from the given image url
  941. * @since: 5.0
  942. */
  943. public static function get_image_id_by_url($image_url) {
  944. global $wpdb;
  945. $attachment_id = 0;
  946. if(function_exists('attachment_url_to_postid')){
  947. $attachment_id = attachment_url_to_postid($image_url); //0 if failed
  948. }
  949. if ( 0 == $attachment_id ){ //try to get it old school way
  950. //for WP < 4.0.0
  951. $attachment_id = false;
  952. // If there is no url, return.
  953. if ( '' == $image_url )
  954. return;
  955. // Get the upload directory paths
  956. $upload_dir_paths = wp_upload_dir();
  957. // Make sure the upload path base directory exists in the attachment URL, to verify that we're working with a media library image
  958. if ( false !== strpos( $image_url, $upload_dir_paths['baseurl'] ) ) {
  959. // If this is the URL of an auto-generated thumbnail, get the URL of the original image
  960. $image_url = preg_replace( '/-\d+x\d+(?=\.(jpg|jpeg|png|gif)$)/i', '', $image_url );
  961. // Remove the upload path base directory from the attachment URL
  962. $image_url = str_replace( $upload_dir_paths['baseurl'] . '/', '', $image_url );
  963. // Finally, run a custom database query to get the attachment ID from the modified attachment URL
  964. $attachment_id = $wpdb->get_var( $wpdb->prepare( "SELECT wposts.ID FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta WHERE wposts.ID = wpostmeta.post_id AND wpostmeta.meta_key = '_wp_attached_file' AND wpostmeta.meta_value = '%s' AND wposts.post_type = 'attachment'", $image_url ) );
  965. }
  966. }
  967. return $attachment_id;
  968. }
  969. public static function update_option($handle, $value, $autoload = 'on'){ //on is on, false is 'off'
  970. if(!add_option($handle, $value, '', $autoload)){ //returns false if option is not existing
  971. delete_option($handle);
  972. }
  973. add_option($handle, $value, '', $autoload);
  974. }
  975. } //end of the class
  976. //init the static vars
  977. RevSliderFunctionsWP::initStaticVars();
  978. /**
  979. * old classname extends new one (old classnames will be obsolete soon)
  980. * @since: 5.0
  981. **/
  982. class UniteFunctionsWPRev extends RevSliderFunctionsWP {}
  983. ?>