http://codex.wordpress.org/AJAX_in_Plugins
add_action('wp_ajax_AFajax', 'AFajaxcallback');
add_action('wp_ajax_nopriv_AFajax', 'AFajaxcallback');
// We get the plugin's option to initialize it
$AF_options = get_option('AF_options');
// admin actions
if ( is_admin() ){
add_action( 'admin_menu', 'AF_add_admin_menu' );
add_action( 'admin_enqueue_scripts', 'AF_load_admin_scripts');
add_action( 'admin_init', 'AF_register_settings' );
add_action('admin_notices', 'AF_admin_notices');
// displaying admin errors
function AF_admin_notices(){
settings_errors();
}
} else {
// non-admin enqueues, actions, and filters
add_action( 'wp_print_scripts', 'adfever_add_my_stylesheets' );
add_action( 'wp_head', 'adfever_generate_css');
if ($AF_options['AF_autoshow_afterpost'] == 1 || $AF_options['AF_autoshow_beforepost'] == 1 ) {
add_action( 'the_content', 'AF_add_outer');
}
}
// Adding the admin menu
function AF_add_admin_menu() {
$adfever_monetisation = add_menu_page( 'AdFever Monétisation', 'AdFever Monétisation', 'manage_options', 'adfever-monetisation', 'AF_options_do_page', plugins_url( 'adfever-monetisation/img/icon.png' ), 61 );
$double_menu_bug_fix = add_submenu_page('adfever-monetisation','','','manage_options','adfever-monetisation','AF_options_do_page');
$adfever_lienssponsos = add_submenu_page( 'adfever-monetisation', 'Liens sponsorisés AdFever', 'Liens sponsorisés', 'manage_options', 'adfever-liens-sponsorises', 'AF_options_do_page');
}
// register options to the wordpress settings API
function AF_register_settings() {
register_setting('AF_options', 'AF_options', 'AF_options_validate');
}
// we load the needed libs, scripts for the admin panel
function AF_load_admin_scripts( ) {
wp_enqueue_style('wp-color-picker');
wp_enqueue_script('AF_admin_scripts', plugins_url('js/admin_scripts.js', __FILE__), array('wp-color-picker'), false, true );
}
// Enqueue plugin style and scripts
function adfever_add_my_stylesheets() {
$AF_options = get_option('AF_options');
if ($AFoptions['AF_custom_styles'] == 0) {
wp_register_style( 'adfever_styles', plugins_url('adfever_styles.css', __FILE__) );
wp_enqueue_style( 'adfever_styles' );
/* column display #DISABLED#
if ($AF_options['AF_display_type'] == "column") {
wp_register_style( 'adfever_styles_column', plugins_url('adfever_styles_column.css', __FILE__) );
wp_enqueue_style( 'adfever_styles_column' );
}
*/
}
// AJAX script
wp_enqueue_script( "AFajaxcallback", plugin_dir_url( __FILE__ ) . 'js/scripts.js', array( 'jquery' ) );
wp_localize_script( 'AFajaxcallback', 'ajax_object', array( 'ajaxurl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'AFajaxscripts-nonce' ), 'AF_options' => $AF_options ) );
}
// Validates the AF_options when passed through the WP seetings API
function AF_options_validate($args){
//$args will contain the values posted in the settings form
if(!isset($args['AF_SID']) || !ctype_digit($args['AF_SID'])){
$args['AF_SID'] = '';
add_settings_error('AF_options', 'AF_invalid_SID', 'Votre SID n\'est pas valide', $type = 'error');
}
if(!isset($args['AF_AID']) || !ctype_digit($args['AF_AID'])){
$args['AF_AID'] = '';
add_settings_error('AF_options', 'AF_invalid_AID', 'Votre AID n\'est pas valide', $type = 'error');
}
//make sure you return the args
return $args;
}
// sets the default values if none are set on plugin activation
function AF_set_default_options() {
$options = get_option('AF_options');
// if no options set, we set the default value
if ($options == false) {
$defaults = array(
'AF_autoshow_afterpost' => 1,
'AF_autoshow_beforepost' => 0,
'AF_display_type' => 'line',
'AF_global_border' => 0,
'AF_global_border_top' => 1,
'AF_global_border_left' => 1,
'AF_global_border_right' => 1,
'AF_global_border_bottom' => 1,
'AF_border' => 0,
'AF_border_top' => 1,
'AF_border_left' => 1,
'AF_border_right' => 1,
'AF_border_bottom' => 1,
'AF_custom_styles' => 0,
'AF_title_size' => 13,
'AF_text_size' => 13,
'AF_link_size' => 13,
'AF_disclaimer_size' => 9,
'AF_disclaimer_position' => 'BR',
'AF_num_to_display' => 3
);
update_option( 'AF_options', $defaults );
}
}
register_activation_hook( __FILE__, 'AF_set_default_options' );
//////////////////////////////////////////
// Admin option page
//////////////////////////////////////////
function AF_options_do_page() {
?>
';
//here we add the global AF block style
$output .= '#adfeverlinks {';
if ($options['AF_bg_color'] != '') {
$output .= 'background-color: '.$options['AF_bg_color'].';';
}
// we add the global border if needed
if ($options['AF_global_border'] != 1) {
$output .= 'border: none;';
}
else {
if ($options['AF_global_border_top'] == 1) {
$output .= "border-top: ".$options['AF_global_border_size']."px ".$options['AF_global_border_type']." ".$options['AF_global_border_color'].";";
}
if ($options['AF_global_border_left'] == 1) {
$output .= "border-left: ".$options['AF_global_border_size']."px ".$options['AF_global_border_type']." ".$options['AF_global_border_color'].";";
}
if ($options['AF_global_border_right'] == 1) {
$output .= "border-right: ".$options['AF_global_border_size']."px ".$options['AF_global_border_type']." ".$options['AF_global_border_color'].";";
}
if ($options['AF_global_border_bottom'] == 1) {
$output .= "border-bottom: ".$options['AF_global_border_size']."px ".$options['AF_global_border_type']." ".$options['AF_global_border_color'].";";
}
}
// we close the global style
$output .= '} ';
// disclaimer text style
$output .= '.AF_disclaimer { ';
if($options['AF_disclaimer_color'] != '') {
$output .= "color: ".remspace($options['AF_disclaimer_color'])."; ";
}
$output .= " font-size: ".remspace($options['AF_disclaimer_size'])."px ; ";
if ($options['AF_disclaimer_position'] == 'TL' || $options['AF_disclaimer_position'] == 'BL') {
$output .= 'text-align: left; ';
} else {
$output .= 'text-align: right; ';
}
if ($options['AF_disclaimer_font'] != 'none') {
$output .= "font-family: ".$options['AF_disclaimer_font']."; ";
}
// we close the disclaimer styles
$output .= '} ';
// ads title styles
$output .= '#adfeverlinks a.AF_title { ';
if($options['AF_title_color'] != '') {
$output .= "color: ".remspace($options['AF_title_color'])."; ";
}
$output .= " font-size: ".remspace($options['AF_title_size'])."px ; ";
if ($options['AF_title_font'] != 'none') {
$output .= "font-family: ".$options['AF_title_font']."; ";
}
$output .= "} ";
// ads text styles
$output .= '#adfeverlinks a.AF_text { ';
if($options['AF_text_color'] != '') {
$output .= "color: ".remspace($options['AF_text_color'])."; ";
}
$output .= " font-size: ".remspace($options['AF_text_size'])."px ; ";
if ($options['AF_title_font'] != 'none') {
$output .= "font-family: ".$options['AF_text_font']."; ";
}
$output .= "} ";
// ads title styles
$output .= '#adfeverlinks a.AF_link { ';
if($options['AF_link_color'] != '') {
$output .= "color: ".remspace($options['AF_link_color'])."; ";
}
$output .= " font-size: ".remspace($options['AF_link_size'])."px ; ";
if ($options['AF_link_font'] != 'none') {
$output .= "font-family: ".$options['AF_link_font']."; ";
}
$output .= "} ";
// adds list item style (for individual ads borders
$output .= '#adfeverlinks li { ';
if ($options['AF_border'] == 0) {
$output .= 'border: none;';
}
else {
$output .= "border-top: ".$options['AF_border_size']."px ".$options['AF_border_type']." ".$options['AF_border_color'].";";
}
$output .= '}';
$output .= '#adfeverlinks li:first-child { border: none;}';
$output .= '';
echo $output;
}
}
//$AF_results = '';
//$AF_error = '';
// we add an outer div with the post ID so we can retrieve that ID in our JS
function AF_add_outer($output='', $post_id='') {
if ($post_id == '') {
$post_id = get_the_ID();
}
$AF_options = get_option('AF_options');
$ads = '';
if ($AF_options['AF_autoshow_beforepost'] == 1 || $AF_options['AF_autoshow_afterpost'] == 1) {
if ($AF_options['AF_autoshow_beforepost'] == 1) {
$output = $ads.' '.$output;
}
if ($AF_options['AF_autoshow_afterpost'] == 1) {
$output = $output.' '.$ads;
}
return $output;
} else {
// no autoshow enabled, we simply echo the adfever outer div
echo $ads;
}
}
function adfever_links($theID ='', $output = '', $AF_options='', $ajax=false){
// $debug = true;
if ($ajax) {
global $wpdb;
}
// echo $theID;
if ($theID != '') {
if ($AF_options == '') {
$AF_options = get_option( 'AF_options' );
}
// we retrieve the post's tags
$posttags = get_the_tags($theID);
$tag = '';
if ($posttags) {
foreach($posttags as $tag) {
$tags .= $tag->name . ' ';
}
}
if ($debug) { echo "--- TAGS: ".$tags; }
// we add the title - if no or few tags, it'll use the title as well
$tags .= get_the_title($theID);
// we cut the request down to adfever limit
$request = substr($tags, 0, 100);
if ($debug) { echo " --- REQUEST: ".$request; }
// Options adFeverSearch
$AF_AID = $AF_options['AF_AID']; //"8640";
$AF_SID = $AF_options['AF_SID']; //20844";
// adfever search query
$AF_search_settings = array ( 'start' => "0" , 'size' => (string)$AF_options['AF_num_to_display'] , "sid" => $AF_SID, "aid"=> $AF_AID, "adult" => "YES") ;
$AF_search = new AdFever ( $AF_search_settings ) ;
// setlocale ( LC_ALL, 'fr', 'fr_FR' ) ;
// $request = preg_replace ( '/[^\w]/', ' ', $request ) ;
//$request = preg_replace ( '/\s\s+/', ' ', $request ) ;
$AF_search->search ( $request ) ;
if ($debug) {
// print_r($AF_search);
echo "---REQUEST: ".$request;
echo "--- TOTAL RESULTS: ".($AF_search->total)."--- XML RESULTS :".$AF_search->return_total;
}
// we get the search results
if ($AF_search->total > 0) {
// $AF_results = 'Annonce '. (1). ' à '. ($AF_search->return_total). ' sur '. $AF_search->total ;
$AF_results .= "";
if ($AF_options['AF_disclaimer_position'] == 'TL' || $AF_options['AF_disclaimer_position'] == 'TR') {
$AF_results .= "
".$AF_options['AF_disclaimer_text']."";
}
$AF_results .= "
";
if ($AF_options['AF_disclaimer_position'] == 'BL' || $AF_options['AF_disclaimer_position'] == 'BR') {
$AF_results .= "
".$AF_options['AF_disclaimer_text']."";
}
$AF_resuluts .= "
";
}
// check if error, else displays the result
if (count ( $AF_search->errors ) > 0) {
foreach ( $AF_search->errors as $err ) {
$AF_error = $err ;
if ($debug) { echo " --- ERROR: ".$AF_error; }
}
} else {
if (!$ajax) {
//$AF_results = trim($AF_results, -1);
}
echo $AF_results;
}
}
}
?>