functions-wordpress.class.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269
  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()){
  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($arrAddition))
  450. $query = array_merge($query, $arrAddition);
  451. $query = apply_filters('revslider_get_posts', $query, $slider_id);
  452. $objQuery = new WP_Query($query);
  453. $arrPosts = $objQuery->posts;
  454. foreach($arrPosts as $key=>$post){
  455. if(method_exists($post, "to_array"))
  456. $arrPost = $post->to_array();
  457. else
  458. $arrPost = (array)$post;
  459. $arrPostCats = self::getPostCategories($post, $arrTax);
  460. $arrPost["categories"] = $arrPostCats;
  461. $arrPosts[$key] = $arrPost;
  462. }
  463. return($arrPosts);
  464. }
  465. /**
  466. *
  467. * get post categories by postID and taxonomies
  468. * the postID can be post object or array too
  469. */
  470. public static function getPostCategories($postID,$arrTax){
  471. if(!is_numeric($postID)){
  472. $postID = (array)$postID;
  473. $postID = $postID["ID"];
  474. }
  475. $arrCats = wp_get_post_terms( $postID, $arrTax);
  476. $arrCats = RevSliderFunctions::convertStdClassToArray($arrCats);
  477. return($arrCats);
  478. }
  479. /**
  480. *
  481. * get single post
  482. */
  483. public static function getPost($postID){
  484. $post = get_post($postID);
  485. if(empty($post))
  486. RevSliderFunctions::throwError("Post with id: $postID not found");
  487. $arrPost = $post->to_array();
  488. return($arrPost);
  489. }
  490. /**
  491. *
  492. * update post state
  493. */
  494. public static function updatePostState($postID,$state){
  495. $arrUpdate = array();
  496. $arrUpdate["ID"] = $postID;
  497. $arrUpdate["post_status"] = $state;
  498. wp_update_post($arrUpdate);
  499. }
  500. /**
  501. *
  502. * update post menu order
  503. */
  504. public static function updatePostOrder($postID,$order){
  505. $arrUpdate = array();
  506. $arrUpdate["ID"] = $postID;
  507. $arrUpdate["menu_order"] = $order;
  508. wp_update_post($arrUpdate);
  509. }
  510. /**
  511. *
  512. * get url of post thumbnail
  513. */
  514. public static function getUrlPostImage($postID,$size = self::THUMB_FULL){
  515. $post_thumbnail_id = get_post_thumbnail_id( $postID );
  516. if(empty($post_thumbnail_id))
  517. return("");
  518. $arrImage = wp_get_attachment_image_src($post_thumbnail_id,$size);
  519. if(empty($arrImage))
  520. return("");
  521. $urlImage = $arrImage[0];
  522. return($urlImage);
  523. }
  524. /**
  525. *
  526. * get post thumb id from post id
  527. */
  528. public static function getPostThumbID($postID){
  529. $thumbID = get_post_thumbnail_id( $postID );
  530. return($thumbID);
  531. }
  532. /**
  533. *
  534. * get attachment image array by id and size
  535. */
  536. public static function getAttachmentImage($thumbID,$size = self::THUMB_FULL){
  537. $arrImage = wp_get_attachment_image_src($thumbID,$size);
  538. if(empty($arrImage))
  539. return(false);
  540. $output = array();
  541. $output["url"] = RevSliderFunctions::getVal($arrImage, 0);
  542. $output["width"] = RevSliderFunctions::getVal($arrImage, 1);
  543. $output["height"] = RevSliderFunctions::getVal($arrImage, 2);
  544. return($output);
  545. }
  546. /**
  547. *
  548. * get attachment image url
  549. */
  550. public static function getUrlAttachmentImage($thumbID,$size = self::THUMB_FULL){
  551. $arrImage = wp_get_attachment_image_src($thumbID,$size);
  552. if(empty($arrImage))
  553. return(false);
  554. $url = RevSliderFunctions::getVal($arrImage, 0);
  555. return($url);
  556. }
  557. /**
  558. *
  559. * get link of edit slides by category id
  560. */
  561. public static function getUrlSlidesEditByCatID($catID){
  562. $url = self::$urlAdmin;
  563. $url .= "edit.php?s&post_status=all&post_type=post&action=-1&m=0&cat=".$catID."&paged=1&mode=list&action2=-1";
  564. return($url);
  565. }
  566. /**
  567. *
  568. * get edit post url
  569. */
  570. public static function getUrlEditPost($postID){
  571. $url = self::$urlAdmin;
  572. $url .= "post.php?post=".$postID."&action=edit";
  573. return($url);
  574. }
  575. /**
  576. *
  577. * get new post url
  578. */
  579. public static function getUrlNewPost(){
  580. $url = self::$urlAdmin;
  581. $url .= "post-new.php";
  582. return($url);
  583. }
  584. /**
  585. *
  586. * delete post
  587. */
  588. public static function deletePost($postID){
  589. $success = wp_delete_post($postID,false);
  590. if($success == false)
  591. RevSliderFunctions::throwError("Could not delete post: $postID");
  592. }
  593. /**
  594. *
  595. * update post thumbnail
  596. */
  597. public static function updatePostThumbnail($postID,$thumbID){
  598. set_post_thumbnail($postID, $thumbID);
  599. }
  600. /**
  601. *
  602. * get intro from content
  603. */
  604. public static function getIntroFromContent($text){
  605. $intro = "";
  606. if(!empty($text)){
  607. $arrExtended = get_extended($text);
  608. $intro = RevSliderFunctions::getVal($arrExtended, "main");
  609. /*
  610. if(strlen($text) != strlen($intro))
  611. $intro .= "...";
  612. */
  613. }
  614. return($intro);
  615. }
  616. /**
  617. *
  618. * get excerpt from post id
  619. */
  620. public static function getExcerptById($postID, $limit=55){
  621. $post = get_post($postID);
  622. $excerpt = $post->post_excerpt;
  623. $excerpt = trim($excerpt);
  624. $excerpt = trim($excerpt);
  625. if(empty($excerpt))
  626. $excerpt = $post->post_content;
  627. $excerpt = strip_tags($excerpt,"<b><br><br/><i><strong><small>");
  628. $excerpt = RevSliderFunctions::getTextIntro($excerpt, $limit);
  629. return apply_filters('revslider_getExcerptById', $excerpt, $post, $limit);
  630. }
  631. /**
  632. *
  633. * get user display name from user id
  634. */
  635. public static function getUserDisplayName($userID){
  636. $displayName = get_the_author_meta('display_name', $userID);
  637. return($displayName);
  638. }
  639. /**
  640. *
  641. * get user avatar from user id
  642. */
  643. public static function getUserAvatarUrl($userID){
  644. $avatar = get_avatar_url($userID,array("size"=>"80"));
  645. return($avatar);
  646. }
  647. /**
  648. *
  649. * get user posts page from user id
  650. */
  651. public static function getUserPostsPage($userID){
  652. $link = get_author_posts_url($userID);
  653. return($link);
  654. }
  655. /**
  656. *
  657. * get user page from user id
  658. */
  659. public static function getUserPage($userID){
  660. $curauth = get_user_by('ID', $userID);
  661. $user_url = $curauth->user_url;
  662. return($user_url);
  663. }
  664. /**
  665. *
  666. * get categories by id's
  667. */
  668. public static function getCategoriesByIDs($arrIDs,$strTax = null){
  669. if(empty($arrIDs))
  670. return(array());
  671. if(is_string($arrIDs))
  672. $strIDs = $arrIDs;
  673. else
  674. $strIDs = implode(",", $arrIDs);
  675. $args = array();
  676. $args["include"] = $strIDs;
  677. if(!empty($strTax)){
  678. if(is_string($strTax))
  679. $strTax = explode(",",$strTax);
  680. $args["taxonomy"] = $strTax;
  681. }
  682. $arrCats = get_categories( $args );
  683. if(!empty($arrCats))
  684. $arrCats = RevSliderFunctions::convertStdClassToArray($arrCats);
  685. return($arrCats);
  686. }
  687. /**
  688. *
  689. * get categories short
  690. */
  691. public static function getCategoriesByIDsShort($arrIDs,$strTax = null){
  692. $arrCats = self::getCategoriesByIDs($arrIDs,$strTax);
  693. $arrNew = array();
  694. foreach($arrCats as $cat){
  695. $catID = $cat["term_id"];
  696. $catName = $cat["name"];
  697. $arrNew[$catID] = $catName;
  698. }
  699. return($arrNew);
  700. }
  701. /**
  702. * get categories list, copy the code from default wp functions
  703. */
  704. public static function getCategoriesHtmlList($catIDs,$strTax = null){
  705. global $wp_rewrite;
  706. //$catList = get_the_category_list( ",", "", $postID );
  707. $categories = self::getCategoriesByIDs($catIDs,$strTax);
  708. $arrErrors = RevSliderFunctions::getVal($categories, "errors");
  709. if(!empty($arrErrors)){
  710. foreach($arrErrors as $key=>$arr){
  711. $strErrors = implode($arr,",");
  712. }
  713. RevSliderFunctions::throwError("getCategoriesHtmlList error: ".$strErrors);
  714. }
  715. $rel = ( is_object( $wp_rewrite ) && $wp_rewrite->using_permalinks() ) ? 'rel="category tag"' : 'rel="category"';
  716. $separator = ',';
  717. $thelist = '';
  718. $i = 0;
  719. foreach ( $categories as $category ) {
  720. if(is_object($category))
  721. $category = (array)$category;
  722. if ( 0 < $i )
  723. $thelist .= $separator;
  724. $catID = $category["term_id"];
  725. $link = get_category_link($catID);
  726. $catName = $category["name"];
  727. if(!empty($link))
  728. $thelist .= '<a href="' . esc_url( $link ) . '" title="' . esc_attr( sprintf( __( "View all posts in %s", 'revslider'), $category["name"] ) ) . '" ' . $rel . '>' . $catName.'</a>';
  729. else
  730. $thelist .= $catName;
  731. ++$i;
  732. }
  733. return $thelist;
  734. }
  735. /**
  736. *
  737. * get post tags html list
  738. */
  739. public static function getTagsHtmlList($postID){
  740. $tagList = get_the_tag_list("",",","",$postID);
  741. return($tagList);
  742. }
  743. /**
  744. *
  745. * convert date to the date format that the user chose.
  746. */
  747. public static function convertPostDate($date, $with_time = false){
  748. if(empty($date))
  749. return($date);
  750. if($with_time){
  751. $date = date_i18n(get_option('date_format').' '.get_option('time_format'), strtotime($date));
  752. }else{
  753. $date = date_i18n(get_option('date_format'), strtotime($date));
  754. }
  755. return($date);
  756. }
  757. /**
  758. *
  759. * get assoc list of the taxonomies
  760. */
  761. public static function getTaxonomiesAssoc(){
  762. $arr = get_taxonomies();
  763. unset($arr["post_tag"]);
  764. unset($arr["nav_menu"]);
  765. unset($arr["link_category"]);
  766. unset($arr["post_format"]);
  767. return($arr);
  768. }
  769. /**
  770. *
  771. * get post types array with taxomonies
  772. */
  773. public static function getPostTypesWithTaxomonies(){
  774. $arrPostTypes = self::getPostTypesAssoc();
  775. foreach($arrPostTypes as $postType=>$title){
  776. $arrTaxomonies = self::getPostTypeTaxomonies($postType);
  777. $arrPostTypes[$postType] = $arrTaxomonies;
  778. }
  779. return($arrPostTypes);
  780. }
  781. /**
  782. *
  783. * get array of post types with categories (the taxonomies is between).
  784. * get only those taxomonies that have some categories in it.
  785. */
  786. public static function getPostTypesWithCats(){
  787. $arrPostTypes = self::getPostTypesWithTaxomonies();
  788. $arrPostTypesOutput = array();
  789. foreach($arrPostTypes as $name=>$arrTax){
  790. $arrTaxOutput = array();
  791. foreach($arrTax as $taxName=>$taxTitle){
  792. $cats = self::getCategoriesAssoc($taxName);
  793. if(!empty($cats))
  794. $arrTaxOutput[] = array(
  795. "name"=>$taxName,
  796. "title"=>$taxTitle,
  797. "cats"=>$cats);
  798. }
  799. $arrPostTypesOutput[$name] = $arrTaxOutput;
  800. }
  801. return($arrPostTypesOutput);
  802. }
  803. /**
  804. *
  805. * get array of all taxonomies with categories.
  806. */
  807. public static function getTaxonomiesWithCats(){
  808. $arrTax = self::getTaxonomiesAssoc();
  809. $arrTaxNew = array();
  810. foreach($arrTax as $key=>$value){
  811. $arrItem = array();
  812. $arrItem["name"] = $key;
  813. $arrItem["title"] = $value;
  814. $arrItem["cats"] = self::getCategoriesAssoc($key);
  815. $arrTaxNew[$key] = $arrItem;
  816. }
  817. return($arrTaxNew);
  818. }
  819. /**
  820. *
  821. * get content url
  822. */
  823. public static function getUrlContent(){
  824. if(self::isMultisite() == false){ //without multisite
  825. $baseUrl = content_url()."/";
  826. }
  827. else{ //for multisite
  828. $arrUploadData = wp_upload_dir();
  829. $baseUrl = $arrUploadData["baseurl"]."/";
  830. }
  831. if(is_ssl()){
  832. $baseUrl = str_replace("http://", "https://", $baseUrl);
  833. }
  834. return($baseUrl);
  835. }
  836. /**
  837. *
  838. * get wp-content path
  839. */
  840. public static function getPathContent(){
  841. if(self::isMultisite()){
  842. if(!defined("BLOGUPLOADDIR")){
  843. $pathBase = self::getPathBase();
  844. $pathContent = $pathBase."wp-content/";
  845. }else
  846. $pathContent = BLOGUPLOADDIR;
  847. }else{
  848. $pathContent = WP_CONTENT_DIR;
  849. if(!empty($pathContent)){
  850. $pathContent .= "/";
  851. }
  852. else{
  853. $pathBase = self::getPathBase();
  854. $pathContent = $pathBase."wp-content/";
  855. }
  856. }
  857. return($pathContent);
  858. }
  859. /**
  860. *
  861. * get cats and taxanomies data from the category id's
  862. */
  863. public static function getCatAndTaxData($catIDs){
  864. if(is_string($catIDs)){
  865. $catIDs = trim($catIDs);
  866. if(empty($catIDs))
  867. return(array("tax"=>"","cats"=>""));
  868. $catIDs = explode(",", $catIDs);
  869. }
  870. $strCats = "";
  871. $arrTax = array();
  872. foreach($catIDs as $cat){
  873. if(strpos($cat,"option_disabled") === 0)
  874. continue;
  875. $pos = strrpos($cat,"_");
  876. if($pos === false)
  877. RevSliderFunctions::throwError("The category is in wrong format");
  878. $taxName = substr($cat,0,$pos);
  879. $catID = substr($cat,$pos+1,strlen($cat)-$pos-1);
  880. $arrTax[$taxName] = $taxName;
  881. if(!empty($strCats))
  882. $strCats .= ",";
  883. $strCats .= $catID;
  884. }
  885. $strTax = "";
  886. foreach($arrTax as $taxName){
  887. if(!empty($strTax))
  888. $strTax .= ",";
  889. $strTax .= $taxName;
  890. }
  891. $output = array("tax"=>$strTax,"cats"=>$strCats);
  892. return($output);
  893. }
  894. /**
  895. *
  896. * get current language code
  897. */
  898. public static function getCurrentLangCode(){
  899. $langTag = ICL_LANGUAGE_CODE;
  900. return($langTag);
  901. }
  902. /**
  903. *
  904. * check the current post for the existence of a short code
  905. */
  906. public static function hasShortcode($shortcode = '') {
  907. if(!is_singular())
  908. return false;
  909. $post = get_post(get_the_ID());
  910. $found = false;
  911. if (empty($shortcode))
  912. return $found;
  913. if (stripos($post->post_content, '[' . $shortcode) !== false )
  914. $found = true;
  915. return $found;
  916. }
  917. /**
  918. * Check if shortcodes exists in the content
  919. * @since: 5.0
  920. */
  921. public static function check_for_shortcodes($mid_content){
  922. if($mid_content !== null){
  923. if(has_shortcode($mid_content, 'gallery')){
  924. preg_match('/\[gallery.*ids=.(.*).\]/', $mid_content, $img_ids);
  925. if(isset($img_ids[1])){
  926. if($img_ids[1] !== '') return explode(',', $img_ids[1]);
  927. }
  928. }
  929. }
  930. return false;
  931. }
  932. /**
  933. * retrieve the image id from the given image url
  934. * @since: 5.0
  935. */
  936. public static function get_image_id_by_url($image_url) {
  937. global $wpdb;
  938. $attachment_id = 0;
  939. if(function_exists('attachment_url_to_postid')){
  940. $attachment_id = attachment_url_to_postid($image_url); //0 if failed
  941. }
  942. if ( 0 == $attachment_id ){ //try to get it old school way
  943. //for WP < 4.0.0
  944. $attachment_id = false;
  945. // If there is no url, return.
  946. if ( '' == $image_url )
  947. return;
  948. // Get the upload directory paths
  949. $upload_dir_paths = wp_upload_dir();
  950. // Make sure the upload path base directory exists in the attachment URL, to verify that we're working with a media library image
  951. if ( false !== strpos( $image_url, $upload_dir_paths['baseurl'] ) ) {
  952. // If this is the URL of an auto-generated thumbnail, get the URL of the original image
  953. $image_url = preg_replace( '/-\d+x\d+(?=\.(jpg|jpeg|png|gif)$)/i', '', $image_url );
  954. // Remove the upload path base directory from the attachment URL
  955. $image_url = str_replace( $upload_dir_paths['baseurl'] . '/', '', $image_url );
  956. // Finally, run a custom database query to get the attachment ID from the modified attachment URL
  957. $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 ) );
  958. }
  959. }
  960. return $attachment_id;
  961. }
  962. public static function update_option($handle, $value, $autoload = 'on'){ //on is on, false is 'off'
  963. if(!add_option($handle, $value, '', $autoload)){ //returns false if option is not existing
  964. delete_option($handle);
  965. }
  966. add_option($handle, $value, '', $autoload);
  967. }
  968. } //end of the class
  969. //init the static vars
  970. RevSliderFunctionsWP::initStaticVars();
  971. /**
  972. * old classname extends new one (old classnames will be obsolete soon)
  973. * @since: 5.0
  974. **/
  975. class UniteFunctionsWPRev extends RevSliderFunctionsWP {}
  976. ?>