Browse Source

ajout auto des input pour les APIkey + écriture des modèles dans le dossier du plugin en attendant une solution

pull/19/head
Chancellier Pierre-Jean 9 months ago
parent
commit
21d5a02b34
  1. 19
      action/tarteaucitron_desinstaller_service.php
  2. 33
      action/tarteaucitron_installer_service.php
  3. 42
      formulaires/configurer_tarteaucitron_services.php
  4. 58
      inc/tarteaucitron.php
  5. 2
      services/googlemaps.html
  6. 26
      tarteaucitron_administrations.php

19
action/tarteaucitron_desinstaller_service.php

@ -33,10 +33,13 @@ function action_tarteaucitron_desinstaller_service_dist(){
die('Service déjà désinstallé');
}
$squelettes_folder = tarteaucitron_dossier_squelettes();
$services_folder = $squelettes_folder . "services/";
$modeles_folder = $squelettes_folder . "modeles/";
$icones_folder = $squelettes_folder . "icones_barre/";
$services_folder = _DIR_PLUGIN_TARTEAUCITRON . "services/";
$modeles_folder = _DIR_PLUGIN_TARTEAUCITRON . "modeles/";
// $squelettes_folder = tarteaucitron_dossier_squelettes();
// $services_folder = $squelettes_folder . "services/";
// $modeles_folder = $squelettes_folder . "modeles/";
// $icones_folder = $squelettes_folder . "icones_barre/";
$file = $services_folder . $service . ".html";
if (file_exists($file)) {
@ -56,12 +59,8 @@ function action_tarteaucitron_desinstaller_service_dist(){
include_spip('inc/config');
$services_installes = lire_config("tarteaucitron/services_installes/");
$i = 0;
foreach($services_installes as $service_installe) {
if ($service==$service_installe){
array_splice($services_installes,$i,1);
}
$i++;
if ($services_installes[$service]) {
unset($services_installes[$service]);
}
ecrire_config("tarteaucitron/services_installes/", $services_installes);

33
action/tarteaucitron_installer_service.php

@ -33,29 +33,32 @@ function action_tarteaucitron_installer_service_dist(){
die('Service déjà installé');
}
$squelettes_folder = tarteaucitron_dossier_squelettes();
$services_folder = $squelettes_folder . "services/";
$modeles_folder = $squelettes_folder . "modeles/";
// $squelettes_folder = tarteaucitron_dossier_squelettes();
// $services_folder = $squelettes_folder . "services/";
// $modeles_folder = $squelettes_folder . "modeles/";
if (!is_dir($services_folder)) {
mkdir($services_folder,0755);
}
$services_folder = _DIR_PLUGIN_TARTEAUCITRON . "services/";
$modeles_folder = _DIR_PLUGIN_TARTEAUCITRON . "modeles/";
if (!is_dir($modeles_folder)) {
mkdir($modeles_folder,0755);
}
// if (!is_dir($services_folder)) {
// mkdir($services_folder,0755);
// }
// if (!is_dir($modeles_folder)) {
// mkdir($modeles_folder,0755);
// }
file_put_contents($services_folder . $service . ".html", "(tarteaucitron.job = tarteaucitron.job || []).push('" . $service . "');");
// TODO : récupérer le modèle HTML de tarteaucitron (scrapping ?)
file_put_contents($modeles_folder . 'tac_' . $service . '.html', "Placez le code HTML");
$code = tarteaucitron_scrap_service($service);
file_put_contents($services_folder . $service . ".html", $code['JS']);
file_put_contents($modeles_folder . 'tac_' . $service . '.html', $code['html']);
// TODO : créer un fichier icones_barre/monservice.png en 17px de côté avec le nom du service ?
// TODO : créer un fichier icones_barre/monservice.png en 17px de côté avec le nom du service pour le porte-plume ?
include_spip('inc/config');
$services_installes = lire_config("tarteaucitron/services_installes/");
if (!in_array($service,$services_installes,true)) {
$services_installes[] = $service;
if (!$services_installes[$service]) {
$services_installes[$service] = ($code['hasKey']) ? "haskey" : "nokey";
}
ecrire_config("tarteaucitron/services_installes/", $services_installes);

42
formulaires/configurer_tarteaucitron_services.php

@ -17,7 +17,9 @@ if (!defined('_ECRIRE_INC_VERSION')) return;
)
);
foreach($liste_services as $service) {
foreach($liste_services as $service => $hasKey) {
$champ = array(
'saisie' => 'checkbox',
'options' => array(
@ -29,45 +31,19 @@ if (!defined('_ECRIRE_INC_VERSION')) return;
);
$fieldset['saisies'][] = $champ;
}
switch ($type) {
case 'ads':
if ($hasKey) {
$fieldset['saisies'][] = array(
'saisie' => 'input',
'options' => array(
'nom' => 'facebookpixelkey',
'label' => 'Facebook Pixel ID',
'explication' => 'Enter your FacebookPixel ID',
'nom' => $service . 'key',
'label' => $service . ' APIkey',
'explication' => 'Enter your API Key',
'placeholder' => 'YOUR_ID',
'afficher_si' => '@facebookpixel@ == "facebookpixel"'
)
);
break;
case 'analytic':
$fieldset['saisies'][] = array(
'saisie' => 'input',
'options' => array(
'nom' => 'gtagkey',
'label' => 'Google global site tag (gtag.js)',
'explication' => 'Replace GA_MEASUREMENT_ID with the ID of the Google Analytics property to which you want to send data',
'placeholder' => 'GA_MEASUREMENT_ID',
'afficher_si' => '@gtag@ == "gtag"'
)
);
break;
case 'api':
$fieldset['saisies'][] = array(
'saisie' => 'input',
'options' => array(
'nom' => 'gmapkey',
'label' => 'Google Map API Key',
'explication' => 'Enter your Google Map API Key',
'placeholder' => 'API KEY',
'afficher_si' => '@googlemaps@ == "googlemaps"'
'afficher_si' => '@' . $service . '@ == "' . $service . '"'
)
);
break;
}
}
$saisies[] = $fieldset;

58
inc/tarteaucitron.php

@ -55,7 +55,7 @@ function tarteaucitron_liste_services_actifs($objet, $id_objet, $page, $recherch
}
/**
* Retourne la liste des services TarteAuCitron et leur statut (enabled,disabled)
* Retourne la liste des services TarteAuCitron et leur statut (installe,desinstalle)
*
* @return Array
*/
@ -69,7 +69,7 @@ function tarteaucitron_liste_services() {
foreach($lines as $line) {
if(preg_match('/^tarteaucitron\.services\.(\w+)/', $line, $matches)) {
$service = $matches[1];
$statut = in_array($service,$services_installes) ? 'installe' : 'desinstalle';
$statut = ($services_installes[$service]) ? 'installe' : 'desinstalle';
if (preg_match('/"type": "(\w+)",/', $lines[$i+2], $matches)) {
$type = $matches[1];
@ -77,7 +77,8 @@ function tarteaucitron_liste_services() {
$list_services[$service] = array(
'type' => $type,
'statut' => $statut
'statut' => $statut,
'hasKey' => ($services_installes[$service] == 'haskey')
);
}
$i++;
@ -99,7 +100,7 @@ function tarteaucitron_liste_types_installes() {
foreach($services as $service => $prop) {
if ($prop['statut']=='installe') {
$list_types[$prop['type']][] = $service;
$list_types[$prop['type']][$service] = $prop['hasKey'];
}
}
@ -118,4 +119,53 @@ function tarteaucitron_dossier_squelettes() {
$dir_squelettes = $dir_racine . 'squelettes/';
return $dir_squelettes;
}
function tarteaucitron_scrap_service($service) {
$tarteaucitron_url = 'https://tarteaucitron.io/fr/install/';
$html = file_get_contents($tarteaucitron_url);
$ret['JS'] = '';
$ret['html'] = '';
$doc = new DOMDocument('1.0', 'UTF-8');
$internalErrors = libxml_use_internal_errors(true);
$doc->loadHTML($html);
libxml_use_internal_errors($internalErrors);
$elem = $doc->getElementById('s_' . $service);
$id = $elem->lastChild->attributes['id']->value;
$elem = $doc->getElementById('m' . $id);
$span_elements = $elem->getElementsByTagName('span');
$is_code_js_found = false;
$nb_items = $span_elements->length;
for($i=0; $i<$nb_items; $i++) {
$child = $span_elements->item($i);
$attributes = $child->attributes;
foreach($attributes as $attr_name => $attr) {
if ($attr_name == "class" && $attr->value == "code" && !$is_code_js_found) {
$b = $child->getElementsByTagName('b');
if ($b->length != 0) {
$newelement = $doc->createTextNode("'#CONFIG{tarteaucitron/services/" . $service . "key}'");
$child->replaceChild($newelement,$b[0]);
$ret['hasKey'] = true;
}
$s = $child->getElementsByTagName('s');
$child->removeChild($s[0]);
$script = trim(strip_tags($child->nodeValue));
$script = preg_replace('/(\n\s+)/', "\n", $script);
$script = str_replace("''","'",$script);
$ret['JS'] = $script;
$is_code_js_found = true;
}
}
}
// TODO : récupérer le HTML pour le modèle
$ret['html'] = "<p>Placez votre code HTML</p>";
return $ret;
}

2
services/googlemaps.html

@ -1,2 +1,2 @@
tarteaucitron.user.googlemapsKey = '#CONFIG{tarteaucitron/services/gmapkey}';
tarteaucitron.user.googlemapsKey = '#CONFIG{tarteaucitron/services/googlemapskey}';
(tarteaucitron.job = tarteaucitron.job || []).push('googlemaps');

26
tarteaucitron_administrations.php

@ -78,7 +78,7 @@ function maj_tarteaucitron_cfg() {
break;
case "gmap" :
if (!empty($value)) {
$cfg['services']['gmapkey'] = $value;
$cfg['services']['googlemapskey'] = $value;
$cfg['services']['googlemaps'] = array("googlemaps");
}
break;
@ -98,18 +98,18 @@ function maj_tarteaucitron_cfg() {
// TODO : ajouter Matomo
$cfg['services_installes'] = array(
'canalu',
'dailymotion',
'facebook',
'facebookpixel',
'gagenda',
'googlemaps',
'gtag',
'twitter',
'twitterembed',
'vimeo',
'webtvnu',
'youtube'
'canalu' => 'nokey',
'dailymotion' => 'nokey',
'facebook' => 'nokey',
'facebookpixel' => 'haskey',
'gagenda' => 'nokey',
'googlemaps' => 'haskey',
'gtag' => 'haskey',
'twitter' => 'nokey',
'twitterembed' => 'nokey',
'vimeo' => 'nokey',
'webtvnu' => 'nokey',
'youtube' => 'nokey'
);
ecrire_config('tarteaucitron',$cfg);

Loading…
Cancel
Save