Advertisement
bananamariap

seo-keen-functionality.php

Sep 16th, 2021
21
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 56.54 KB | None | 0 0
  1. <?php
  2. /**
  3. * Plugin Name: SEO Keen Functionality
  4. * Plugin URI: http://www.keen.com.mt
  5. * Description: Extra functionality for SEO Keen.
  6. * Version: 1.0
  7. * Author: Keen Ltd.
  8. * Author URI: http://www.keen.com.mt
  9. */
  10.  
  11. ini_set('max_execution_time', 500);
  12.  
  13. //Register Publisher CPT
  14. function publisher_cpt()
  15. {
  16. $labels = array(
  17. 'name' => _x( 'Publishers', 'Post Type General Name', 'ax-front' ),
  18. 'singular_name' => _x( 'Publisher', 'Post Type Singular Name', 'ax-front' ),
  19. 'menu_name' => __( 'Publishers', 'ax-front' ),
  20. 'name_admin_bar' => __( 'Publisher', 'ax-front' ),
  21. 'archives' => __( 'Publisher Archives', 'ax-front' ),
  22. 'attributes' => __( 'Publisher Attributes', 'ax-front' ),
  23. 'parent_item_colon' => __( 'Parent Publisher:', 'ax-front' ),
  24. 'all_items' => __( 'All Publishers', 'ax-front' ),
  25. 'add_new_item' => __( 'Add New Publisher', 'ax-front' ),
  26. 'add_new' => __( 'Add New', 'ax-front' ),
  27. 'new_item' => __( 'New Publisher', 'ax-front' ),
  28. 'edit_item' => __( 'Edit Publisher', 'ax-front' ),
  29. 'update_item' => __( 'Update Publisher', 'ax-front' ),
  30. 'view_item' => __( 'View Publisher', 'ax-front' ),
  31. 'view_items' => __( 'View Publisher', 'ax-front' ),
  32. 'search_items' => __( 'Search Publishers', 'ax-front' ),
  33. 'not_found' => __( 'Not found', 'ax-front' ),
  34. 'not_found_in_trash' => __( 'Not found in Trash', 'ax-front' ),
  35. 'featured_image' => __( 'Featured Image', 'ax-front' ),
  36. 'set_featured_image' => __( 'Set featured image', 'ax-front' ),
  37. 'remove_featured_image' => __( 'Remove featured image', 'ax-front' ),
  38. 'use_featured_image' => __( 'Use as featured image', 'ax-front' ),
  39. 'insert_into_item' => __( 'Insert into event', 'ax-front' ),
  40. 'uploaded_to_this_item' => __( 'Uploaded to this event', 'ax-front' ),
  41. 'items_list' => __( 'Publisher list', 'ax-front' ),
  42. 'items_list_navigation' => __( 'Publisher list navigation', 'ax-front' ),
  43. 'filter_items_list' => __( 'Filter Publisher list', 'ax-front' ),
  44. );
  45.  
  46. register_post_type('publisher',
  47. array(
  48. 'labels' => $labels,
  49. 'public' => true,
  50. 'show_in_rest'=> true,
  51. 'menu_icon' => "dashicons-welcome-view-site",
  52. 'menu_position' => 6,
  53. 'rewrite' => array(
  54. 'slug' => 'publisher',
  55. 'with_front' => false
  56. ),
  57. 'supports' => array( 'title' ),
  58. )
  59. );
  60. }
  61. add_action('init', 'publisher_cpt');
  62.  
  63. function publisher_categories_taxonomy() {
  64. register_taxonomy(
  65. 'category', // The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces).
  66. 'publisher', // post type name
  67. array(
  68. 'hierarchical' => true,
  69. 'label' => 'Categories', // display name
  70. 'query_var' => true,
  71. 'rewrite' => array(
  72. 'slug' => 'category', // This controls the base slug that will display before each term
  73. 'with_front' => false // Don't display the category base before
  74. )
  75. )
  76. );
  77. }
  78. add_action( 'init', 'publisher_categories_taxonomy');
  79.  
  80. //Register Links CPT
  81. function link_cpt()
  82. {
  83. $labels = array(
  84. 'name' => _x( 'Links', 'Post Type General Name', 'ax-front' ),
  85. 'singular_name' => _x( 'Link', 'Post Type Singular Name', 'ax-front' ),
  86. 'menu_name' => __( 'Links', 'ax-front' ),
  87. 'name_admin_bar' => __( 'Link', 'ax-front' ),
  88. 'archives' => __( 'Link Archives', 'ax-front' ),
  89. 'attributes' => __( 'Link Attributes', 'ax-front' ),
  90. 'parent_item_colon' => __( 'Parent Link:', 'ax-front' ),
  91. 'all_items' => __( 'All Links', 'ax-front' ),
  92. 'add_new_item' => __( 'Add New Link', 'ax-front' ),
  93. 'add_new' => __( 'Add New', 'ax-front' ),
  94. 'new_item' => __( 'New Link', 'ax-front' ),
  95. 'edit_item' => __( 'Edit Link', 'ax-front' ),
  96. 'update_item' => __( 'Update Link', 'ax-front' ),
  97. 'view_item' => __( 'View Link', 'ax-front' ),
  98. 'view_items' => __( 'View Link', 'ax-front' ),
  99. 'search_items' => __( 'Search Links', 'ax-front' ),
  100. 'not_found' => __( 'Not found', 'ax-front' ),
  101. 'not_found_in_trash' => __( 'Not found in Trash', 'ax-front' ),
  102. 'featured_image' => __( 'Featured Image', 'ax-front' ),
  103. 'set_featured_image' => __( 'Set featured image', 'ax-front' ),
  104. 'remove_featured_image' => __( 'Remove featured image', 'ax-front' ),
  105. 'use_featured_image' => __( 'Use as featured image', 'ax-front' ),
  106. 'insert_into_item' => __( 'Insert into event', 'ax-front' ),
  107. 'uploaded_to_this_item' => __( 'Uploaded to this event', 'ax-front' ),
  108. 'items_list' => __( 'Link list', 'ax-front' ),
  109. 'items_list_navigation' => __( 'Link list navigation', 'ax-front' ),
  110. 'filter_items_list' => __( 'Filter Link list', 'ax-front' ),
  111. );
  112.  
  113. register_post_type('link',
  114. array(
  115. 'labels' => $labels,
  116. 'public' => true,
  117. 'show_in_rest'=> true,
  118. 'menu_icon' => "dashicons-admin-links",
  119. 'menu_position' => 6,
  120. 'capabilities' => array(
  121. 'edit_post' => 'edit_link',
  122. 'edit_posts' => 'edit_links',
  123. 'edit_others_posts' => 'edit_other_links',
  124. 'publish_posts' => 'publish_links',
  125. 'read_post' => 'read_link',
  126. 'read_private_posts' => 'read_private_links',
  127. 'delete_post' => 'delete_link',
  128. 'delete_posts' => 'delete_links',
  129. ),
  130. 'rewrite' => array(
  131. 'slug' => 'link',
  132. 'with_front' => false
  133. ),
  134. 'supports' => array( 'title' ),
  135. )
  136. );
  137. }
  138. add_action('init', 'link_cpt');
  139.  
  140. function sources_taxonomy() {
  141. register_taxonomy(
  142. 'source', // The name of the taxonomy. Name should be in slug form (must not contain capital letters or spaces).
  143. 'link', // post type name
  144. array(
  145. 'hierarchical' => true,
  146. 'label' => 'Sources', // display name
  147. 'query_var' => true,
  148. 'rewrite' => array(
  149. 'slug' => 'source', // This controls the base slug that will display before each term
  150. 'with_front' => false // Don't display the category base before
  151. )
  152. )
  153. );
  154. }
  155. add_action( 'init', 'sources_taxonomy');
  156.  
  157.  
  158. //Register Request CPT
  159. function request_cpt()
  160. {
  161. $labels = array(
  162. 'name' => _x( 'Requests', 'Post Type General Name', 'ax-front' ),
  163. 'singular_name' => _x( 'Request', 'Post Type Singular Name', 'ax-front' ),
  164. 'menu_name' => __( 'Requests', 'ax-front' ),
  165. 'name_admin_bar' => __( 'Request', 'ax-front' ),
  166. 'archives' => __( 'Request Archives', 'ax-front' ),
  167. 'attributes' => __( 'Request Attributes', 'ax-front' ),
  168. 'parent_item_colon' => __( 'Parent Request:', 'ax-front' ),
  169. 'all_items' => __( 'All Requests', 'ax-front' ),
  170. 'add_new_item' => __( 'Add New Request', 'ax-front' ),
  171. 'add_new' => __( 'Add New', 'ax-front' ),
  172. 'new_item' => __( 'New Request', 'ax-front' ),
  173. 'edit_item' => __( 'Edit Request', 'ax-front' ),
  174. 'update_item' => __( 'Update Request', 'ax-front' ),
  175. 'view_item' => __( 'View Request', 'ax-front' ),
  176. 'view_items' => __( 'View Request', 'ax-front' ),
  177. 'search_items' => __( 'Search Request', 'ax-front' ),
  178. 'not_found' => __( 'Not found', 'ax-front' ),
  179. 'not_found_in_trash' => __( 'Not found in Trash', 'ax-front' ),
  180. 'featured_image' => __( 'Featured Image', 'ax-front' ),
  181. 'set_featured_image' => __( 'Set featured image', 'ax-front' ),
  182. 'remove_featured_image' => __( 'Remove featured image', 'ax-front' ),
  183. 'use_featured_image' => __( 'Use as featured image', 'ax-front' ),
  184. 'insert_into_item' => __( 'Insert into event', 'ax-front' ),
  185. 'uploaded_to_this_item' => __( 'Uploaded to this event', 'ax-front' ),
  186. 'items_list' => __( 'Requests list', 'ax-front' ),
  187. 'items_list_navigation' => __( 'Requests list navigation', 'ax-front' ),
  188. 'filter_items_list' => __( 'Filter Requests list', 'ax-front' ),
  189. );
  190.  
  191. register_post_type('request',
  192. array(
  193. 'labels' => $labels,
  194. 'public' => true,
  195. 'publicly_queryable' => false,
  196. 'show_in_rest'=> true,
  197. 'menu_icon' => "dashicons-lightbulb",
  198. 'menu_position' => 6,
  199. 'rewrite' => array(
  200. 'slug' => 'request',
  201. 'with_front' => false
  202. ),
  203. 'supports' => array( 'title' ),
  204. )
  205. );
  206. }
  207. add_action('init', 'request_cpt');
  208.  
  209. //Register Task CPT
  210. // function task_cpt()
  211. // {
  212. // $labels = array(
  213. // 'name' => _x( 'Tasks', 'Post Type General Name', 'ax-front' ),
  214. // 'singular_name' => _x( 'Task', 'Post Type Singular Name', 'ax-front' ),
  215. // 'menu_name' => __( 'Tasks', 'ax-front' ),
  216. // 'name_admin_bar' => __( 'Task', 'ax-front' ),
  217. // 'archives' => __( 'Task Archives', 'ax-front' ),
  218. // 'attributes' => __( 'Task Attributes', 'ax-front' ),
  219. // 'parent_item_colon' => __( 'Parent Task:', 'ax-front' ),
  220. // 'all_items' => __( 'All Tasks', 'ax-front' ),
  221. // 'add_new_item' => __( 'Add New Task', 'ax-front' ),
  222. // 'add_new' => __( 'Add New', 'ax-front' ),
  223. // 'new_item' => __( 'New Task', 'ax-front' ),
  224. // 'edit_item' => __( 'Edit Task', 'ax-front' ),
  225. // 'update_item' => __( 'Update Task', 'ax-front' ),
  226. // 'view_item' => __( 'View Task', 'ax-front' ),
  227. // 'view_items' => __( 'View Task', 'ax-front' ),
  228. // 'search_items' => __( 'Search Task', 'ax-front' ),
  229. // 'not_found' => __( 'Not found', 'ax-front' ),
  230. // 'not_found_in_trash' => __( 'Not found in Trash', 'ax-front' ),
  231. // 'featured_image' => __( 'Featured Image', 'ax-front' ),
  232. // 'set_featured_image' => __( 'Set featured image', 'ax-front' ),
  233. // 'remove_featured_image' => __( 'Remove featured image', 'ax-front' ),
  234. // 'use_featured_image' => __( 'Use as featured image', 'ax-front' ),
  235. // 'insert_into_item' => __( 'Insert into event', 'ax-front' ),
  236. // 'uploaded_to_this_item' => __( 'Uploaded to this event', 'ax-front' ),
  237. // 'items_list' => __( 'Tasks list', 'ax-front' ),
  238. // 'items_list_navigation' => __( 'Tasks list navigation', 'ax-front' ),
  239. // 'filter_items_list' => __( 'Filter Tasks list', 'ax-front' ),
  240. // );
  241.  
  242. // register_post_type('task',
  243. // array(
  244. // 'labels' => $labels,
  245. // 'public' => true,
  246. // 'publicly_queryable' => true,
  247. // 'show_in_rest'=> true,
  248. // 'menu_icon' => "dashicons-yes-alt",
  249. // 'menu_position' => 6,
  250. // 'capabilities' => array(
  251. // 'edit_post' => 'edit_task',
  252. // 'edit_posts' => 'edit_tasks',
  253. // 'edit_others_posts' => 'edit_other_tasks',
  254. // 'publish_posts' => 'publish_tasks',
  255. // 'read_post' => 'read_task',
  256. // 'read_private_posts' => 'read_private_tasks',
  257. // 'delete_post' => 'delete_task',
  258. // 'delete_posts' => 'delete_tasks',
  259. // ),
  260. // 'rewrite' => array(
  261. // 'slug' => 'task',
  262. // 'with_front' => false
  263. // ),
  264. // 'supports' => array( 'title' ),
  265. // )
  266. // );
  267. // }
  268. // add_action('init', 'task_cpt');
  269.  
  270. //Register Invoice CPT
  271. function invoice_cpt()
  272. {
  273. $labels = array(
  274. 'name' => _x( 'Invoices', 'Post Type General Name', 'ax-front' ),
  275. 'singular_name' => _x( 'Invoice', 'Post Type Singular Name', 'ax-front' ),
  276. 'menu_name' => __( 'Invoices', 'ax-front' ),
  277. 'name_admin_bar' => __( 'Invoice', 'ax-front' ),
  278. 'archives' => __( 'Invoice Archives', 'ax-front' ),
  279. 'attributes' => __( 'Invoice Attributes', 'ax-front' ),
  280. 'parent_item_colon' => __( 'Parent Invoice:', 'ax-front' ),
  281. 'all_items' => __( 'All Invoices', 'ax-front' ),
  282. 'add_new_item' => __( 'Add New Invoice', 'ax-front' ),
  283. 'add_new' => __( 'Add New', 'ax-front' ),
  284. 'new_item' => __( 'New Invoice', 'ax-front' ),
  285. 'edit_item' => __( 'Edit Invoice', 'ax-front' ),
  286. 'update_item' => __( 'Update Invoice', 'ax-front' ),
  287. 'view_item' => __( 'View Invoice', 'ax-front' ),
  288. 'view_items' => __( 'View Invoice', 'ax-front' ),
  289. 'search_items' => __( 'Search Invoice', 'ax-front' ),
  290. 'not_found' => __( 'Not found', 'ax-front' ),
  291. 'not_found_in_trash' => __( 'Not found in Trash', 'ax-front' ),
  292. 'featured_image' => __( 'Featured Image', 'ax-front' ),
  293. 'set_featured_image' => __( 'Set featured image', 'ax-front' ),
  294. 'remove_featured_image' => __( 'Remove featured image', 'ax-front' ),
  295. 'use_featured_image' => __( 'Use as featured image', 'ax-front' ),
  296. 'insert_into_item' => __( 'Insert into event', 'ax-front' ),
  297. 'uploaded_to_this_item' => __( 'Uploaded to this event', 'ax-front' ),
  298. 'items_list' => __( 'Invoices list', 'ax-front' ),
  299. 'items_list_navigation' => __( 'Invoices list navigation', 'ax-front' ),
  300. 'filter_items_list' => __( 'Filter Invoices list', 'ax-front' ),
  301. );
  302.  
  303. register_post_type('invoices',
  304. array(
  305. 'labels' => $labels,
  306. 'public' => true,
  307. 'publicly_queryable' => false,
  308. 'show_in_rest'=> true,
  309. 'menu_icon' => "dashicons-media-spreadsheet",
  310. 'menu_position' => 6,
  311. 'rewrite' => array(
  312. 'slug' => 'invoice',
  313. 'with_front' => false
  314. ),
  315. 'supports' => array( 'title' ),
  316. )
  317. );
  318. }
  319. add_action('init', 'invoice_cpt');
  320.  
  321. //Register Expenses CPT
  322. function expenses_cpt()
  323. {
  324. $labels = array(
  325. 'name' => _x( 'Expenses', 'Post Type General Name', 'ax-front' ),
  326. 'singular_name' => _x( 'Expense', 'Post Type Singular Name', 'ax-front' ),
  327. 'menu_name' => __( 'Expenses', 'ax-front' ),
  328. 'name_admin_bar' => __( 'Expense', 'ax-front' ),
  329. 'archives' => __( 'Expense Archives', 'ax-front' ),
  330. 'attributes' => __( 'Expense Attributes', 'ax-front' ),
  331. 'parent_item_colon' => __( 'Parent Expense:', 'ax-front' ),
  332. 'all_items' => __( 'All Expenses', 'ax-front' ),
  333. 'add_new_item' => __( 'Add New Expense', 'ax-front' ),
  334. 'add_new' => __( 'Add New', 'ax-front' ),
  335. 'new_item' => __( 'New Expense', 'ax-front' ),
  336. 'edit_item' => __( 'Edit Expense', 'ax-front' ),
  337. 'update_item' => __( 'Update Expense', 'ax-front' ),
  338. 'view_item' => __( 'View Expense', 'ax-front' ),
  339. 'view_items' => __( 'View Expense', 'ax-front' ),
  340. 'search_items' => __( 'Search Expense', 'ax-front' ),
  341. 'not_found' => __( 'Not found', 'ax-front' ),
  342. 'not_found_in_trash' => __( 'Not found in Trash', 'ax-front' ),
  343. 'featured_image' => __( 'Featured Image', 'ax-front' ),
  344. 'set_featured_image' => __( 'Set featured image', 'ax-front' ),
  345. 'remove_featured_image' => __( 'Remove featured image', 'ax-front' ),
  346. 'use_featured_image' => __( 'Use as featured image', 'ax-front' ),
  347. 'insert_into_item' => __( 'Insert into event', 'ax-front' ),
  348. 'uploaded_to_this_item' => __( 'Uploaded to this event', 'ax-front' ),
  349. 'items_list' => __( 'Expenses list', 'ax-front' ),
  350. 'items_list_navigation' => __( 'Expenses list navigation', 'ax-front' ),
  351. 'filter_items_list' => __( 'Filter Expenses list', 'ax-front' ),
  352. );
  353.  
  354. register_post_type('expenses',
  355. array(
  356. 'labels' => $labels,
  357. 'public' => true,
  358. 'publicly_queryable' => false,
  359. 'show_in_rest'=> true,
  360. 'menu_icon' => "dashicons-tickets-alt",
  361. 'menu_position' => 6,
  362. 'rewrite' => array(
  363. 'slug' => 'expense',
  364. 'with_front' => false
  365. ),
  366. 'supports' => array( 'title' ),
  367. )
  368. );
  369. }
  370. add_action('init', 'expenses_cpt');
  371.  
  372.  
  373.  
  374. //custom functions
  375. function get_block( $post_id, $block_name ) {
  376.  
  377. //get post_content for page
  378. $post_content = get_post( $post_id )->post_content;
  379.  
  380. //get all blocks of requested type
  381. $blocks = array_filter( parse_blocks( $post_content ), function( $block ) use( $block_name ) {
  382. return $block_name === $block['blockName'];
  383. });
  384.  
  385. $block_content = '';
  386. foreach( $blocks as $block ) {
  387. $block_content .= render_block( $block );
  388. }
  389.  
  390. return $block_content;
  391. }
  392.  
  393. function get_block_field( $post_id, $block_name, $field ) {
  394.  
  395. //get post_content for page
  396. $post_content = get_post( $post_id )->post_content;
  397.  
  398. //get all blocks of requested type
  399. $blocks = array_filter( parse_blocks( $post_content ), function( $block ) use( $block_name ) {
  400. return $block_name === $block['blockName'];
  401. });
  402.  
  403. foreach ($blocks as $block) {
  404. $field_data = $block['attrs']['data'][$field];
  405. }
  406.  
  407. return $field_data;
  408.  
  409. }
  410.  
  411.  
  412. //add options page
  413. if( function_exists('acf_add_options_page') ) {
  414.  
  415. acf_add_options_page(array(
  416. 'page_title' => 'Global Settings',
  417. 'menu_title' => 'Global Settings',
  418. 'menu_slug' => 'global-settings',
  419. 'capability' => 'edit_posts',
  420. 'redirect' => false
  421. ));
  422.  
  423. }
  424.  
  425. function enqueue() {
  426. wp_register_script( 'jszip', plugin_dir_url( __FILE__ ).'/js/jszip.min.js', array('jquery'), '', true );
  427. wp_register_script( 'jszip-utils', plugin_dir_url( __FILE__ ).'/js/jszip-utils.min.js', array('jquery'), '', true );
  428. wp_register_script( 'filesaver', plugin_dir_url( __FILE__ ).'/js/FileSaver.min.js', array('jquery'), '', true );
  429. wp_register_script( 'admin-links', plugin_dir_url( __FILE__ ).'/js/admin-links.js', array('jquery'), '', true );
  430. wp_localize_script( 'admin-links', 'myAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));
  431. wp_register_script( 'admin-publishers', plugin_dir_url( __FILE__ ).'/js/admin-publishers.js', array('jquery'), '', true );
  432. wp_localize_script( 'admin-publishers', 'myAjax', array( 'ajaxurl' => admin_url( 'admin-ajax.php' )));
  433. wp_register_script( 'chosen-js', plugin_dir_url( __FILE__ ).'/js/chosen.jquery.min.js', array('jquery'), '', true );
  434.  
  435.  
  436. wp_enqueue_script( 'jquery' );
  437. wp_enqueue_script( 'jszip' );
  438. wp_enqueue_script( 'jszip-utils' );
  439. wp_enqueue_script( 'filesaver' );
  440. wp_enqueue_script( 'admin-links' );
  441. wp_enqueue_script( 'admin-publishers' );
  442. wp_enqueue_script( 'chosen-js' );
  443.  
  444. wp_enqueue_style( 'admin-css', plugin_dir_url( __FILE__ ).'/css/admin-css.css' );
  445. wp_enqueue_style( 'chosen-css', plugin_dir_url( __FILE__ ).'/css/chosen.min.css' );
  446. }
  447.  
  448. add_action( 'admin_enqueue_scripts', 'enqueue', 100 );
  449.  
  450. function enqueue_publishers_scripts(){
  451. wp_enqueue_script( 'publisher_datatables', plugin_dir_url( __FILE__ ).'/js/publishertable.js', array('jquery'), '', true );
  452. wp_localize_script( 'publisher_datatables', 'ajax_url', admin_url('admin-ajax.php?action=publisher_datatables') );
  453. }
  454.  
  455. function enqueue_orders_scripts(){
  456. wp_enqueue_script( 'orders_datatables', plugin_dir_url( __FILE__ ).'/js/orderstable.js', array('jquery'), '', true );
  457. wp_localize_script( 'orders_datatables', 'ajax_url', admin_url('admin-ajax.php?action=orders_datatables') );
  458. }
  459.  
  460. add_action( 'template_redirect', function(){
  461. if(is_page('publishers') || is_page('favourites')){
  462. add_action( 'wp_enqueue_scripts', 'enqueue_publishers_scripts' );
  463. }else if(is_page('orders')){
  464. add_action( 'wp_enqueue_scripts', 'enqueue_orders_scripts' );
  465. }
  466. } );
  467.  
  468.  
  469. //Populate Links select fields
  470. function load_publisher_choices( $field ) {
  471.  
  472. $field['choices'] = array();
  473.  
  474. $publishers = get_posts([
  475. 'post_type' => 'publisher',
  476. 'post_status' => 'publish',
  477. 'numberposts' => -1
  478. ]);
  479.  
  480. $field['choices'][] = 'Select a publisher';
  481.  
  482. foreach($publishers as $publisher) {
  483. $value = $publisher->ID;
  484. $label = get_field("domain", $publisher->ID);
  485.  
  486. $field['choices'][ $value ] = $label;
  487. }
  488.  
  489. return $field;
  490. }
  491. add_filter('acf/load_field/name=publisher', 'load_publisher_choices');
  492.  
  493. function load_source_choices( $field ) {
  494.  
  495. $field['choices'] = array();
  496.  
  497. $sources = get_terms(array(
  498. 'taxonomy' => 'source',
  499. 'hide_empty' => false
  500. ));
  501.  
  502. $field['choices'][] = 'Select a source';
  503.  
  504. foreach($sources as $source) {
  505. $value = $source->term_id;
  506. $label = $source->name;
  507.  
  508. $field['choices'][ $value ] = $label;
  509. }
  510.  
  511. return $field;
  512. }
  513. add_filter('acf/load_field/name=source', 'load_source_choices');
  514.  
  515. function load_client_choices( $field ) {
  516.  
  517. $field['choices'] = array();
  518.  
  519. $args = array(
  520. 'role__in' => [ 'links_client', 'monthly_client', 'administrator' ],
  521. 'orderby' => 'user_nicename',
  522. 'order' => 'ASC'
  523. );
  524. $clients = get_users( $args );
  525.  
  526. $field['choices'][] = 'Select a client';
  527.  
  528. foreach($clients as $client) {
  529.  
  530. $value = $client->ID;
  531. $company = get_user_meta($client->ID, "company", true);
  532.  
  533. $field['choices'][ $value ] = $company;
  534. }
  535.  
  536. return $field;
  537. }
  538. add_filter('acf/load_field/name=client', 'load_client_choices');
  539.  
  540. function load_writer_choices( $field ) {
  541.  
  542. $field['choices'] = array();
  543.  
  544. $args = array(
  545. 'role__in' => [ 'writer' ],
  546. 'orderby' => 'user_nicename',
  547. 'order' => 'ASC'
  548. );
  549. $writers = get_users( $args );
  550.  
  551. $field['choices'][] = 'Select a writer';
  552.  
  553. foreach($writers as $writer) {
  554.  
  555. $value = $writer->ID;
  556. $label = $writer->display_name;
  557.  
  558. $field['choices'][ $value ] = $label;
  559. }
  560.  
  561. return $field;
  562. }
  563. add_filter('acf/load_field/name=writer', 'load_writer_choices');
  564.  
  565. function load_invoice_choices( $field ) {
  566.  
  567. $field['choices'] = array();
  568.  
  569. $invoices = get_posts([
  570. 'post_type' => 'invoices',
  571. 'post_status' => 'publish',
  572. 'numberposts' => -1
  573. ]);
  574.  
  575. $field['choices'][] = 'Select an invoice no.';
  576.  
  577. foreach($invoices as $invoice) {
  578. $value = $invoice->ID;
  579. $invoice_no = get_post_meta( $invoice->ID , 'invoice_no' , true );
  580. $label = $invoice_no;
  581.  
  582.  
  583.  
  584. $field['choices'][ $value ] = $label;
  585. }
  586.  
  587. return $field;
  588. }
  589. add_filter('acf/load_field/name=invoice_no', 'load_invoice_choices');
  590.  
  591.  
  592.  
  593.  
  594. //Populate Tasks select fields
  595. function load_link_choices( $field ) {
  596.  
  597. $field['choices'] = array();
  598.  
  599. $links = get_posts([
  600. 'post_type' => 'link',
  601. 'post_status' => 'publish',
  602. 'numberposts' => -1
  603. ]);
  604.  
  605. $field['choices'][] = 'Select a link';
  606.  
  607. foreach($links as $link) {
  608. $value = $link->ID;
  609. $label = $link->post_title;
  610.  
  611. $field['choices'][ $value ] = $label;
  612. }
  613.  
  614. return $field;
  615. }
  616. add_filter('acf/load_field/name=link', 'load_link_choices');
  617.  
  618.  
  619. //Populate client publisher categories
  620. function load_publisher_categories_choices( $field ) {
  621.  
  622. $field['choices'] = array();
  623.  
  624. $publisher_categories = get_terms([
  625. 'taxonomy' => 'category',
  626. 'hide_empty' => 'false',
  627. ]);
  628.  
  629. foreach($publisher_categories as $category) {
  630. $value = $category->name;
  631. $label = $category->name;
  632.  
  633. $field['choices'][ $value ] = $label;
  634. }
  635.  
  636. return $field;
  637. }
  638. add_filter('acf/load_field/name=publisher_categories', 'load_publisher_categories_choices');
  639.  
  640.  
  641. function acf_disabled($field) {
  642. $post = get_post();
  643.  
  644. $field['disabled'] = 1;
  645. return $field;
  646. }
  647.  
  648. function acf_readonly($field) {
  649. $post = get_post();
  650.  
  651. $field['readonly'] = 1;
  652. return $field;
  653. }
  654.  
  655. add_filter('acf/load_field/name=publisher_fee', 'acf_readonly');
  656. add_filter('acf/load_field/name=client_price', 'acf_readonly');
  657. add_filter('acf/load_field/name=unique_code', 'acf_disabled');
  658. add_filter('acf/load_field/name=date_updated', 'acf_disabled');
  659. add_filter('acf/load_field/name=writer_cost', 'acf_readonly');
  660. add_filter('acf/load_field/name=publisher_requirements_links', 'acf_readonly');
  661.  
  662.  
  663. //User management
  664. function add_roles() {
  665. add_role( 'links_client', 'Links Client', array( 'read' => true, 'level_0' => true ) );
  666. add_role( 'monthly_client', 'Monthly SEO Client', array( 'read' => true, 'level_0' => true ) );
  667. add_role( 'writer', 'Writer', array( 'read' => true, 'level_0' => true ) );
  668. add_role( 'freelancer', 'Freelancer', array( 'read' => true, 'level_0' => true ) );
  669. }
  670. add_action( 'init', 'add_roles' );
  671.  
  672.  
  673. function add_theme_caps() {
  674. $writer = get_role( 'writer' );
  675.  
  676.  
  677. $writer->add_cap( 'edit_link' );
  678. $writer->add_cap( 'edit_links' );
  679. $writer->add_cap( 'edit_other_links' );
  680. // $writer->add_cap( 'publish_links' );
  681. $writer->add_cap( 'read_link' );
  682. $writer->add_cap( 'read_private_links' );
  683. $writer->add_cap( 'delete_link' );
  684. $writer->add_cap( 'delete_links' );
  685. $writer->remove_cap( 'publish_links' );
  686.  
  687. $freelancer = get_role( 'freelancer' );
  688.  
  689. $freelancer->add_cap( 'edit_link' );
  690. $freelancer->add_cap( 'edit_links' );
  691. $freelancer->add_cap( 'edit_other_links' );
  692. $freelancer->add_cap( 'publish_links' );
  693. $freelancer->add_cap( 'read_link' );
  694. $freelancer->add_cap( 'read_private_links' );
  695. $freelancer->add_cap( 'delete_link' );
  696. $freelancer->add_cap( 'delete_links' );
  697.  
  698.  
  699. $admin = get_role( 'administrator' );
  700.  
  701. $admin->add_cap( 'edit_task' );
  702. $admin->add_cap( 'edit_tasks' );
  703. $admin->add_cap( 'edit_other_tasks' );
  704. $admin->add_cap( 'publish_tasks' );
  705. $admin->add_cap( 'read_task' );
  706. $admin->add_cap( 'read_private_tasks' );
  707. $admin->add_cap( 'delete_task' );
  708. $admin->add_cap( 'delete_tasks' );
  709.  
  710. $admin->add_cap( 'edit_link' );
  711. $admin->add_cap( 'edit_links' );
  712. $admin->add_cap( 'edit_other_links' );
  713. $admin->add_cap( 'publish_links' );
  714. $admin->add_cap( 'read_link' );
  715. $admin->add_cap( 'read_private_links' );
  716. $admin->add_cap( 'delete_link' );
  717. $admin->add_cap( 'delete_links' );
  718. }
  719. add_action( 'admin_init', 'add_theme_caps');
  720.  
  721. //show only links assigned to current writer
  722. function only_show_assigned_links($query) {
  723.  
  724. // Make sure we're in the admin and it's the main query
  725. if ( !is_admin() && !$query->is_main_query() ) {
  726. return;
  727. }
  728.  
  729. if ( !in_array( 'writer', (array) wp_get_current_user()->roles ) ) {
  730. return;
  731. }
  732.  
  733. global $typenow;
  734.  
  735. // Only do this for pages
  736. if ( 'link' == $typenow) {
  737.  
  738. if(get_current_screen()->id != 'link'){
  739. $previous_meta_query = $query->get('meta_query');
  740.  
  741. array_push($previous_meta_query, array(
  742. 'relation' => 'AND',
  743. array(
  744. 'key' => 'writer',
  745. 'value' => get_current_user_id(),
  746. 'compare' => '='
  747. ),
  748. array(
  749. 'key' => 'status',
  750. 'value' => array('Article Ordered', 'To Improve'),
  751. 'compare' => 'IN'
  752. )
  753. ));
  754.  
  755. $query->set('meta_query', $previous_meta_query);
  756.  
  757. }
  758.  
  759. return;
  760. }
  761.  
  762. }
  763. add_action('pre_get_posts', 'only_show_assigned_links');
  764.  
  765.  
  766. //freelancer show only links in his region
  767. function only_show_region_links($query) {
  768.  
  769. // Make sure we're in the admin and it's the main query
  770. if ( !is_admin() && !$query->is_main_query() ) {
  771. return;
  772. }
  773.  
  774. if ( !in_array( 'freelancer', (array) wp_get_current_user()->roles ) ) {
  775. return;
  776. }
  777.  
  778. //get assigned market to current freelancer
  779. $markets = get_user_meta(get_current_user_ID(), "markets", true);
  780.  
  781. global $typenow;
  782.  
  783. // Only do this for pages
  784. if ( 'link' == $typenow) {
  785.  
  786. if(get_current_screen()->id != 'link'){
  787. $query->set( 'meta_query', array(
  788. array(
  789. 'key' => 'market',
  790. 'value' => $markets,
  791. 'compare' => 'IN',
  792. )
  793. ) );
  794. }
  795.  
  796. return;
  797.  
  798. }
  799.  
  800. }
  801. add_action('pre_get_posts', 'only_show_region_links');
  802.  
  803. //hide fields for writer
  804. function writer_hide_fields($field) {
  805. $post = get_post();
  806.  
  807. if ( in_array( 'writer', (array) wp_get_current_user()->roles ) ) {
  808. return null;
  809. }
  810.  
  811. return $field;
  812. }
  813.  
  814. add_filter('acf/load_field/name=client', 'writer_hide_fields');
  815. add_filter('acf/load_field/name=publisher', 'writer_hide_fields');
  816. add_filter('acf/load_field/name=publisher_fee', 'writer_hide_fields');
  817. add_filter('acf/load_field/name=source', 'writer_hide_fields');
  818. add_filter('acf/load_field/name=link_type', 'writer_hide_fields');
  819. add_filter('acf/load_field/name=writer_cost', 'writer_hide_fields');
  820. add_filter('acf/load_field/name=writer_paid', 'writer_hide_fields');
  821. add_filter('acf/load_field/name=author', 'writer_hide_fields');
  822. // add_filter('acf/load_field/name=assets', 'writer_hide_fields');
  823. add_filter('acf/load_field/name=guest_post', 'writer_hide_fields');
  824. add_filter('acf/load_field/name=client_price', 'writer_hide_fields');
  825. add_filter('acf/load_field/name=our_discounted_fee', 'writer_hide_fields');
  826. add_filter('acf/load_field/name=invoice_no', 'writer_hide_fields');
  827. add_filter('acf/load_field/name=writer', 'writer_hide_fields');
  828. add_filter('acf/load_field/name=publisher_paid', 'writer_hide_fields');
  829. add_filter('acf/load_field/name=notes', 'writer_hide_fields');
  830. add_filter('acf/load_field/name=market', 'writer_hide_fields');
  831. add_filter('acf/load_field/name=client_url_2', 'writer_hide_fields');
  832.  
  833. //hide fields for freelancer
  834. function freelancer_hide_fields($field) {
  835. $post = get_post();
  836.  
  837. if ( in_array( 'freelancer', (array) wp_get_current_user()->roles ) ) {
  838. return null;
  839. }
  840.  
  841. return $field;
  842. }
  843.  
  844. add_filter('acf/load_field/name=market', 'freelancer_hide_fields');
  845.  
  846.  
  847. //read only
  848. function writer_read_only($field) {
  849. $post = get_post();
  850. if(!empty($post)){
  851. if($post->post_type == 'link'){
  852. if ( in_array( 'writer', (array) wp_get_current_user()->roles ) ) {
  853. $field['disabled'] = 1;
  854.  
  855. $field['disabled'] = array(
  856. 'approve',
  857. 'improve',
  858. );
  859. return $field;
  860. }
  861. }
  862. }
  863.  
  864. return $field;
  865. }
  866. add_filter('acf/load_field/name=anchor_text', 'writer_read_only');
  867. add_filter('acf/load_field/name=anchor_text_2', 'writer_read_only');
  868. add_filter('acf/load_field/name=date_live', 'writer_read_only');
  869. add_filter('acf/load_field/name=word_count', 'writer_read_only');
  870. add_filter('acf/load_field/name=client_url', 'writer_read_only');
  871. add_filter('acf/load_field/name=live_url', 'writer_read_only');
  872. add_filter('acf/load_field/name=article_title', 'writer_read_only');
  873. add_filter('acf/load_field/name=improvement_comments', 'writer_read_only');
  874. // add_filter('acf/load_field/name=status', 'writer_read_only');
  875. add_filter('acf/load_field/name=approve_improve', 'writer_read_only');
  876. add_filter('acf/load_field/name=link_in', 'writer_read_only');
  877. add_filter('acf/load_field/name=notes_to_writer', 'writer_read_only');
  878. add_filter('acf/load_field/name=date_updated', 'writer_read_only');
  879. add_filter('acf/load_field/name=unique_code', 'writer_read_only');
  880. add_filter('acf/load_field/name=article_document', 'writer_read_only');
  881. add_filter('acf/load_field/name=invoice_url', 'writer_read_only');
  882.  
  883.  
  884. // Restrict entire pages for logged in users only
  885. function restrict_page_to_logged_in_user() {
  886.  
  887. // Get global post
  888. global $post;
  889. $user = wp_get_current_user();
  890.  
  891. $restricted_pages = array();
  892. array_push($restricted_pages,
  893. "publishers",
  894. "tasks",
  895. "black-list",
  896. "favourites",
  897. "requests",
  898. "invoices",
  899. "orders"
  900. );
  901.  
  902. $redirect = true;
  903.  
  904. $post_slug = $post->post_name;
  905.  
  906. if(is_page() && in_array($post_slug, $restricted_pages)){
  907. if ( is_user_logged_in() ) {
  908. if ( in_array( 'administrator', (array) $user->roles ) ) {
  909. $redirect = false;
  910. }
  911.  
  912. if ( in_array( 'links_client', (array) $user->roles ) ) {
  913. $redirect = false;
  914. }
  915.  
  916. if ( in_array( 'monthly_client', (array) $user->roles ) ) {
  917. if($post_slug == 'orders'){
  918. $redirect = false;
  919. }
  920. }
  921. }
  922.  
  923. }else {
  924. $redirect = false;
  925. }
  926.  
  927. // foreach ($restricted_pages as $page){
  928. // $page_id = get_page_by_path($page)->ID;
  929.  
  930. // if ( is_page() && ( $post->post_parent == $page_id || is_page( $page_id ) ) ) {
  931.  
  932. // // Set redirect to true by default
  933.  
  934. // // If logged in do nor redirect
  935. // // You can/should place additional checks here based on user roles or user meta
  936. // if ( is_user_logged_in() ) {
  937. // if ( in_array( 'administrator', (array) $user->roles ) ) {
  938. // $redirect = false;
  939. // }
  940.  
  941. // if ( in_array( 'links_client', (array) $user->roles ) ) {
  942. // $redirect = false;
  943. // }
  944. // }
  945. // }
  946. // }
  947.  
  948. // Redirect people witout access to homepage
  949. if ( $redirect ) {
  950. wp_redirect( esc_url( home_url( '/' ) ), 307 );
  951. }
  952. }
  953. add_action( 'template_redirect', 'restrict_page_to_logged_in_user' );
  954.  
  955.  
  956.  
  957. // function add_roles() {
  958. // add_role( 'user_download', 'User Download', array( 'read' => true, 'level_0' => true ) );
  959. // add_role( 'user', 'User', array( 'read' => true, 'level_0' => true ) );
  960. // }
  961. // add_action( 'init', 'add_roles' );
  962.  
  963.  
  964.  
  965. function remove_admin_bar() {
  966. if (current_user_can('links_client') || current_user_can('monthly_client') && !is_admin()) {
  967. show_admin_bar(false);
  968. }
  969. }
  970.  
  971. add_action('after_setup_theme', 'remove_admin_bar');
  972.  
  973. add_action('wp_logout','logout');
  974. function logout(){
  975. wp_redirect( home_url() );
  976. exit();
  977. }
  978.  
  979. //remove logout confirmation
  980. add_action('check_admin_referer', 'logout_without_confirm', 10, 2);
  981.  
  982. function logout_without_confirm($action, $result) {
  983. // Allow logout without confirmation
  984. if ($action == "log-out" && !isset($_GET['_wpnonce'])) {
  985. $redirect_to = isset($_REQUEST['redirect_to']) ? $_REQUEST['redirect_to'] : '';
  986. $location = str_replace('&amp;', '&', wp_logout_url($redirect_to));;
  987. header("Location: $location");
  988. die;
  989. }
  990. }
  991.  
  992. function force_post_categ_init()
  993. {
  994. wp_enqueue_script('jquery');
  995. }
  996. function force_post_categ()
  997. {
  998. echo "<script type='text/javascript'>\n";
  999. echo "
  1000. jQuery('#publish').click(function()
  1001. {
  1002. var cats = jQuery('[id^=\"source\"]')
  1003. .find('.selectit')
  1004. .find('input');
  1005. category_selected=false;
  1006. for (counter=0; counter<cats.length; counter++)
  1007. {
  1008. if (cats.get(counter).checked==true)
  1009. {
  1010. category_selected=true;
  1011. break;
  1012. }
  1013. }
  1014. if(cats.length){
  1015. if(category_selected==false)
  1016. {
  1017. alert('You have not selected any Source for the Link. Please select a Source.');
  1018. setTimeout(\"jQuery('#ajax-loading').css('visibility', 'hidden');\", 100);
  1019. jQuery('[id^=\"taxonomy\"]').find('.tabs-panel').css('background', '#F96');
  1020. setTimeout(\"jQuery('#publish').removeClass('button-primary-disabled');\", 100);
  1021. return false;
  1022. }
  1023. }
  1024.  
  1025. var publisherCats = jQuery('[id^=\"categorychecklist\"]')
  1026. .find('.selectit')
  1027. .find('input');
  1028. category_selected=false;
  1029. for (counter=0; counter<publisherCats.length; counter++)
  1030. {
  1031. if (publisherCats.get(counter).checked==true)
  1032. {
  1033. category_selected=true;
  1034. break;
  1035. }
  1036. }
  1037. if(publisherCats.length){
  1038. if(category_selected==false)
  1039. {
  1040. alert('You have not selected any Category for the Publisher. Please select a Category.');
  1041. setTimeout(\"jQuery('#ajax-loading').css('visibility', 'hidden');\", 100);
  1042. jQuery('[id^=\"taxonomy\"]').find('.tabs-panel').css('background', '#F96');
  1043. setTimeout(\"jQuery('#publish').removeClass('button-primary-disabled');\", 100);
  1044. return false;
  1045. }
  1046. }
  1047. });
  1048. ";
  1049. echo "</script>\n";
  1050. }
  1051. add_action('admin_init', 'force_post_categ_init');
  1052. add_action('edit_form_advanced', 'force_post_categ');
  1053.  
  1054.  
  1055.  
  1056. //remove Add New Links Button
  1057. function disable_new_posts() {
  1058. // Hide sidebar link
  1059. // global $submenu;
  1060. // unset($submenu['post-new.php?post_type=link'][10]);
  1061.  
  1062. // Hide link on listing page
  1063. if ( in_array( 'writer', (array) wp_get_current_user()->roles ) ) {
  1064. echo '<style type="text/css">
  1065. .page-title-action { display: none }
  1066.  
  1067. #menu-posts-link .wp-submenu li:last-child { display: none }
  1068.  
  1069. </style>';
  1070. }
  1071.  
  1072. }
  1073. add_action('admin_init', 'disable_new_posts');
  1074.  
  1075.  
  1076. //update prices when publisher is updated
  1077. // add_action('save_post_publisher', 'update_publisher_prices', 10, 3);
  1078. // function update_publisher_prices($post_ID, $post_after, $post_before){
  1079. // $links_assigned_to_publisher = get_posts(
  1080. // array(
  1081. // 'post_type' => 'link',
  1082. // 'numberposts' => -1,
  1083. // 'meta_query' => array(
  1084. // array(
  1085. // 'key' => 'publisher',
  1086. // 'value' => $post_ID,
  1087. // 'compare' => '=',
  1088. // )
  1089. // )
  1090. // )
  1091. // );
  1092.  
  1093. // var_dump($post_after);
  1094.  
  1095. // var_dump($links_assigned_to_publisher);
  1096.  
  1097. // foreach($links_assigned_to_publisher as $link) {
  1098. // //our fee
  1099. // $our_fee = get_field("fee", $post_ID);
  1100. // $market = get_field("market", $link->ID);
  1101. // var_dump($our_fee);
  1102. // var_dump($market);
  1103. // }
  1104.  
  1105. // die();
  1106. // }
  1107.  
  1108. add_action('updated_post_meta', 'update_publisher_prices', 10, 4);
  1109. function update_publisher_prices($meta_id, $object_id, $meta_key, $meta_value){
  1110.  
  1111. // if($meta_key == "fee"){
  1112. // $links_assigned_to_publisher = get_posts(
  1113. // array(
  1114. // 'post_type' => 'link',
  1115. // 'numberposts' => -1,
  1116. // 'meta_query' => array(
  1117. // array(
  1118. // 'key' => 'publisher',
  1119. // 'value' => $object_id,
  1120. // 'compare' => '=',
  1121. // )
  1122. // )
  1123. // )
  1124. // );
  1125.  
  1126. // if(isset($links_assigned_to_publisher)){
  1127. // foreach($links_assigned_to_publisher as $link) {
  1128. // //our fee
  1129. // $our_fee = $meta_value;
  1130. // $market = get_field("market", $link->ID);
  1131.  
  1132. // if($market == "EN"){
  1133. // $total_fee = $our_fee + 150;
  1134. // }else {
  1135. // $total_fee = $our_fee+ 220;
  1136. // }
  1137.  
  1138. // update_field("client_price", $total_fee, $link->ID);
  1139. // }
  1140. // }
  1141. // }
  1142.  
  1143. if($meta_key == "publisher_fees"){
  1144. $links_assigned_to_publisher = get_posts(
  1145. array(
  1146. 'post_type' => 'link',
  1147. 'numberposts' => -1,
  1148. 'meta_query' => array(
  1149. array(
  1150. 'key' => 'publisher',
  1151. 'value' => $object_id,
  1152. 'compare' => '=',
  1153. )
  1154. )
  1155. )
  1156. );
  1157.  
  1158. if(isset($links_assigned_to_publisher)){
  1159. foreach($links_assigned_to_publisher as $link) {
  1160. //our fee
  1161. $publisher_fees = $meta_value;
  1162.  
  1163. update_field("publisher_fee", $publisher_fees, $link->ID);
  1164. }
  1165. }
  1166.  
  1167. }
  1168. }
  1169.  
  1170.  
  1171.  
  1172. //create ajax function to check blacklist
  1173. add_action("wp_ajax_check_black_list", "check_black_list");
  1174.  
  1175. function check_black_list() {
  1176.  
  1177. if(isset($_REQUEST["publisher_url"]) && isset($_REQUEST["url"]) && isset($_REQUEST["client_id"])){
  1178. $args = array(
  1179. 'numberposts' => -1,
  1180. 'post_type'=> 'link',
  1181. 'meta_query' => array(
  1182. 'relation' => 'AND',
  1183. array(
  1184. 'key' => 'publisher',
  1185. 'value' => $_REQUEST['publisher_url'],
  1186. 'type' => 'numeric',
  1187. 'compare' => 'IN',
  1188. ),
  1189. array(
  1190. 'key' => 'client',
  1191. 'value' => $_REQUEST['client_id'],
  1192. 'type' => 'numeric',
  1193. 'compare' => 'IN',
  1194. ),
  1195. )
  1196. );
  1197. $links = get_posts($args);
  1198.  
  1199. $publisher_url = get_field("domain", $_REQUEST["publisher_url"] );
  1200. $publisher_url = strip_url($publisher_url);
  1201.  
  1202. $inputted_url = $_REQUEST["url"];
  1203. $inputted_url = strip_url($inputted_url);
  1204.  
  1205. $connection_exists = false;
  1206. foreach($links as $link){
  1207. $client_url = get_field("client_url", $link->ID);
  1208. $client_url = strip_url($client_url);
  1209.  
  1210. $result["inputted_url"] = $inputted_url;
  1211. $result["client_url"] = $client_url;
  1212.  
  1213. if($inputted_url == $client_url){
  1214. $connection_exists = true;
  1215. }
  1216. }
  1217.  
  1218. //check in blacklist
  1219. //get blacklist for user
  1220. $blacklist = get_user_meta($_REQUEST["client_id"], 'blacklist', true);
  1221. $result["blacklist"] = $blacklist;
  1222.  
  1223. $result["publisher_url"] = $publisher_url;
  1224. $result["inputted_url"] = $inputted_url;
  1225.  
  1226. foreach($blacklist as $list_item){
  1227. $item_publisher = $list_item["publisher_url"];
  1228. $item_publisher = strip_url($item_publisher);
  1229.  
  1230. $item_website = $list_item["website"];
  1231. $item_website = strip_url($item_website);
  1232.  
  1233. if(($item_publisher == $publisher_url) && (strpos($inputted_url, $item_website) !== false)){
  1234. $connection_exists = true;
  1235. }
  1236. }
  1237.  
  1238. $result["connection_exists"] = $connection_exists;
  1239.  
  1240. }
  1241.  
  1242. $result['type'] = "success";
  1243.  
  1244. if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
  1245. $result = json_encode($result);
  1246. echo $result;
  1247. }
  1248. else {
  1249. header("Location: ".$_SERVER["HTTP_REFERER"]);
  1250. }
  1251.  
  1252. die();
  1253.  
  1254. }
  1255.  
  1256. function strip_url($url){
  1257. $url = strip_tags($url);
  1258. $url = str_replace("https://", "", $url);
  1259. $url = str_replace("www.", "", $url);
  1260. $url = str_replace("http://", "", $url);
  1261. $url = str_replace("/", "", $url);
  1262. $url = str_replace(" ", "", $url);
  1263. $url = preg_replace('/\s/', '', $url);
  1264. return $url;
  1265. }
  1266.  
  1267. // Add Post title to custom meta
  1268. add_action( 'transition_post_status', 'duplicate_title', 10, 3 );
  1269.  
  1270. function duplicate_title( $new, $old, $post ) {
  1271. if ( $post->post_type == 'publisher' ) {
  1272. update_post_meta( $post->ID, 'd_title', $post->post_title );
  1273. }
  1274. }
  1275.  
  1276.  
  1277.  
  1278. add_action('wp_ajax_publisher_datatables', 'datatables_server_side_callback');
  1279. add_action('wp_ajax_nopriv_publisher_datatables', 'datatables_server_side_callback');
  1280.  
  1281.  
  1282. function datatables_server_side_callback() {
  1283.  
  1284. header("Content-Type: application/json");
  1285.  
  1286. $request= $_GET;
  1287.  
  1288. $meta_query = [];
  1289. $tax_query = [];
  1290.  
  1291. if(isset($request)){
  1292. if(isset($request['country'])){
  1293. $meta_query[] = array(
  1294. 'key' => 'country',
  1295. 'value' => $request['country'],
  1296. );
  1297. }
  1298.  
  1299. if(isset($request['url'])){
  1300. $meta_query[] = array(
  1301. 'key' => 'domain',
  1302. 'value' => $request['url'],
  1303. 'compare' => 'LIKE',
  1304. );
  1305. }
  1306.  
  1307. if(isset($request['language'])){
  1308. if(!empty($request['language'])){
  1309. $meta_query[] = array(
  1310. 'key' => 'language',
  1311. 'value' => $request['language'],
  1312. 'compare' => 'LIKE',
  1313. );
  1314. }
  1315. }
  1316.  
  1317. if(isset($request['links'])){
  1318. if(!empty($request['links'])){
  1319. $meta_query[] = array(
  1320. 'key' => 'link_type',
  1321. 'value' => $request['links'],
  1322. );
  1323. }
  1324. }
  1325.  
  1326. if(isset($request['traffic'])){
  1327. $traffic_exploded = explode("-", $request['traffic']);
  1328. if(count($traffic_exploded) > 1){
  1329. $meta_query[] = array(
  1330. 'key' => 'organic_traffic',
  1331. 'value' => array($traffic_exploded[0], $traffic_exploded[1]),
  1332. 'type' => 'numeric',
  1333. 'compare' => 'BETWEEN',
  1334. );
  1335. }
  1336. }
  1337.  
  1338. if(isset($request['dr-min']) && isset($request['dr-max'])){
  1339. if($request['dr-min'] != ''){
  1340. $meta_query[] = array(
  1341. 'key' => 'dr',
  1342. 'value' => $request['dr-min'],
  1343. 'type' => 'numeric',
  1344. 'compare' => '>=',
  1345. );
  1346. }
  1347.  
  1348. if($request['dr-max'] != ''){
  1349. $meta_query[] = array(
  1350. 'key' => 'dr',
  1351. 'value' => $request['dr-max'],
  1352. 'type' => 'numeric',
  1353. 'compare' => '<=',
  1354. );
  1355. }
  1356. }
  1357.  
  1358. if(isset($request['rd-min']) && isset($request['rd-max'])){
  1359. if(($request['rd-min'] != '') && ($request['rd-max'] != '')){
  1360. $meta_query[] = array(
  1361. 'key' => 'rd',
  1362. 'value' => array($request['rd-min'], $request['rd-max']),
  1363. 'type' => 'numeric',
  1364. 'compare' => 'BETWEEN',
  1365. );
  1366. }
  1367. }
  1368.  
  1369. if(isset($request['price-min']) && isset($request['price-max'])){
  1370. if($request['price-min'] != ''){
  1371. $meta_query[] = array(
  1372. 'key' => 'fee',
  1373. 'value' => $request['price-min'],
  1374. 'type' => 'numeric',
  1375. 'compare' => '>=',
  1376. );
  1377. }
  1378.  
  1379. if($request['price-max'] != ''){
  1380. $meta_query[] = array(
  1381. 'key' => 'fee',
  1382. 'value' => $request['price-max'],
  1383. 'type' => 'numeric',
  1384. 'compare' => '<=',
  1385. );
  1386. }
  1387. }
  1388.  
  1389. if(isset($request['tf-min']) && isset($request['tf-max'])){
  1390. if(($request['tf-min'] != '') && ($request['tf-max'] != '')){
  1391. $meta_query[] = array(
  1392. 'key' => 'tf',
  1393. 'value' => array($request['tf-min'], $request['tf-max']),
  1394. 'type' => 'numeric',
  1395. 'compare' => 'BETWEEN',
  1396. );
  1397. }
  1398. }
  1399.  
  1400. //get categories assigned to user
  1401. $user_categories = get_field("publisher_categories", "user_".get_current_user_id());
  1402.  
  1403. if(!empty($user_categories)){
  1404. $tax_query[] = array(
  1405. 'taxonomy' => 'category',
  1406. 'field' => 'slug',
  1407. 'terms' => $user_categories,
  1408. 'operator' => 'IN',
  1409. );
  1410. }
  1411.  
  1412. if(isset($request['category'])){
  1413. $tax_query[] = array(
  1414. 'taxonomy' => 'category',
  1415. 'field' => 'name',
  1416. 'terms' => $request['category'],
  1417. 'operator' => 'IN',
  1418. );
  1419. }
  1420.  
  1421. }
  1422.  
  1423. $date_query = array();
  1424.  
  1425. if(isset($request['date-min'])){
  1426. $date_min = strtotime($request['date-min']);
  1427.  
  1428. if(($request['date-min'] != '')){
  1429. $date_query["after"] = array(
  1430. "year" => date('Y', $date_min),
  1431. "month" => date('m', $date_min),
  1432. "day" => date('d', $date_min),
  1433. );
  1434. }
  1435. }
  1436.  
  1437. if(isset($request['date-max'])){
  1438. $date_max = strtotime($request['date-max']);
  1439.  
  1440. if(($request['date-max'] != '')){
  1441. $date_query["before"] = array(
  1442. "year" => date('Y', $date_max),
  1443. "month" => date('m', $date_max),
  1444. "day" => date('d', $date_max),
  1445. );
  1446. }
  1447. }
  1448.  
  1449. $columns = array(
  1450. 0 => 'domain',
  1451. 1 => 'dr',
  1452. 2 => 'rd',
  1453. 3 => 'organic_traffic',
  1454. 5 => 'language',
  1455. 6 => 'link_type',
  1456. 7 => 'fee',
  1457. );
  1458.  
  1459.  
  1460. $args = array(
  1461. 'post_type' => 'publisher',
  1462. 'post_status' => 'publish',
  1463. 'posts_per_page' => $request['length'],
  1464. 'offset' => $request['start'],
  1465. 'order' => $request['order'][0]['dir'],
  1466. 'meta_query' => $meta_query,
  1467. 'tax_query' => $tax_query,
  1468. );
  1469.  
  1470. if ($request['order'][0]['column'] == 0 || $request['order'][0]['column'] == 5 || $request['order'][0]['column'] == 6) {
  1471.  
  1472. $args['orderby'] = 'meta_value';
  1473. $args['meta_key'] = $columns[$request['order'][0]['column']];
  1474.  
  1475. } elseif ($request['order'][0]['column'] == 1 || $request['order'][0]['column'] == 2 || $request['order'][0]['column'] == 3 || $request['order'][0]['column'] == 7) {
  1476.  
  1477. $args['orderby'] = 'meta_value_num';
  1478.  
  1479. $args['meta_key'] = $columns[$request['order'][0]['column']];
  1480. }
  1481.  
  1482. $current_user = wp_get_current_user();
  1483. $previous_favs = get_user_meta($current_user->ID, 'fav_publishers', true);
  1484.  
  1485. if($request['favs_only'] == 1){
  1486. if(empty($previous_favs)){
  1487. $previous_favs = array(-1);
  1488. }
  1489.  
  1490. $args['post__in'] = $previous_favs;
  1491. }
  1492.  
  1493. $publisher_query = new WP_Query($args);
  1494. $totalData = $publisher_query->found_posts;
  1495.  
  1496. $currency = get_field("currency", "user_".$current_user->ID);
  1497. if(empty($currency)){
  1498. $currency = "€";
  1499. }
  1500.  
  1501. $allowDomainViewFav = get_field("allow_domain_name_view_fav", "user_".$current_user->ID);
  1502. $allowDomainViewPub = get_field("allow_domain_name_view_publishers", "user_".$current_user->ID);
  1503.  
  1504.  
  1505. if ( $publisher_query->have_posts() ) {
  1506.  
  1507. while ( $publisher_query->have_posts() ) {
  1508.  
  1509. $publisher_query->the_post();
  1510.  
  1511. $hidden_url = str_replace("www.","",( str_replace("https://", "", get_field("domain"))));
  1512. $exploded_url = explode('.', $hidden_url);
  1513.  
  1514.  
  1515.  
  1516. if($request['favs_only'] == 1){
  1517. if ( $allowDomainViewFav != 1 ) {
  1518. $exploded_url[0] = substr($exploded_url[0], 0, 2) . str_repeat("*", strlen($exploded_url[0]) - 2);
  1519. }
  1520. }else {
  1521. if ( $allowDomainViewPub != 1 ) {
  1522. $exploded_url[0] = substr($exploded_url[0], 0, 2) . str_repeat("*", strlen($exploded_url[0]) - 2);
  1523. }
  1524. }
  1525.  
  1526. $hidden_url = implode('.', $exploded_url);
  1527.  
  1528. $domain_html = '<div class="flex items-center justify-between" data-enable-view-fab="'.$allowDomainViewFav.'" data-enable-view-pub="'.$allowDomainViewPub.'">';
  1529. // $domain_html .= '<a target="_blank" class="url" href="'.addhttp(get_field('domain')).'">'.$hidden_url.'</a>';
  1530. $domain_html .= '<p target="_blank" class="url">'.$hidden_url.'</p>';
  1531. $domain_html .= '<form action="" method="POST" >
  1532. <input type="hidden" value="'.get_the_ID().'" name="pub">
  1533. </form>';
  1534.  
  1535. if(empty($previous_favs)){
  1536. $previous_favs = array();
  1537. }
  1538.  
  1539. //check if favourite
  1540. $fav_classes = "";
  1541. if(in_array(get_the_ID(), $previous_favs)){
  1542. $fav_classes .= "selected";
  1543. }
  1544.  
  1545. $domain_html .= '
  1546. <div class="star '.$fav_classes.'" title="ADD TO FAVOURITES" data-publisher="'.get_the_ID().'">
  1547. <svg version="1.1"
  1548. xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:a="http://ns.adobe.com/AdobeSVGViewerExtensions/3.0/"
  1549. x="0px" y="0px" width="26.1px" height="24.9px" viewBox="0 0 26.1 24.9"
  1550. style="overflow:scroll;enable-background:new 0 0 26.1 24.9;" xml:space="preserve">
  1551. <style type="text/css">
  1552. .st0{fill:#E9704A00;stroke:#E9704A;stroke-miterlimit:10;transition: fill 0.2s ease;}
  1553. </style>
  1554. <defs>
  1555. </defs>
  1556. <path class="st0" d="M13.6,0.9l3.2,7.5L25,9.1c0.4,0,0.6,0.3,0.6,0.7c0,0.2-0.1,0.3-0.2,0.4l-6.2,5.4l1.9,8c0.1,0.3-0.1,0.7-0.5,0.8
  1557. c-0.2,0-0.3,0-0.5-0.1l-7-4.2l-7,4.2c-0.3,0.2-0.7,0.1-0.9-0.2C5,23.9,5,23.8,5.1,23.6l1.9-8l-6.2-5.4C0.5,10,0.4,9.6,0.7,9.4
  1558. c0.1-0.1,0.3-0.2,0.4-0.2l8.2-0.7l3.2-7.5c0.1-0.3,0.5-0.5,0.8-0.3C13.4,0.6,13.6,0.7,13.6,0.9z"/>
  1559. </svg>
  1560. </div>';
  1561.  
  1562. $domain_html .= '</div>';
  1563.  
  1564. $categories_html = '';
  1565. $categories = get_the_terms(get_the_ID(), 'category');
  1566. $len = count($categories);
  1567. foreach ($categories as $key=>$category) {
  1568. $categories_html.= $category->name;
  1569.  
  1570. if($key !== $len-1){
  1571. $categories_html.= ', ';
  1572. }
  1573. }
  1574.  
  1575. $language_html = '';
  1576. $language = get_field("language");
  1577. if (is_array($language)){
  1578. $language_html .= implode(', ', $language);
  1579. } else {
  1580. $language_html.=$language;
  1581. }
  1582.  
  1583. $links_html= '';
  1584. $link_type = get_field("link_type");
  1585. if (is_array($link_type)){
  1586. $links_html.=$link_type['label'];
  1587. }else{
  1588. $links_html.=$link_type;
  1589. }
  1590.  
  1591. $nestedData = array();
  1592. $nestedData[] = $domain_html;
  1593. $nestedData[] = number_format(get_field('dr'));
  1594. $nestedData[] = number_format(get_field('rd'));
  1595. $nestedData[] = number_format(get_field('organic_traffic'));
  1596. $nestedData[] = $categories_html;
  1597. $nestedData[] = $language_html;
  1598. $nestedData[] = $links_html;
  1599. $nestedData[] = $currency.number_format(get_field('fee'));
  1600. $nestedData[] = '<a href="'.get_permalink().'" class="order">ORDER</a>';
  1601.  
  1602. $data[] = $nestedData;
  1603.  
  1604. }
  1605.  
  1606. wp_reset_query();
  1607.  
  1608. $json_data = array(
  1609. "draw" => intval($request['draw']),
  1610. "recordsTotal" => intval($totalData),
  1611. "recordsFiltered" => intval($totalData),
  1612. "data" => $data
  1613. );
  1614.  
  1615. echo json_encode($json_data);
  1616.  
  1617. } else {
  1618.  
  1619. $json_data = array(
  1620. "data" => array()
  1621. );
  1622.  
  1623. echo json_encode($json_data);
  1624. }
  1625.  
  1626. wp_die();
  1627.  
  1628. }
  1629.  
  1630.  
  1631.  
  1632. add_action('wp_ajax_orders_datatables', 'datatables_server_side_callback_orders');
  1633. add_action('wp_ajax_nopriv_orders_datatables', 'datatables_server_side_callback_orders');
  1634.  
  1635.  
  1636. function datatables_server_side_callback_orders() {
  1637.  
  1638. header("Content-Type: application/json");
  1639.  
  1640. $request= $_GET;
  1641.  
  1642. $columns = array(
  1643. 0 => 'date',
  1644. 1 => 'client_url',
  1645. 2 => 'publisher',
  1646. 3 => 'unique_code',
  1647. 4 => 'client_price',
  1648. 5 => 'anchor_text',
  1649. 6 => 'anchor_text_2',
  1650. 7 => 'live_url',
  1651. 8 => 'status',
  1652. );
  1653.  
  1654. if(App::isMonthlyClient()){
  1655. $columns = array(
  1656. 0 => 'date',
  1657. 1 => 'client_url',
  1658. 2 => 'publisher',
  1659. 3 => 'unique_code',
  1660. 4 => 'anchor_text',
  1661. 5 => 'anchor_text_2',
  1662. 6 => 'live_url',
  1663. 7 => 'status',
  1664. );
  1665. }
  1666.  
  1667. $current_user = wp_get_current_user();
  1668.  
  1669. $meta_query = array(
  1670. 'relation' => 'AND'
  1671. );
  1672.  
  1673. $meta_query[] = array(
  1674. 'key' => 'client',
  1675. 'value' => $current_user->ID,
  1676. );
  1677.  
  1678. if(current_user_can("monthly_client")){
  1679. $meta_query[] = array(
  1680. 'key' => 'status',
  1681. 'value' => array("Pitch Accepted", "Article Ordered", "Article Sent", "Live"),
  1682. 'compare' => 'IN',
  1683. );
  1684. }
  1685.  
  1686. if(!empty($request['search']['value'])){
  1687. $meta_query[] = array(
  1688. 'key' => 'status',
  1689. 'value' => sanitize_text_field($request['search']['value']),
  1690. 'compare' =>'LIKE',
  1691. );
  1692. }
  1693.  
  1694. $args = array(
  1695. 'post_type' => 'link',
  1696. 'post_status' => 'publish',
  1697. 'posts_per_page' => $request['length'],
  1698. 'offset' => $request['start'],
  1699. 'order' => $request['order'][0]['dir'],
  1700. 'meta_query' => $meta_query,
  1701. 'tax_query' => $tax_query
  1702. );
  1703.  
  1704. if ($request['order'][0]['column'] == 1 || $request['order'][0]['column'] == 3 || $request['order'][0]['column'] == 5 || $request['order'][0]['column'] == 6 || $request['order'][0]['column'] == 7 || $request['order'][0]['column'] == 8) {
  1705.  
  1706. $args['orderby'] = 'meta_value';
  1707. $args['meta_key'] = $columns[$request['order'][0]['column']];
  1708.  
  1709. } elseif ( $request['order'][0]['column'] == 4) {
  1710.  
  1711. $args['orderby'] = 'meta_value_num';
  1712. $args['meta_key'] = $columns[$request['order'][0]['column']];
  1713.  
  1714. } elseif ( $request['order'][0]['column'] == 0 ) {
  1715.  
  1716. $args['orderby'] = $columns[$request['order'][0]['column']];
  1717.  
  1718. }elseif ( $request['order'][0]['column'] == 2 ) {
  1719.  
  1720. // $args['orderby'] = get_field('domain', $columns[$request['order'][0]['column']]);
  1721. $args['orderby'] = 'meta_value_num';
  1722. $args['meta_key'] = $columns[$request['order'][0]['column']];
  1723. }
  1724.  
  1725. $orders_query = new WP_Query($args);
  1726. $totalData = $orders_query->found_posts;
  1727.  
  1728. $currency = get_field("currency", "user_".$current_user->ID);
  1729. if(empty($currency)){
  1730. $currency = "€";
  1731. }
  1732.  
  1733. if ( $orders_query->have_posts() ) {
  1734.  
  1735. while ( $orders_query->have_posts() ) {
  1736.  
  1737. $orders_query->the_post();
  1738.  
  1739. $date = DateTime::createFromFormat('Ymd', get_field("date_updated", get_the_ID()));
  1740. $date_formatted = get_field("date_updated", get_the_ID());
  1741. if($date){
  1742. $date_formatted = $date->format('d/m/Y') ;
  1743. }
  1744.  
  1745. $invoice_id = get_field("invoice_no", get_the_ID());
  1746. $invoice_file = get_field("invoice", $invoice_id);
  1747. $invoice_url = $invoice_file["url"];
  1748.  
  1749. //need if here
  1750. $status_html = '<td class="ttu status tr'.(get_field("status", get_the_ID())=='Live') ? "green" : "" .'">'.get_field("status", get_the_ID()).'</td>';
  1751.  
  1752. $nestedData = array();
  1753. $nestedData[] = '<div>'.$date_formatted.'</div>';
  1754. $nestedData[] = '<div>'.get_field('client_url').'</div>';
  1755. $nestedData[] = '<div>'.get_field("domain", get_field("publisher")).'</div>';
  1756. $nestedData[] = '<div class="tc">'.get_field('unique_code').'</div>';
  1757. if(!App::isMonthlyClient()){
  1758. if(get_field('our_discounted_fee')){
  1759. $nestedData[] = '<div class="tc">'.$currency.number_format(get_field('our_discounted_fee')).'</div>';
  1760. }else{
  1761. $nestedData[] = '<div class="tc">'.$currency.number_format(get_field('client_price')).'</div>';
  1762. }
  1763. }
  1764. $nestedData[] = '<div>'.get_field('anchor_text').'</div>';
  1765. $nestedData[] = '<div>'.get_field('anchor_text_2').'</div>';
  1766. $nestedData[] = '<a target="_blank" class="live-url-wrapper" href="'.addhttp(get_field('live_url')).'"><div class="live-url">'.get_field('live_url').'</div></a>';
  1767. $nestedData[] = '<div class="ttu status tr '.(get_field("status") == 'Live' ? "green" : "").' ">'.get_field("status").'</div>';
  1768.  
  1769. $data[] = $nestedData;
  1770.  
  1771. }
  1772.  
  1773. wp_reset_query();
  1774.  
  1775. $json_data = array(
  1776. "draw" => intval($request['draw']),
  1777. "recordsTotal" => intval($totalData),
  1778. "recordsFiltered" => intval($totalData),
  1779. "data" => $data
  1780. );
  1781.  
  1782. echo json_encode($json_data);
  1783.  
  1784. } else {
  1785.  
  1786. $json_data = array(
  1787. "data" => array()
  1788. );
  1789.  
  1790. echo json_encode($json_data);
  1791. }
  1792.  
  1793. wp_die();
  1794.  
  1795. }
  1796.  
  1797. function addhttp($url) {
  1798. if (!preg_match("~^(?:f|ht)tps?://~i", $url)) {
  1799. $url = "http://" . $url;
  1800. }
  1801. return $url;
  1802. }
  1803.  
  1804. add_action( 'load-profile.php', function() {
  1805. if ( !in_array( 'administrator', (array) wp_get_current_user()->roles ) ) {
  1806. remove_menu_page( 'load-profile.php' );
  1807. exit( wp_safe_redirect( admin_url() ) );
  1808. }
  1809. } );
  1810.  
  1811. function hide_personal_options(){
  1812. if ( !in_array( 'administrator', (array) wp_get_current_user()->roles ) ) {
  1813. echo "\n" . '<script type="text/javascript">jQuery(document).ready(function($) { $(\'#menu-users\').hide(); });</script>' . "\n";
  1814. }
  1815. }
  1816. add_action('admin_head','hide_personal_options');
  1817.  
  1818.  
  1819. add_filter('disable_categories_dropdown', function(){
  1820. return true;
  1821. });
  1822.  
  1823.  
  1824.  
  1825. include 'inc/admin-filters.php';
  1826. include 'inc/admin-columns.php';
  1827. include 'inc/admin-links.php';
  1828. include 'inc/admin-publishers.php';
  1829.  
  1830. // include 'css/admin-css.php';
  1831.  
  1832. // wp_enqueue_style( 'seo-keen', plugins_url( '/css/admin.css', __FILE__ ) );
  1833.  
  1834.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement