Browse Source

mmo update

master
Felty 2 years ago
parent
commit
0690e661e0
6 changed files with 338 additions and 1 deletions
  1. +21
    -0
      vidyen-wc-mmo/includes/functions/core/vyps_sql_call_func.php
  2. +145
    -0
      vidyen-wc-mmo/includes/shortcodes/vidyen-mmo-postback.php
  3. +17
    -0
      vidyen-wc-mmo/vidyen-wc-mmo-menu.php
  4. +123
    -0
      vidyen-wc-mmo/vidyen-wc-mmo-template-function.php
  5. +22
    -0
      vidyen-wc-mmo/vidyen-wc-mmo-template.php
  6. +10
    -1
      vidyen-wc-mmo/vidyen-wc-mmo.php

+ 21
- 0
vidyen-wc-mmo/includes/functions/core/vyps_sql_call_func.php View File

@ -90,3 +90,24 @@ function vyps_mmo_sql_output_amount_func()
return $output_amount;
}
/*** API KEY SQL Call ***/
function vyps_mmo_sql_api_key_func()
{
global $wpdb;
//the $wpdb stuff to find what the current name and icons are
$table_name_wc_mmo = $wpdb->prefix . 'vidyen_wc_mmo';
$first_row = 1; //Note sure why I'm setting this.
//Ouput Amount NOTE: For now this is just for WooWallet
$api_key_query = "SELECT api_key FROM ". $table_name_wc_mmo . " WHERE id= %d"; //I'm not sure if this is resource optimal but it works. -Felty
$api_key_query_prepared = $wpdb->prepare( $api_key_query, $first_row );
$api_key = $wpdb->get_var( $api_key_query_prepared );
$api_key = sanitize_text_field($api_key); //Extra sanitzation
return $api_key;
}

+ 145
- 0
vidyen-wc-mmo/includes/shortcodes/vidyen-mmo-postback.php View File

@ -0,0 +1,145 @@
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
/*** adgate short code to make a postback page ***/
//NOTE: As much as I hate post backs, its not hard to do and adgate doesn't have the Adscend point api tracking system (nor like Coinhive)
//Of course since the adgate site won't have a wp login, has to be just just a shortcode with page. And you will have to wait on adgate to talk to your server
//Lots of terrible things can will go wrong, but the demand for this (due to Adscend just being.... well Adscend) keeps happening so I broke down and decided to do this
//regardless of having to use a post back. I will have to do it in a way that is secure etc etc.
function vidyen_mmo_postback_func( $atts )
{
//NOTE: The admin needs to set the post back correctly. We will have no idea what the user id will be as it will be fed into the system by the post back
//We will need the secret
//Also NOTE: I changed pid to outputid because i think going forward pid is a bit nondescriptive
//THis needs to called to get the api key.
$api_key = sanitize_text_field(vyps_mmo_sql_api_key_func());
$atts = shortcode_atts(
array(
'apikey' => $api_key,
'profile' => '',
'ip1' => '163.182.175.208 ',
'ip2' => '163.182.175.208 ',
'ip3' => '208.253.87.210',
'outputid' => 0,
'outputamount' => 0,
'refer' => 0,
'to_user_id' => 0,
'comment' => '',
'reason' => 'MMO Transfer',
'meta_id' => '',
'round' => 'default',
'pro'=> FALSE,
), $atts, 'vyps-adgate' );
$round_direction_decision = $atts['round']; //By default this is default, which just takes the direction its closest too.
//The scarcy thing is... This post back can be writing to your SQL tables. SO we HAVE to be careful with it.
global $wpdb;
$table_name_log = $wpdb->prefix . 'vyps_points_log';
//This is the point id that the post back should go to. ie. the Point ID
if ( $atts['outputid'] == 0 )
{
return "You did not set output point ID! outputid=";
}
//NOTE: Due to the lax nature of AdGate security methods. I am adding my own API system.
//EX: https://vidyen.com/fabius/adgate-postback/?tx_id={transaction_id}&user_id={s1}&point_value={points}&usd_value={payout}&offer_title={vc_titlpoe}&point_value={points}&status={status}&api=7xB944
//The api=7xB944 has to be the same on both your shortcode and post back. Its not required, but if you set a shortcode for it. Then it has to have it.
$site_api_key = $atts['apikey'];
//Copied and pasted from https://github.com/adgatemedia/adgaterewards/blob/master/postback_pdo_example.php
//Modified to deal with my format and OCD
/**
* For a plain PHP page to receive the postback data from AdGate Media you may simply
* retrieve the array from the global $_GET variable. To ensure that the data is coming
* from AdGate Media check that the server sending the data is from AdGate Media by the ip
* address as listed on your affiliate panel at http://adgatemedia.com under
* the Postbacks Section and the Postback Information heading.
*/
//define('AdGate_IP', $postback_ip_address); // Note: as noted above change the IP to match what is in your affiliate panel.
$post_ip = $_SERVER['REMOTE_ADDR'];
//$data = null; //Don'te need this.
/**
* Check the Remote Address is AdGate Media
* if it is not throw an Exception
*/
//NOTE: Checking to make sure the post back ips match and if there is a user api key then check that.
if(in_array($post_ip, $atts)) //Some old greygoose and bad coding. I'm just checking to see if the ip address exists in shortcode.
{
if(!empty($site_api_key))
{
//Ok so we know the IP addy is fine, so lets check the api key
$postback_api_key = $_GET['api'];
if($site_api_key != $postback_api_key)
{
// Throw either a custom Exception or just throw a generic \Exception
header('HTTP/1.1 203 Partial Information');
exit(); //NOTE: I put exit as the AdGate method was bad
}
}
//$data = array($_GET);
// Process or Persist Data here inline or via a function call.
} else {
// Throw either a custom Exception or just throw a generic \Exception
header('HTTP/1.1 203 Partial Information');
exit(); //NOTE: I put exit as the AdGate method was bad.
}
//We are getting the email and then get the user id from that since they might be different between servers. I'm just guessing
if ( isset($_GET['email']) AND isset($_GET['point_value']) AND isset($_GET['status']) AND isset($_GET['tx_id']))
{
$user_email = sanitize_email($_GET['email']); //Huh they actualyly had this
$user_data = get_user_by('email', $user_email);
$user_id = $user_data->ID;
$points = isset($_GET['point_value']) ? $_GET['point_value'] : null;
$action = isset($_GET['status']) ? $_GET['status'] : null; //Determines if added (1) or subtracted (0) NOTE: This is different than Adgate where 2 is a chargeback
$action = isset($_GET['tx_id']) ? $_GET['tx_id'] : null; //This will be EPOCH time stamp being fed so yeah
//$ipuser = isset($_GET['ip']) ? $_GET['ip'] : null; //Note used or needed.
//NOTE: Ok we got that post back. And if the keys match in theory we have the variables above. But there is no hell in way I'm trusting adgate to SQL the users Database with that data
//Yeah its unlikely adgate may try an SQL injection their user base, but if the user is lax with their secret key and someone knows what this is, they can have an injection fest
$userId_sanitized = intval($userId); //User Id should be an int
$transactionId_sanitized = sanitize_text_field($transactionId); //This actually doesn't have to be collected but could be useful in one of the metas columsn
$action_sanitized = intval($action); //Good thing I read the documentation. According to adgate, if this is 1 there should be a reward and 2 if there is punishment for some reason. Should be int
$point_id = intval(vyps_mmo_sql_point_id_func()); //this is set by the wpdb so only one point at a time.
$point_amount = intval($points);
$reason = sanitize_text_field($atts['reason']);
$vyps_meta_id = 'mmo' . $userId_sanitized . $transactionId_sanitized; //the meta_id will be adgate with userid plus the transaction id. To see if its unique.
$vyps_meta_id = $vyps_meta_id
$current_balance = vyps_point_balance_func($point_id, $user_id); //need to check to see if they have an actual balance to report
//NOTE: I opted with letting the other site tell how much it will withdraw at a time.
if($action == 0 AND $current_balance >= $point_amount) // action = 1 CREDITED // action = 0 charge back
{
return vyps_point_deduct_func( $point_id, $point_amount, $user_id, $reason, $vyps_meta_id ); //I knew I had a good reason to use this
//The above should resturn a 1 if successful. I'm not going to add an add here just yet. This is an output system.
//If the get gets a 1 then it adds the points on the other side. I would recommend not doing an all system just like 100 points.
//I am going to add a balance api, but may not be needed.
}
else
{
return 0; //simple enough. It didn't work. Did not add points.
}
//The rest of the post back isn't needed. I will delete but will make a different page for ads or balances.
}
return "Invalid postback URL!";
}
/* Telling WP to use function for shortcode */
add_shortcode( 'vidyen-mmo-postback', 'vidyen_mmo_postback_func');

+ 17
- 0
vidyen-wc-mmo/vidyen-wc-mmo-menu.php View File

@ -48,12 +48,22 @@ function vidyen_wc_mmo_sub_menu_page()
//The icon. I'm suprised this works so well
$output_amount = abs(floatval($_POST['output_amount']));
if($_POST['api_key'] == '' OR !isset($_POST['api_key']))
{
$api_key = sanitize_text_field(str_replace('-', '', implode('-', str_split(substr(strtolower(md5(microtime().rand(1000, 9999))), 0, 30), 6))));
}
else
{
$api_key = sanitize_text_field(($_POST['api_key']));
}
$table_name_wc_mmo = $wpdb->prefix . 'vidyen_wc_mmo';
$data = [
'point_id' => $point_id,
'point_amount' => $point_amount,
'output_amount' => $output_amount,
'api_key' => $api_key,
];
$wpdb->update($table_name_wc_mmo, $data, ['id' => 1]);
@ -76,6 +86,8 @@ function vidyen_wc_mmo_sub_menu_page()
//Ouput Amount
$output_amount = floatval(vyps_mmo_sql_output_amount_func());
$api_key = sanitize_text_field(vyps_mmo_sql_api_key_func());
//It's possible we don't use the VYPS logo since no points.
$vyps_logo_url = plugins_url( 'includes/images/logo.png', __FILE__ );
@ -97,6 +109,7 @@ function vidyen_wc_mmo_sub_menu_page()
<th>Input Amount</th>
<th>WooWallet Icon</th>
<th>WooWallet Amount</th>
<th>API Key (To Reset Leave Blank)</th>
<th>Submit</th>
</tr>
<tr>
@ -105,13 +118,17 @@ function vidyen_wc_mmo_sub_menu_page()
<td><input type="number" name="point_amount" type="number" id="point_amount" min="1" max="1000000" step="1" value="' . $point_amount . '" required="true"></td>
<td><input type="number" name="output_id" type="number" id="output_id" min="1" step="1" value="' . $output_id . '" required="true"></td>
<td><input type="number" name="output_amount" type="number" id="output_amount" min="0.0000001" max="1000000" step="0.0000001" value="' . $output_amount . '" required="true"></td>
<td><input type="text" name="api_key" id="api_key" value="' . $api_key . '"></td>
<td><input type="submit" value="Submit"></td>
</tr>
</form>
</table>
<h2>API Key Copy and Paste</h2>
<p>'.$api_key.'</p>
<h2>Shortcode</h2>
<p><b>[vyps-mmo-bal]</b> for live balance.</p>
<p><b>[vyps-mmo-pe]</b> for live point exchange.</p>
<p><b>[vidyen-mmo-postback]</b> For the post back page. Use like Wannads with MMO template.</p>
<p>Simply put the shortcodes on a page and let it run with the point id from the VidYen point system.</p>
<p>Point ID is the point ID from the VidYen System. Found in Manage Points section of VYPS</p>
<p>NOTE: If you change this settings while a game is in play, they must close browser or tab and reload page as is server session based.</p>

+ 123
- 0
vidyen-wc-mmo/vidyen-wc-mmo-template-function.php View File

@ -0,0 +1,123 @@
<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
class vidyenMMOPageTemplater {
/**
* A reference to an instance of this class.
*/
private static $instance;
/**
* The array of templates that this plugin tracks.
*/
protected $templates;
/**
* Returns an instance of this class.
*/
public static function get_instance() {
if ( null == self::$instance ) {
self::$instance = new vidyenMMOPageTemplater();
}
return self::$instance;
}
/**
* Initializes the plugin by setting filters and administration functions.
*/
private function __construct() {
$this->templates = array();
// Add a filter to the attributes metabox to inject template into the cache.
if ( version_compare( floatval( get_bloginfo( 'version' ) ), '4.7', '<' ) ) {
// 4.6 and older
add_filter(
'page_attributes_dropdown_pages_args',
array( $this, 'register_project_templates' )
);
} else {
// Add a filter to the wp 4.7 version attributes metabox
add_filter(
'theme_page_templates', array( $this, 'add_new_template' )
);
}
// Add a filter to the save post to inject out template into the page cache
add_filter(
'wp_insert_post_data',
array( $this, 'register_project_templates' )
);
// Add a filter to the template include to determine if the page has our
// template assigned and return it's path
add_filter(
'template_include',
array( $this, 'view_project_template')
);
// Add your templates to this array.
$this->templates = array(
'vidyen-wc-mmo-template.php' => 'vidyen MMO Template',
);
}
/**
* Adds our template to the page dropdown for v4.7+
*
*/
public function add_new_template( $posts_templates ) {
$posts_templates = array_merge( $posts_templates, $this->templates );
return $posts_templates;
}
/**
* Adds our template to the pages cache in order to trick WordPress
* into thinking the template file exists where it doens't really exist.
*/
public function register_project_templates( $atts ) {
// Create the key used for the themes cache
$cache_key = 'page_templates-' . md5( get_theme_root() . '/' . get_stylesheet() );
// Retrieve the cache list.
// If it doesn't exist, or it's empty prepare an array
$templates = wp_get_theme()->get_page_templates();
if ( empty( $templates ) ) {
$templates = array();
}
// New cache, therefore remove the old one
wp_cache_delete( $cache_key , 'themes');
// Now add our template to the list of templates by merging our templates
// with the existing templates array from the cache.
$templates = array_merge( $templates, $this->templates );
// Add the modified cache to allow WordPress to pick it up for listing
// available templates
wp_cache_add( $cache_key, $templates, 'themes', 1800 );
return $atts;
}
/**
* Checks if the template is assigned to the page
*/
public function view_project_template( $template ) {
// Return the search template if we're searching (instead of the template for the first result)
if ( is_search() ) {
return $template;
}
// Get global post
global $post;
// Return template if post is empty
if ( ! $post ) {
return $template;
}
// Return default template if we don't have a custom one defined
if ( ! isset( $this->templates[get_post_meta(
$post->ID, '_wp_page_template', true
)] ) ) {
return $template;
}
// Allows filtering of file path
$filepath = apply_filters( 'page_templater_plugin_dir_path', plugin_dir_path( __FILE__ ) );
$file = $filepath . get_post_meta(
$post->ID, '_wp_page_template', true
);
// Just to be safe, we check if the file exist first
if ( file_exists( $file ) ) {
return $file;
} else {
echo $file;
}
// Return template
return $template;
}
}
add_action( 'plugins_loaded', array( 'vidyenMMOPageTemplater', 'get_instance' ) );

+ 22
- 0
vidyen-wc-mmo/vidyen-wc-mmo-template.php View File

@ -0,0 +1,22 @@
<?php
/*
Template Name: Adgate Post Back Template
*/
/*
language_attributes();
if ( ! get_theme_support( 'title-tag' ) )
{
wp_title();
}
wp_head();
body_class();
*/
while ( have_posts() ) : the_post();
//the_ID();
//post_class();
the_content();
endwhile;
//wp_footer();

+ 10
- 1
vidyen-wc-mmo/vidyen-wc-mmo.php View File

@ -3,7 +3,7 @@
Plugin Name: VidYen WooCommerce MMO Plugin
Plugin URI: https://wordpress.org/plugins/vidyen-point-system-vyps/
Description: Adds RPG like currencies to WooCommerce for VidYen Point System
Version: 0.0.43
Version: 0.0.47
Author: VidYen, LLC
Author URI: https://vidyen.com/
License: GPLv2
@ -46,6 +46,7 @@ function vidyen_wc_mmo_sql_install()
point_amount mediumint(9) NOT NULL,
output_id mediumint(9) NOT NULL,
output_amount decimal(32,8) NOT NULL,
api_key varchar(128) NOT NULL,
PRIMARY KEY (id)
) {$charset_collate};";
@ -53,12 +54,17 @@ function vidyen_wc_mmo_sql_install()
dbDelta($sql);
//create random api_key. Shall be santizied.
$key = sanitize_text_field(str_replace('-', '', implode('-', str_split(substr(strtolower(md5(microtime().rand(1000, 9999))), 0, 30), 6))));
//Default data
$data_insert = [
'point_id' => 1,
'point_amount' => 100,
'output_id' => 2,
'output_amount' => 1,
'api_key' => $key,
];
$wpdb->insert($table_name_wc_mmo, $data_insert);
@ -81,3 +87,6 @@ include( plugin_dir_path( __FILE__ ) . 'vidyen-wc-mmo-menu.php'); //Order 600
/*** AJAX ***/
include( plugin_dir_path( __FILE__ ) . 'includes/functions/ajax/vyps_mmo_bal_ajax.php');
include( plugin_dir_path( __FILE__ ) . 'includes/functions/ajax/vyps_mmo_exchange_ajax.php');
/*** Templater ***/
include( plugin_dir_path( __FILE__ ) . 'vidyen-wc-mmo-template-function.php'); //Order 600

Loading…
Cancel
Save