| 1 |
<?php |
| 2 |
namespace BetterLinks\Tools; |
| 3 |
if ( ! defined( 'ABSPATH' ) ) { exit; } |
| 4 |
|
| 5 |
// phpcs:disable PluginCheck.Security.DirectDB, WordPress.DB.DirectDatabaseQuery, WordPress.DB.PreparedSQL |
| 6 |
|
| 7 |
class Export { |
| 8 |
|
| 9 |
public function __construct() { |
| 10 |
add_action( 'admin_init', array( $this, 'export_data' ) ); |
| 11 |
} |
| 12 |
public function export_data() { |
| 13 |
$can_access_settings = apply_filters( 'betterlinks/admin/' . BETTERLINKS_PLUGIN_SLUG . '-settings_menu_capability', 'manage_options' ); |
| 14 |
$nonce = isset( $_GET['nonce'] ) ? sanitize_text_field( wp_unslash( $_GET['nonce'] ) ) : ''; |
| 15 |
if ( ! wp_verify_nonce( $nonce, 'betterlinks_admin_nonce' ) || ! is_user_logged_in() || ! current_user_can( $can_access_settings ) ) { |
| 16 |
return false; |
| 17 |
} |
| 18 |
$page = isset( $_GET['page'] ) ? sanitize_text_field( wp_unslash( $_GET['page'] ) ) : ''; |
| 19 |
$export = isset( $_GET['export'] ) ? sanitize_text_field( wp_unslash( $_GET['export'] ) ) : false; |
| 20 |
if ( 'betterlinks-settings' === $page && true == $export ) { |
| 21 |
$type = isset( $_POST['content'] ) ? sanitize_text_field( wp_unslash( $_POST['content'] ) ) : ''; |
| 22 |
$this->download_files( $type ); |
| 23 |
exit(); |
| 24 |
} |
| 25 |
} |
| 26 |
|
| 27 |
public function download_files( $type ) { |
| 28 |
$data = array(); |
| 29 |
$filename = 'betterlinks'; |
| 30 |
if ( 'links' === $type ) { |
| 31 |
$links = $this->get_links(); |
| 32 |
$data = $this->prepare_csv_file_data( $links ); |
| 33 |
} elseif ( 'clicks' === $type ) { |
| 34 |
$clicks = $this->get_clicks(); |
| 35 |
$data = $this->prepare_csv_file_data( $clicks ); |
| 36 |
$filename .= '-clicks'; |
| 37 |
} else { |
| 38 |
$filename = 'Sample-file'; |
| 39 |
$data = $this->simple_file_download(); |
| 40 |
} |
| 41 |
$filename .= '.' . gmdate( 'Y-m-d' ) . '.csv'; |
| 42 |
$this->array_to_csv_download( |
| 43 |
$data, |
| 44 |
$filename |
| 45 |
); |
| 46 |
} |
| 47 |
|
| 48 |
public function array_to_csv_download( $arr, $filename = 'export.csv' ) { |
| 49 |
header( 'Content-Type: application/csv' ); |
| 50 |
header( 'Content-Disposition: attachment; filename="' . $filename . '";' ); |
| 51 |
$f = fopen( 'php://output', 'w' ); |
| 52 |
foreach ( $arr as $line ) { |
| 53 |
fputcsv( $f, $line, ',', '"' ); |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
public function prepare_csv_file_data( $data ) { |
| 58 |
if ( is_array( $data ) && count( $data ) > 0 ) { |
| 59 |
return array_merge( array( array_keys( $data[0] ) ), $data ); |
| 60 |
} |
| 61 |
return array(); |
| 62 |
} |
| 63 |
|
| 64 |
public function get_links() { |
| 65 |
global $wpdb; |
| 66 |
$links = $wpdb->get_results( "SELECT * FROM {$wpdb->prefix}betterlinks", ARRAY_A ); |
| 67 |
$results = array(); |
| 68 |
if ( is_array( $links ) && count( $links ) > 0 ) { |
| 69 |
foreach ( $links as $link ) { |
| 70 |
$terms = $this->get_terms_from_link_id( $link['ID'] ); |
| 71 |
$auto_link_keywords = array( |
| 72 |
'auto_link_keywords' => serialize( $this->get_auto_link_keywords_from_link_id( $link['ID'] ) ), |
| 73 |
); |
| 74 |
|
| 75 |
$results[] = array_merge( $link, $terms, $auto_link_keywords ); |
| 76 |
} |
| 77 |
} |
| 78 |
return $results; |
| 79 |
} |
| 80 |
|
| 81 |
public function simple_file_download() { |
| 82 |
$links = array( |
| 83 |
'ID', |
| 84 |
'link_author', |
| 85 |
'link_date', |
| 86 |
'link_date_gmt', |
| 87 |
'link_title', |
| 88 |
'link_slug', |
| 89 |
'link_note', |
| 90 |
'link_status', |
| 91 |
'nofollow', |
| 92 |
'sponsored', |
| 93 |
'track_me', |
| 94 |
'param_forwarding', |
| 95 |
'param_struct', |
| 96 |
'redirect_type', |
| 97 |
'target_url', |
| 98 |
'short_url', |
| 99 |
'link_order', |
| 100 |
'link_modified', |
| 101 |
'link_modified_gmt', |
| 102 |
'wildcards', |
| 103 |
'expire', |
| 104 |
'dynamic_redirect', |
| 105 |
'favorite', |
| 106 |
'uncloaked', |
| 107 |
); |
| 108 |
$current_date = wp_date( 'Y-m-d H:i:s' ); |
| 109 |
$sample_data = array( |
| 110 |
'1', |
| 111 |
'1', |
| 112 |
$current_date, |
| 113 |
$current_date, |
| 114 |
'Example Title Here', |
| 115 |
'example-title-Here', |
| 116 |
'', |
| 117 |
'publish', |
| 118 |
'1', |
| 119 |
'', |
| 120 |
'1', |
| 121 |
'', |
| 122 |
'', |
| 123 |
'307', |
| 124 |
'https://your.site/example', |
| 125 |
'go/example', |
| 126 |
'0', |
| 127 |
$current_date, |
| 128 |
$current_date, |
| 129 |
'0', |
| 130 |
'{"status":0,"type":"date","clicks":"","date":"","redirect_status":0,"redirect_url":""}', |
| 131 |
'{"type":"","value":[],"extra":{"rotation_mode":"weighted","split_test":"","goal_link":""}}', |
| 132 |
'', |
| 133 |
'', |
| 134 |
'', |
| 135 |
'uncategorized', |
| 136 |
serialize( array() ), |
| 137 |
); |
| 138 |
|
| 139 |
if ( is_array( $links ) && count( $links ) > 0 ) { |
| 140 |
array_push( $links, 'tags', 'category', 'auto_link_keywords' ); |
| 141 |
return array( $links, $sample_data ); |
| 142 |
} |
| 143 |
return array(); |
| 144 |
} |
| 145 |
|
| 146 |
public function get_auto_link_keywords_from_link_id( $link_id = 0 ) { |
| 147 |
global $wpdb; |
| 148 |
$query = sprintf( 'SELECT meta_id, meta_key, meta_value FROM %2$sbetterlinkmeta where link_id=%1$s', $link_id, $wpdb->prefix ); |
| 149 |
$auto_link_keywords = $wpdb->get_results( $query, ARRAY_A ); |
| 150 |
return $auto_link_keywords; |
| 151 |
} |
| 152 |
|
| 153 |
public function get_terms_from_link_id( $link_id = 0 ) { |
| 154 |
global $wpdb; |
| 155 |
$category = array(); |
| 156 |
$tags = array(); |
| 157 |
$query = $wpdb->prepare( |
| 158 |
"SELECT * FROM {$wpdb->prefix}betterlinks_terms LEFT JOIN {$wpdb->prefix}betterlinks_terms_relationships ON {$wpdb->prefix}betterlinks_terms.ID = {$wpdb->prefix}betterlinks_terms_relationships.term_id WHERE {$wpdb->prefix}betterlinks_terms_relationships.link_id=%s", |
| 159 |
$link_id |
| 160 |
); |
| 161 |
$terms = $wpdb->get_results( $query, ARRAY_A ); |
| 162 |
if ( is_array( $terms ) && count( $terms ) > 0 ) { |
| 163 |
foreach ( $terms as $term ) { |
| 164 |
if ( 'category' === $term['term_type'] ) { |
| 165 |
$category[] = $term['term_slug']; |
| 166 |
} elseif ( 'tags' === $term['term_type'] ) { |
| 167 |
$tags[] = $term['term_slug']; |
| 168 |
} |
| 169 |
} |
| 170 |
} |
| 171 |
return array( |
| 172 |
'tags' => ( count( $tags ) > 0 ? implode( ',', $tags ) : '' ), |
| 173 |
'category' => ( count( $category ) > 0 ? implode( ',', $category ) : '' ), |
| 174 |
); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
public function get_clicks() { |
| 179 |
global $wpdb; |
| 180 |
$options = json_decode( get_option( BETTERLINKS_LINKS_OPTION_NAME ), true ); |
| 181 |
$ip_tracking = ( isset( $options['is_disable_analytics_ip'] ) && ! $options['is_disable_analytics_ip'] ) ? "{$wpdb->prefix}betterlinks_clicks.ip," : ''; |
| 182 |
$clicks = $wpdb->get_results( |
| 183 |
"SELECT |
| 184 |
{$wpdb->prefix}betterlinks.short_url, |
| 185 |
{$wpdb->prefix}betterlinks.target_url, |
| 186 |
{$ip_tracking} |
| 187 |
{$wpdb->prefix}betterlinks_clicks.browser, |
| 188 |
{$wpdb->prefix}betterlinks_clicks.os, |
| 189 |
{$wpdb->prefix}betterlinks_clicks.referer, |
| 190 |
{$wpdb->prefix}betterlinks_clicks.host, |
| 191 |
{$wpdb->prefix}betterlinks_clicks.uri, |
| 192 |
{$wpdb->prefix}betterlinks_clicks.visitor_id, |
| 193 |
{$wpdb->prefix}betterlinks_clicks.click_order, |
| 194 |
{$wpdb->prefix}betterlinks_clicks.created_at, |
| 195 |
{$wpdb->prefix}betterlinks_clicks.created_at_gmt |
| 196 |
FROM {$wpdb->prefix}betterlinks_clicks LEFT JOIN {$wpdb->prefix}betterlinks ON {$wpdb->prefix}betterlinks_clicks.link_id = {$wpdb->prefix}betterlinks.ID", |
| 197 |
ARRAY_A |
| 198 |
); |
| 199 |
return $clicks; |
| 200 |
} |
| 201 |
} |
| 202 |
|