Plugin Directory

source: ads-for-visual-composer/trunk/advanced-ads-vc.php

Last change on this file was 3253353, checked in by advancedads, 9 months ago

Update to version 2.0.0 from GitHub

  • Property svn:executable set to *
File size: 7.6 KB
Line 
1<?php
2/**
3 * Plugin Name:       Ads for WPBakery Page Builder (formerly Visual Composer)
4 * Plugin URI:        https://wpadvancedads.com
5 * Description:       Display Advanced Ads as a Visual Composer Element
6 * Version:           2.0.0
7 * Author:            Advanced Ads
8 * Author URI:        https://wpadvancedads.com
9 * Text Domain:       ads-for-visual-composer
10 * Domain Path:       /languages
11 * License:           GPL-2.0+
12 * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
13 *
14 * based on Extend WPBakery Page Builder Plugin (formerly Visual Composer)
15 */
16
17if ( ! defined( 'ABSPATH' ) ) {
18        die( '-1' );
19}
20
21class Advanced_Ads_Visual_Composer {
22        /**
23         * Advanced_Ads_Visual_Composer constructor.
24         */
25        public function __construct() {
26                // We safely integrate with VC with this hook.
27                add_action( 'init', [ $this, 'check_dependencies' ] );
28                add_action( 'init', [ $this, 'add_arguments' ], 30 );
29                // load translations.
30                add_action( 'plugins_loaded', [ $this, 'ads_for_visual_composer_load_plugin_textdomain' ] );
31        }
32
33        /**
34         * Check if Advanced Ads and WP Bakery Visual Composer are installed
35         */
36        public function check_dependencies() {
37                // Check if WPBakery Page Builder is installed.
38                if ( ! defined( 'WPB_VC_VERSION' ) ) {
39                        // Display notice that Visual Composer is required.
40                        add_action( 'admin_notices', [ $this, 'show_vc_version_notice' ] );
41                }
42
43                // Check if Advanced Ads is installed.
44                if ( ! defined( 'ADVADS_VERSION' ) ) {
45                        // Display notice that Advanced Ads is required.
46                        add_action( 'admin_notices', [ $this, 'show_advads_version_notice' ] );
47                }
48        }
49
50        /**
51         * Add WP Bakery options
52         */
53        public function add_arguments() {
54
55                if ( ! defined( 'ADVADS_PLUGIN_BASENAME' ) || ! defined( 'WPB_VC_VERSION' ) ) {
56                        return;
57                }
58
59                vc_map( [
60                                'name'        => __( 'Advanced Ads – Ad', 'ads-for-visual-composer' ),
61                                'description' => __( 'Displays an Ad', 'ads-for-visual-composer' ),
62                                'base'        => 'the_ad',
63                                'icon'        => plugins_url( 'assets/icon.png', __FILE__ ),
64                                'category'    => 'Ads',
65                                'group'       => 'Advanced Ads',
66
67                                'params' => [
68                                        [
69                                                'type'        => 'dropdown',
70                                                'heading'     => __( 'Select an ad', 'ads-for-visual-composer' ),
71                                                'param_name'  => 'id',
72                                                'description' => __( 'Display an Ad', 'ads-for-visual-composer' ),
73                                                'value'       => $this->get_ads(),
74                                                'std'         => '',
75                                                'admin_label' => true,
76                                        ],
77                                ],
78                        ]
79                );
80
81                vc_map( [
82                                'name'        => __( 'Advanced Ads – Group', 'ads-for-visual-composer' ),
83                                'description' => __( 'Displays an Ad Group', 'ads-for-visual-composer' ),
84                                'base'        => 'the_ad_group',
85                                'icon'        => plugins_url( 'assets/icon.png', __FILE__ ),
86                                'category'    => 'Ads',
87                                'group'       => 'Advanced Ads',
88
89                                "params" => [
90                                        [
91                                                'type'        => 'dropdown',
92                                                'heading'     => __( 'Select a group', 'ads-for-visual-composer' ),
93                                                'param_name'  => 'id',
94                                                'description' => __( 'Displays an Ad Group', 'ads-for-visual-composer' ),
95                                                'value'       => $this->get_groups(),
96                                                'std'         => '',
97                                                'admin_label' => true,
98                                        ],
99                                ],
100                        ]
101                );
102
103                vc_map( [
104                                'name'        => __( 'Advanced Ads – Placement', 'ads-for-visual-composer' ),
105                                'description' => __( 'Displays an Ad Placement', 'ads-for-visual-composer' ),
106                                'base'        => 'the_ad_placement',
107                                'icon'        => plugins_url( 'assets/icon.png', __FILE__ ),
108                                'category'    => 'Ads',
109                                'group'       => 'Advanced Ads',
110
111                                'params' => [
112                                        [
113                                                'type'        => 'dropdown',
114                                                'heading'     => __( 'Select a placement', 'ads-for-visual-composer' ),
115                                                'param_name'  => 'id',
116                                                'description' => __( 'Displays an Ad Placement', 'ads-for-visual-composer' ),
117                                                'value'       => $this->get_placements(),
118                                                'std'         => '',
119                                                'admin_label' => true,
120                                        ],
121                                ],
122                        ]
123                );
124        }
125
126        /**
127         * Warn if WP Bakery Visual Composer plugin is missing
128         */
129        public function show_vc_version_notice() {
130                $plugin_data = get_plugin_data( __FILE__ );
131
132                echo wp_kses_post(
133                        sprintf(
134                                '<div class="error"><p>%s</p></div>',
135                                sprintf(
136                                /* translators: %s is the name of this plugin. */
137                                        __( '<strong>%s</strong> requires the <strong><a href="http://bit.ly/vcomposer" target="_blank">WPBakery Page Builder</a></strong> plugin to be installed and activated on your site.', 'ads-for-visual-composer' ),
138                                        esc_html( $plugin_data['Name'] )
139                                )
140                        )
141                );
142        }
143
144        /**
145         * Check if Advanced Ads 2.0 or newer is installed.
146         *
147         * @return bool|int
148         */
149        private function is_a2_2() {
150                return version_compare( ADVADS_VERSION, '2.0', '>=' );
151        }
152
153        /**
154         * Get all ads
155         *
156         * @return array
157         */
158        private function get_ads() {
159                global $wpdb;
160                static $ads;
161
162                if ( null === $ads ) {
163                        $ads = [ '' => '' ];
164
165                        if ( $this->is_a2_2() ) {
166                                foreach ( wp_advads_get_all_ads() as $id => $ad ) {
167                                        $ads[ $ad->get_title() ] = $id;
168                                }
169                        } else {
170                                foreach ( ( new Advanced_Ads_Model( $wpdb ) )->get_ads() as $ad ) {
171                                        $ads[ $ad->post_title ] = $ad->ID;
172                                }
173                        }
174                }
175
176                return $ads;
177        }
178
179        /**
180         * Get all groups
181         *
182         * @return array
183         */
184        private function get_groups() {
185                global $wpdb;
186                static $groups;
187
188                if ( null === $groups ) {
189                        $groups = [ '' => '' ];
190                        if ( $this->is_a2_2() ) {
191                                foreach ( wp_advads_get_all_groups() as $id => $group ) {
192                                        $groups[ $group->get_title() ] = $id;
193                                }
194                        } else {
195                                foreach ( ( new Advanced_Ads_Model( $wpdb ) )->get_ad_groups() as $group ) {
196                                        $groups[ $group->name ] = $group->term_id;
197                                }
198                        }
199                }
200
201                return $groups;
202        }
203
204        /**
205         * Get all placements
206         *
207         * @return array
208         */
209        private function get_placements() {
210                static $placements;
211
212                if ( null === $placements ) {
213                        $placements = [ '' => '' ];
214                        if ( $this->is_a2_2() ) {
215                                foreach ( wp_advads_get_placements_by_types( 'default' ) as $id => $placement ) {
216                                        $placements[ $placement->get_title() ] = $placement->get_slug();
217                                }
218                        } else {
219                                foreach ( get_option( 'advads-ads-placements', [] ) as $id => $placement ) {
220                                        if ( 'default' !== $placement['type'] ) {
221                                                continue;
222                                        }
223                                        $placements[ $placement['name'] ] = $id;
224                                };
225                        }
226                }
227
228                return $placements;
229        }
230
231        /**
232         * Warn if Advanced Ads is missing
233         */
234        public function show_advads_version_notice() {
235                $plugin_data = get_plugin_data( __FILE__ );
236                $plugins     = get_plugins();
237
238                if ( isset( $plugins['advanced-ads/advanced-ads.php'] ) ) { // is installed, but not active.
239                        $link = '<a class="button button-primary" href="' . wp_nonce_url( 'plugins.php?action=activate&amp;plugin=advanced-ads/advanced-ads.php&amp', 'activate-plugin_advanced-ads/advanced-ads.php' ) . '">' . __( 'Activate Now', 'ads-for-visual-composer' ) . '</a>';
240                } else {
241                        $link = '<a class="button button-primary" href="' . wp_nonce_url( self_admin_url( 'update.php?action=install-plugin&plugin=' . 'advanced-ads' ), 'install-plugin_' . 'advanced-ads' ) . '">' . __( 'Install Now', 'ads-for-visual-composer' ) . '</a>';
242                }
243
244                echo wp_kses_post(
245                        sprintf(
246                                '<div class="error"><p>%1$s&nbsp;%2$s</p></div>',
247                                sprintf(
248                                /* translators: %s is the name of this plugin. */
249                                        __( '<strong>%s</strong> requires the <strong><a href="https://wpadvancedads.com/#utm_source=advanced-ads&utm_medium=link&utm_campaign=activate-vc" target="_blank">Advanced Ads</a></strong> plugin to be installed and activated on your site.', 'ads-for-visual-composer' ),
250                                        $plugin_data['Name']
251                                ),
252                                $link
253                        )
254                );
255        }
256
257        /**
258         * Load translations
259         *
260         * @return void
261         */
262        public function ads_for_visual_composer_load_plugin_textdomain() {
263                load_plugin_textdomain( 'ads-for-visual-composer', false, basename( dirname( __FILE__ ) ) . '/languages/' );
264        }
265
266}
267
268new Advanced_Ads_Visual_Composer();
Note: See TracBrowser for help on using the repository browser.